If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Shell - Split a line by a phrase?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-08-05, 17:13
old_farmer old_farmer is offline
Registered User
 
Join Date: Feb 2005
Posts: 19
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
Reply With Quote
  #2 (permalink)  
Old 02-08-05, 17:26
vgersh99 vgersh99 is offline
Registered User
 
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
}
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+

Last edited by vgersh99; 02-08-05 at 22:22.
Reply With Quote
  #3 (permalink)  
Old 02-08-05, 19:49
old_farmer old_farmer is offline
Registered User
 
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--
Reply With Quote
  #4 (permalink)  
Old 02-08-05, 21:30
vgersh99 vgersh99 is offline
Registered User
 
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> |
+-----------------------+
Reply With Quote
  #5 (permalink)  
Old 02-09-05, 15:40
old_farmer old_farmer is offline
Registered User
 
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....
Reply With Quote
  #6 (permalink)  
Old 02-09-05, 17:48
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,454
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".
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On