Search for 2 string in 2 lines with sed ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search for 2 string in 2 lines with sed ?
# 1  
Old 11-06-2014
Search for 2 string in 2 lines with sed ?

Hi !
I want to search a string in all lines with sed. If that string is there, i want to look for an other string in the next line.If that string is there i want to put an other line under it.

eg:
Code:
aaa
bbb
ccc
ddd

Code:
cat bla.txt | sed -e '/aaa/a\' -e ' \!!!'

in the upper case, i would have following outputbacause i am looking for aaa:
aaa
Code:
!!!
bbb
ccc
ddd

how can i look for that aaa and check that the next line is bbb so i can have the following output:
Code:
aaa
bbb
!!!
ccc
ddd

Thanks for your help
Mfg Fugitivus
Moderator's Comments:
Mod Comment Please use CODE tags for sample input and output as well as for sample code.

Last edited by Don Cragun; 11-06-2014 at 05:07 AM.. Reason: Add CODE tags.
# 2  
Old 11-06-2014
How about
Code:
sed -e '/aaa/ {N; /aaa\nbbb/ s/$/\n!!!/}' file

given your sed version allows for the "\n" replacement...
# 3  
Old 11-06-2014
If your sed doesn't allow backslash escapes in the replacement text of a substitute command, try:
Code:
sed '/aaa/a\
!!!
' bla.txt

Note that there must be a newline immediately following the backslash. If you want to put this in a script and pass in the string to be searched for as the 1st operand and the name of the file to be searched as the 2nd operand, try:
Code:
#!/bin/ksh
sed "/$1/"'a\
!!!
' "$2"

so you could invoke:
Code:
myscript "aaa" bla.txt

to get the 1st output you wanted and:
Code:
myscript "bob" bla.txt

to get the 2nd output you wanted.

This example uses the Korn shell, but this will work with any shell that is based on Bourne shell syntax.
# 4  
Old 11-06-2014
Thank you for your Help !

Code:
sed -e '/aaa/ {N; /aaa\nbbb/ s/$/\n!!!/}' file

is a realy good thing. is there a way to work with negations ?
like if i would look for an aaa and and print a bbb to the next line if bbb not exists in that line ?!?

Mfg Fugitivus
# 5  
Old 11-06-2014
Try:
Code:
sed '/aaa/{N; /\nbbb/!s/\n/&bbb&/;}' file

Although that would not work for occurrences of aaa on the last line..


--
Also last line:
Code:
sed '/aaa/{$s/$/\
\bbb/; $!N; /\nbbb/!s/\n/&bbb&/;}' file


Last edited by Scrutinizer; 11-06-2014 at 12:12 PM..
# 6  
Old 11-06-2014
Only GNU sed works as a one-liner. The classic Unix sed needs a multi-liner:
Code:
sed '
  /aaa/ {
    N
    /bbb/ a\
!!!
  }
' bla.txt

# 7  
Old 11-07-2014
Thanks thats exactly what i want to do !!!
I'll think i have to experimentate a little bit more to understand sed Smilie
Realy nice tool !!!

Mfg Fugitivus
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for string on a range of lines

Hi, I am trying to parse a file searching for specific set of string and then within those set of strings finding a keyword. The script works the way that I intended it to be but I thought it could be a lot simpler may be. Any advice will be much appreciated. The script at the moment is as... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. Shell Programming and Scripting

Search String and extract few lines under the searched string

Need Assistance in shell programming... I have a huge file which has multiple stations and i wanted to search particular station and extract few lines from it and the rest is not needed Bold letters are the stations . The whole file has multiple stations . Below example i wanted to search... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

3. Shell Programming and Scripting

How to Search string(which is in 2 lines) in a file?

Hello, I want to search 2 lines from the file. I wanted to display all such matches. Example file: ================== Testfile is test TEST1 TEST2 testing the file string to do testing TEST1 TEST2 sample strings ================= I wanted to search the file with 2 lines " TEST1... (3 Replies)
Discussion started by: balareddy
3 Replies

4. Shell Programming and Scripting

Search a string and replace the same string above two lines?.

I need to search this "<CardinalMPI>" string and replace the same string above two lines of the string. Nov 16, 2012 12:58:34 PM INFO: Centinel LookUp ResponseXML : <CardinalMPI> <ReasonCode></ReasonCode> <ErrorDesc></ErrorDesc> <MerchantReferenceNumber></MerchantReferenceNumber>... (4 Replies)
Discussion started by: laknar
4 Replies

5. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

6. Shell Programming and Scripting

Awk - find string, search lines below string for other strings

What's the easiest way to search a file for a specific string and then look for other instances after that? I want to search for all Virtual Hosts and print out the Server Name and Document Root (if it has that info), while discarding the rest of the info. Basically my file looks like this: ...... (6 Replies)
Discussion started by: Mbohmer
6 Replies

7. Shell Programming and Scripting

search string in a file and retrieve 10 lines including string line

Hi Guys, I am trying to write a perl script to search a string "Name" in the file "FILE" and also want to create a new file and push the searched string Name line along with 10 lines following the same. can anyone of you please let me know how to go about it ? (8 Replies)
Discussion started by: sukrish
8 Replies

8. Shell Programming and Scripting

How to use sed to search for string and Print previous two lines and current line

Hello, Can anybody help me to correct my sed syntax to find the string and print previous two lines and current line and next one line. i am using string as "testing" netstat -v | sed -n -e '/test/{x;2!p;g;$!N;p;D;}' -e h i am able to get the previous line current line next line but... (1 Reply)
Discussion started by: nmadhuhb
1 Replies

9. UNIX for Dummies Questions & Answers

Search for string and return lines above and below

Hi, I have a file with the following contents: COMPANY ABCD TRUCKER JOE SMITH TRUCK 456 PLATE A12345678 TRUCKER BILL JONES TRUCK 789 PLATE B7894561 I need to create a script or search command that will search for this string '789' in the file. This string is unique and only... (7 Replies)
Discussion started by: doza22
7 Replies

10. UNIX for Dummies Questions & Answers

How can you show lines surrounding a search string?

I would like to be able to grep (or some such thing) a search argument and then display the line plus the preceding 3 lines of the file and the following 3 lines of the file. Any ideas? Thanks in advance! :D (3 Replies)
Discussion started by: robster
3 Replies
Login or Register to Ask a Question