SED - Multiple String - Single Line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED - Multiple String - Single Line
# 1  
Old 07-04-2011
SED - Multiple String - Single Line

Would appear to me to be a farily simple question but having search all the threads I can't find the answer .. I just want sed to output the single line in a file that contains two string anywhere on the line..

e.g. currently using this command
Code:
sed -n -e'/str1/p' -e '/str2/p' < file

and get ..
Code:
str1 xxxxxxxxxxxxxxxxxxxxxxxx
str1 xxxxxxxxxxxstr2xxxxxxxxx
str1 xxxxxxxxxxxstr2xxxxxxxxx
str1 xxxxxxxxxxxxxxxxxxxxxxxx
str1 xxxxxxxxxxxxxxxxxxxxxxxx

So I get the line I want ( italics ) duplicated ..

I've no doubt this is very obvious .. please enlighten me ..

Last edited by Franklin52; 07-04-2011 at 07:56 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 07-04-2011
Code:
 
awk ' /str1.*str2/ { print $0}' filename

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 07-04-2011
Spot on .. thanks mate ..
# 4  
Old 07-04-2011
Depending on the intended result :

Code:
# cat tst
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxx str1 xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxx str2 xxxxx
str1 xxxxxxxxxxxstr2xxxxxxxxx
xxxx str1 xxxxxxxxxx str2 xxx
str1 xxxxxxxxxxxxxxxxxxx str2
xxxxxxx str1 xxxxxxxxxxx str2
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
# sed '/str1.*str2/!d' tst
str1 xxxxxxxxxxxstr2xxxxxxxxx
xxxx str1 xxxxxxxxxx str2 xxx
str1 xxxxxxxxxxxxxxxxxxx str2
xxxxxxx str1 xxxxxxxxxxx str2
# awk '/str1.*str2/' tst
str1 xxxxxxxxxxxstr2xxxxxxxxx
xxxx str1 xxxxxxxxxx str2 xxx
str1 xxxxxxxxxxxxxxxxxxx str2
xxxxxxx str1 xxxxxxxxxxx str2
# awk '/^str1.*str2/' tst
str1 xxxxxxxxxxxstr2xxxxxxxxx
str1 xxxxxxxxxxxxxxxxxxx str2
#  sed '/^str1.*str2/!d' tst
str1 xxxxxxxxxxxstr2xxxxxxxxx
str1 xxxxxxxxxxxxxxxxxxx str2
#

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Splitting Single line into multiple line

Hi All, I am reading a line from a file and writing it to other file. Whenever I got a particular line then I want that line to be splited into 4 line and written it to new file. e.g My line is U_ABC connector3 pin24E connector4 pin25E connector5 pin26E connector6 pin27E connector7... (2 Replies)
Discussion started by: diehard
2 Replies

2. Shell Programming and Scripting

Find multiple strings and replace single string

Hi, following Perl code i used for finding multiple strings and replace with single string. code: #!/usr/bin/perl my @files = <*.txt>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)'; my $replaceStr =... (2 Replies)
Discussion started by: chettyravi
2 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

convert single line output to multiple line

Hi all, I have a single line output like below echo $ips 10.26.208.28 10.26.208.26 10.26.208.27 want to convert above single line output as below format. Pls advice how to do ? 10.26.208.28 10.26.208.26 10.26.208.27 Regards Kannan (6 Replies)
Discussion started by: kamauv234
6 Replies

5. Shell Programming and Scripting

grepping multiple matches in a single string

Hi All, I'm trying to grep for 3 patterns in a string of gibberish. It so happens that each line is appended by a date/time stamp and i was able to figure out how to extract only the datetime. here is the string.. i have to display tinker tailor soldier spy Please can some help... (2 Replies)
Discussion started by: Irishboy24
2 Replies

6. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

7. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

8. Shell Programming and Scripting

Putting multiple sed commands on a single line

Hi, I want to make sed write a part of fileA (first 7 lines) to file1 and the rest of fileA to file2 in a single call and single line in sed. If I do the following: sed '1,7w file1; 8,$w file2' fileA I get only one file named file1 plus all the characters following file1. If I try to use curly... (1 Reply)
Discussion started by: varelg
1 Replies

9. Shell Programming and Scripting

make multiple line containing a pattern into single line

I have the following data file. zz=aa azxc-1234 aa=aa zz=bb azxc-1234 bb=bb zz=cc azxc-1234 cc=cc zz=dd azxc-2345 dd=dd zz=ee azxc-2345 ee=ee zz=ff azxc-3456 ff=ff zz=gg azxc-4567 gg=gg zz=hh azxc-4567 hh=hh zz=ii azxc-4567 ii=ii I want to make 2nd field pattern matching multiple lines... (13 Replies)
Discussion started by: VTAWKVT
13 Replies

10. Shell Programming and Scripting

single line input to multiple line output with sed

hey gents, I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm... (8 Replies)
Discussion started by: mitch
8 Replies
Login or Register to Ask a Question