Find the answer to your Linux question:
Results 1 to 5 of 5
Hi guys, I'm having some difficulties in insert some details to the following contents. I need to insert "TEST" under MIR & a value "25" to the next line. So ...
  1. #1
    Just Joined!
    Join Date
    Jul 2009
    Posts
    2

    Difficulties Inserting Data to Text File

    Hi guys, I'm having some difficulties in insert some details to the following contents.
    I need to insert "TEST" under MIR & a value "25" to the next line.

    So far, I am able to insert "TEST" by using awk to capture MIR as the identifier.
    However, I am having some difficulties in inserting "25" to the 2nd split line after MIR

    Recordset MIR and ATR contains split lines.
    I need some help to join up the split lines.
    Afterwhich I need to insert value 25 to MIR recordset.
    Can any nice souls here help me? Thanks


    Existing Content
    ATR:12:21:05 5-Jul-2009|
    /image/bin.rls/stdf_repair XXX_TCS5B_WS2_R03_09Jul05_
    09_48.stdf
    MIR:3UPF22030.1||TCS5B Probe|T09-J750|J750|
    ||||||||||SG|||||||T09-J750
    SDR:1|0|0,1|Prober|H05|ProbeCard|P02||TCS5B_WS2_R03|0 |0
    PMR:256|768|2|C10, k4.8, k3.5|AVDD|1|0
    PMR:256|768|5|C10, k4.8, k3.5|AVDD|1|1

    Expected Output
    ATR:12:21:05 5-Jul-2009|/image/bin.rls/stdf_repair XXX_TCS5B_WS2_R03_09Jul05_09_48.stdf
    MIR:3UPF22030.1|TEST|TCS5B Probe|T09-J750|J750|||||25||||||SG|||||||T09-J750
    SDR:1|0|0,1|Prober|H05|ProbeCard|P02||TCS5B_WS2_R03|0 |0
    PMR:256|768|2|C10, k4.8, k3.5|AVDD|1|0
    PMR:256|768|5|C10, k4.8, k3.5|AVDD|1|1

    PS:
    The 2 lines are not joined together.
    MIR:3UPF22030.1|TEST|TCS5B Probe|T09-J750|J750|07:39:58 5-Jul-2009|5-
    Jul-2009|Operator|P|0|10||0|Rev03|IG-XL|3.40.16||||25||||||SG|||||||T09-J750
    This is the code I'd done for inserting "TEST"

    cat $atdf_file | nawk 'BEGIN {
    FS="|" } {
    where = match($1,"MIR*")
    # Process MIR field
    if (where != 0) {
    replace=0
    if($2 == ""){
    $2 = "TEST"
    OFS = "|"
    print $0
    break }
    if(replace==0){
    print $0 } }
    else {
    print $0 }
    }
    ' > tmp.atdf

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    However, I am having some difficulties in inserting "25" to the 2nd line below MIR as there is no fixed identifier.
    I'm not sure what you mean by "no fixed identifier"...I guess what I'm asking is - How do you know where to insert "25"...G4143
    Make mine Arch Linux

  3. #3
    Just Joined!
    Join Date
    Jul 2009
    Posts
    2
    Hi gerard4143,
    I had make a mistake in my first post.
    MIR recordset contains 2 split lines.
    I figure it out that I need to join up the 2 split lines to make things easier for additional text insert.

  4. #4
    Just Joined!
    Join Date
    Mar 2008
    Posts
    5
    a sed script to do what you want:
    Code:
    /^ATR:/{
        N
        s/\n//
        h
        n
        H
        x
        s/\n//
        p
        b end
    }
    /^MIR:/{
        N
        s/\n//
        p
        b end
    }
    p
    :end
    pop it into a file and run it against your data file:
    Code:
    sed -n -f sedscript datafile
    /^ATR:/ - match the regex
    { } - perform the commands within the parenthesis on the matched line
    N - append the next line of the input
    s/\n// - substitute "nothing" for the newline character which sed will have inserted between the two lines in the pattern space
    h - overwrite the contents of the hold space with the pattern space
    n - get next line into the pattern space
    H - append the pattern space to the hold space
    x - exchange the hold space and the pattern space
    s/\n// - strip the new line character again
    p - output the pattern space
    b - jump to the label (which I have used here, but without a named label, jump to the end, which I could just have easily used

  5. #5
    Just Joined!
    Join Date
    Mar 2008
    Posts
    5
    sorry, forgot about the insertion of text:
    change the /MIR:/
    Code:
    /MIR:/{
        s/\(^MIR:.*\.1|\)\(.*\)/\1TEST|\225/
        N
        s/\n//
        p
        b end
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...