how do I break line in a file when a pattern is matched ?

Hi All,

I am stuck for quite sometime now. Below is a line in my file -
GS|ED|001075|001081|20110626|1806|100803|X|004010ST|130|100803001

This line occurs only once and it is the second line.

I have to break this line into two lines from ST (bold) such that it looks like -
GS|ED|001075|001081|20110626|1806|100803|X|004010
ST|130|100803001

I have been trying pattern matching techniques using sed and awk but in vain till now. Would appreciate if someone can help me out. I am using bash shell.

--Immi

echo 'GS|ED|001075|001081|20110626|1806|100803|X|004010ST|130|100803001' | nawk '{gsub("ST",RS "&")}1'

Thanks a lot @vgersh99 !!!

---------- Post updated at 03:29 PM ---------- Previous update was at 03:19 PM ----------

I used awk because the system is very minimal and requesting nawk would take 2-3 days. The file contains numerous records and this is the second line which gotta be split into second and third line. I am trying to achieve like below where Intermediate_2 is the filename containing records:-

sed '2p' Intermediate_2 | awk '{gsub("ST",RS "&")}1'
OR
sed '/GS/p' Intermediate_2 | awk '{gsub("ST",RS "&")}1'

As you would know, placing this in my script produces duplicates of the split-up lines. If I use above sed(s) with -n option, then only the split lines are printed and the other contents goes away.

Is there a workaround to this ?

I think you want this:

awk 'FNR==2{gsub("ST",RS "&")}1' Intermediate_2

Thanks a lot !!! :slight_smile:

sed '2 s/ST/\nST/' Intermediate_2