Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2005
    Posts
    19

    Unanswered: Shell - Split a line by a phrase?

    Does anyone know how to use Shell to split a line based on a certain phrase:

    so to split this long line:

    --BOL--|some text|some text too|some text also|--EOL--|some text|some text too|some text also|--EOL--|--BOL--|some text|some text too|some text also|--EOL--|some text|some text too|some text also|--EOL--|


    I want to split this long line by the phrase --EOL--| so that a new line starts after each --EOL--|

    Any ideas? thanks a lot

  2. #2
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    nawk -f farmer.awk file.txt

    farmer.awk:
    Code:
    BEGIN {
      FS="[|]--EOL--[|]"
      OFS="|--EOL--|"
    }
    {
       for(i=1; i <= NF; i++)
         if( i < NF )
           print $i OFS
         else
           if (length($i) != 0) print $i OFS
    }
    Last edited by vgersh99; 02-08-05 at 23:22.
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  3. #3
    Join Date
    Feb 2005
    Posts
    19
    I'm not sure if it works yet.... I'm still trying to concatenate the lines where it looks like what I wrote above. Right now each string before each bar is on separate lines. Once I figure that out, I will run your script to make the new lines only after the --EOL--

  4. #4
    Join Date
    Apr 2004
    Location
    Boston, MA
    Posts
    325
    maybe you should figure out the way to get get where you want to be from where you CURRENTLY are without any intermediate format.

    Just an idea..... the rest is up to you!
    vlad
    +-----------------------+
    | #include <disclaimer.h> |
    +-----------------------+

  5. #5
    Join Date
    Feb 2005
    Posts
    19
    Ultimately I want to have this:

    "start_here"|ldfjlsdffj|93849340|9034093|"end_here "|
    "start_here"|ldfjlsdffj|93849340|9034093|"end_here "|

    but right now, all of it is on separate lines but I can't figure out to concatenate only between the "start_here" and "end_here" phrases. So I was trying to concatenate the whole file into one line and then just make a newline after each "end_here" but that's not working either because it creates too big of a line for me to VI. So I'm not sure what to do anymore....

  6. #6
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Something like this?
    Code:
    awk 'BEGIN {aline="";} { aline = aline $0 "|" ;} /end_here/ { print aline; aline = ""; }' input.file
    This assumes that the first line in the file is "start_here".

Posting Permissions

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