Added new line before a specific pattern problem asking


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Added new line before a specific pattern problem asking
# 1  
Old 03-08-2010
Added new line before a specific pattern problem asking

Input file:
Code:
Sample1 Type pattern 842 3150
Sample1 Type range 842 3150
Sample1 Type pattern 842 1127
Sample1 Type option 842 1127
Sample1 Type length 1483 1603
Sample1 Type pattern 1483 1603
Sample1 Type length 1698 1747
Sample1 Type option 1698 1747
Sample1 Type length 1868 1935
Sample1 Type pattern 1868 1935
Sample1 Type length 2164 2262

Output file:
Code:
Sample1 Type pattern 842 3150
Sample1 Type range 842 3150

Sample1 Type pattern 842 1127
Sample1 Type option 842 1127
Sample1 Type length 1483 1603

Sample1 Type pattern 1483 1603
Sample1 Type length 1698 1747
Sample1 Type option 1698 1747
Sample1 Type length 1868 1935

Sample1 Type pattern 1868 1935
Sample1 Type length 2164 2262

I'm trying to added a new line before it matched that column 3 is "pattern". Thanks a lot for any advice.
# 2  
Old 03-08-2010
Hai!!! you can try the following sed command

Code:
sed -e 's/\(\(.* \)\{2\}\(pattern\)\)/\n\1/g' file

This will print as follows

Code:
Sample1 Type pattern 842 3150
Sample1 Type range 842 3150

Sample1 Type pattern 842 1127
Sample1 Type option 842 1127
Sample1 Type length 1483 1603

Sample1 Type pattern 1483 1603
Sample1 Type length 1698 1747
Sample1 Type option 1698 1747
Sample1 Type length 1868 1935

Sample1 Type pattern 1868 1935
Sample1 Type length 2164 2262


Last edited by thillai_selvan; 03-08-2010 at 01:01 AM..
# 3  
Old 03-08-2010
Try this,

Code:
sed -r "s/(^.+ +.+ +pattern .+$)/\n\1/g" file

# 4  
Old 03-08-2010
Sed:

Code:
sed  '/pattern/{N;s/^/\n/}' filename


cheers,
Devaraj Takhellambam
# 5  
Old 03-08-2010
Thanks a lot, devtakh.
Your sed code worked perfectly for my case.
Thanks again for sharing Smilie
# 6  
Old 03-08-2010
you can try this

Code:
sed '/pattern/{x;p;x}' file.txt

Smilie
# 7  
Old 03-08-2010
Quote:
Originally Posted by Manabhanjan
you can try this

Code:
sed '/pattern/{x;p;x}' file.txt

Smilie
Hai actually the poster want to add a new line if the pattern is there in the 3rd column. But your sed script will simply match a pattern line and will add a new line before to that. kindly change it
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to cut a specific pattern from a line

Hello, I need to cut a specific pattern from a line irrespecitve of knowing field place. I am aware to cut field if you know the place of the field, but for me The sting place varies. 1468129514436,0,something_error,Non HTTP response code: java.net.URISyntaxException,Non HTTP response... (5 Replies)
Discussion started by: mirwasim
5 Replies

2. Shell Programming and Scripting

Grep pattern after specific line number in a file

Hi guys, I am running a while loop in a script ro read a file line by line. Now I want to run a grep only on the lines below the line I am that is being read by the while loop. Eg: If my while loop is on line 4 of the file, the grep only runs below line 4 and does not include line 1,2... (3 Replies)
Discussion started by: Junaid Subhani
3 Replies

3. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

4. Shell Programming and Scripting

Delete lines that contain a pattern from specific line to the end.

Gents, I am trying to delete all lines which start with "H" character, but keeping the fist header. Example In the input file I will delete all lines starting from line 8 which contents character "H" to the end of the file. I try sed '8,10000{/^H/d;}' file But as don't know the end... (1 Reply)
Discussion started by: jiam912
1 Replies

5. Shell Programming and Scripting

Replace the line with specific pattern

Hello All I'm trying to change one string from a file contening this patern: xxxx-xxxx 4 numbers - end 4 other numbers This is a sample of the file: LDR 00679 am a2200205 4500 =001 3617 =008 030219s2000\\\\xxx|||||\||||\00|\0\spa\d =020 \\$a0211-1942 =041 \\$aCastellà =093 ... (5 Replies)
Discussion started by: ldiaz2106
5 Replies

6. Shell Programming and Scripting

Replace string in line below specific pattern?

Hi, I'm trying to replace a string with sed, in a text file containing this pattern: location alpha value x location beta value y location gamma value y location delta value y location theta value z ... What I want to achieve is: Find location beta into text file... (1 Reply)
Discussion started by: TECK
1 Replies

7. Shell Programming and Scripting

Problem in Pattern-Specific Actions in awk

Hi i am in learning phase of unix i am able to understand basic of awk but not able to understand Pattern-Specific Actions in awk below is the snippet . awk ' / *\$*\. */ { print $1,$2,$3,"*"; } / *\$0\. */ { print ; } ' fruit_prices.txthere i am not getting the use of wild card. what... (4 Replies)
Discussion started by: scriptor
4 Replies

8. Programming

Print specific pattern line in c++

Input file: @HWI-BRUNOP1_header_1 GACCAATAAGTGATGATTGAATCGCGAGTGCTCGGCAGATTGCGATAAAC +HWI-BRUNOP1_header_1 TNTTJTTTETceJSP__VRJea`_NfcefbWe Desired output file: >HWI-BRUNOP1_header_1 GACCAATAAGTGATGATTGAATCGCGAGTGCTCGGCAGATTGCGATAAAC >HWI-BRUNOP1_header_2... (10 Replies)
Discussion started by: cpp_beginner
10 Replies

9. Shell Programming and Scripting

extract specific line if the search pattern is found

Hi, I need to extract <APPNUMBER> tag alone, if the <college> haas IIT Chennai value. college tag value will have spaces embedded. Those spaces should not be suppresses. My Source file <Record><sno>1</sno><empid>E0001</empid><name>Rejsh suderam</name><college>IIT ... (3 Replies)
Discussion started by: Sekar1
3 Replies

10. Shell Programming and Scripting

merge columns into one line after a specific pattern

Hi all, im a linux newbie, plz help! I have a file - box -------- Fox-2 -------- UF29 zip42 -------- zf-CW SNF2_N Heli_Z -------- Fox -------- Kel_1 box (3 Replies)
Discussion started by: sam_2921
3 Replies
Login or Register to Ask a Question