Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    Join Date
    Jul 2006
    Posts
    115

    Unanswered: merge lines based on specific last pattern of a line

    pls advise how to merge the following lines (by sed command)

    1;
    2
    3
    4
    5;
    6
    7;

    to becomes
    1
    2345
    67

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Code:
    nawk -v RS=';' '$1=$1' myFile
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  3. #3
    Join Date
    Jul 2006
    Posts
    115
    thx vgersh99 ... how about

    1;
    2
    3
    4
    ;5;
    6
    7;

    expected result:1) 2) no data changed except lines merging 2) only last ; of each merged line is replaced
    =>
    1
    234;5
    67

  4. #4
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Code:
    nawk '{a=(a=="")? $0 : a OFS $0}; /;$/ { print substr(a, 1, length(a)-1);a=""}' myFile
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  5. #5
    Join Date
    Jul 2006
    Posts
    115
    woo... but donno why my result contains space between fields ... pls advise
    os : sun unix (soloris)

    mine:
    1
    2 3 4 ;5
    6 7

  6. #6
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Code:
    nawk '{a=(a=="")? $0 : a OFS $0}; /;$/ { gsub(OFS, "", a);print substr(a, 1, length(a)-1);a=""}' myFile
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  7. #7
    Join Date
    Jul 2006
    Posts
    115
    sorry vgersh99, i mislead you .. my problem is merge the broken line due to content in text file:
    e.g
    1st data line <EOL>
    2nd data
    line <EOL>
    3rd
    data
    line <EOL>
    4th data line <EOL>
    =>
    1st data line <EOL>
    2nd data line <EOL>
    3rd data line <EOL>
    4th data line <EOL>

    pls advise

  8. #8
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Given the previous solution, what have you tried and where exactly are you stuck?
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  9. #9
    Join Date
    Jul 2006
    Posts
    115
    $cat tmp3
    1st data line <EOL>
    2nd data
    line <EOL>
    3rd
    data
    line <EOL>
    4th data line <EOL>

    command:
    nawk '{a=(a=="")? $0 : a OFS $0}; /;$/ { gsub(OFS, "", a);print substr(a, 1, length(a)-1);a=""}' tmp3
    =>
    no result

    command:
    nawk '{a=(a=="")? $0 : a OFS $0}; />$/ { gsub(OFS, "", a);print substr(a, 1, length(a)-1);a=""}' tmp3
    =>
    1stdataline<EOL
    2nddataline<EOL
    3rddataline<EOL
    4thdataline<EOL

  10. #10
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    This might work :
    Code:
    cat your_file | gawk '{
                    split( $0, a, "" )
                    for( v in a ) {
                            if ( a[v] >= 0 && a[v] <=9 ) printf a[v]
                            if ( a[v] == ";" ) print "\n"
                    }
            }' | grep "."
    But it assumes you only want to print out the chars 0-9.

    Mike

  11. #11
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    Code:
    nawk -v sep='<EOL>' '{a=(a=="")? $0 : a OFS $0}; $NF ~ (sep "$") { print substr(a, 1, length(a) - length(sep));a=""}' myFile
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  12. #12
    Join Date
    Jul 2006
    Posts
    115
    oh but my unix version has no gawk command... btw, can sed work ? checked from sed command, it can allow us to merge line selectively but donno why to code.

  13. #13
    Join Date
    Jul 2006
    Posts
    115
    Quote Originally Posted by vgersh99
    Code:
    nawk -v sep='<EOL>' '{a=(a=="")? $0 : a OFS $0}; $NF ~ (sep "$") { print substr(a, 1, length(a) - length(sep));a=""}' myFile
    revised as following in order to keep <EOL>

    nawk -v sep='<EOL>' '{a=(a=="")? $0 : a OFS $0}; $NF ~ (sep "$") { print a;a=""}' tmp3
    =>
    1st data line <EOL>
    2nd data line <EOL>
    3rd data line <EOL>
    4th data line <EOL>
    =>
    extra spaces found b/w "2nd data " & "3rd..." line

  14. #14
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    not sure what you're implying and/or what to change.
    If you're looking to get rid of the extra spaces, look into the previous solution (hint: use 'gsub').

    In the future, please use the vB Codes when posting data or code samples.
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  15. #15
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    gawk and awk are very similar, both are usually available with gawk has more features. I've rewritten the code extensively to use awk :
    Code:
    cat your_file | awk '{
                    split( $0, a, "" )
                    for( v in a ) {
                            if ( a[v] >= 0 && a[v] <=9 ) printf a[v]
                            if ( a[v] == ";" ) print "\n"
                    }
            }' | grep "."
    Mike

Posting Permissions

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