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
# 8  
Old 03-08-2010
Hi patrick87,
What is your requirement whether that pattern will in the third column or anywhere in the line.
devtakh's solution seems to correct,but its not exactly correct,because he is matching the pattern in line.He has not match the pattern with the third column.
His solution will work for the following input also.

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

Here the pattern is in the second column.
# 9  
Old 03-08-2010
Awk solution...

Code:
awk '$3=="pattern"{print "\n"$0;next}1' infile

# 10  
Old 03-08-2010
Try:

Code:
awk '/pattern/&&NR>1 { print RS; }NR' file

# 11  
Old 03-08-2010
If specific to a column, ignoring extra line if matches in the first line itself.

Code:
sed  '2,$s/\([^ ]*\)\( .*\)\( pattern\)\(.*\)/\n\1\2\3\4/' file


cheers,
Devaraj Takhellambam
# 12  
Old 03-08-2010
Try this script,

Code:
#!/bin/sh
while read line
do
        val=` echo $line | cut -d " " -f 3 `
        if [[ $val == "pattern" ]]
        then
                echo -e "\n$line"
        else
                echo $line
        fi
done < inputfile

# 13  
Old 03-08-2010
Hi malcomex999,
Your awk solution worked perfectly to my case too Smilie
Thanks a lot.
# 14  
Old 03-08-2010
Quote:
Originally Posted by dennis.jacob
Try:

Code:
awk '/pattern/&&NR>1 { print RS; }NR' file

If you are looking for appatern in third col, try:

Code:
awk '$3 ~ /pattern/&&NR>1 { print RS"\b" ;} NR ' file

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