Returning two lines if they both match strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Returning two lines if they both match strings
# 1  
Old 10-18-2013
Returning two lines if they both match strings

Hi

I have a problem where I have a large amount of files that I need to scan and return a line and its following line, but only when the following line begins with a string.

String one - line one must begin with 'Bill'
String two - line two must begin with 'Jones'.

If these two criteria are matched, it returns the two lines. Repeat for the whole file.

ie. original file:

Code:
Edith Blue
Edith Green
Edith Red
Bill Blue
Jones Red
Edith Green
Bill Green
Edith Red
Jones Green
Bill Blue


I'd want it to return only:

Code:
Bill Blue
Jones Red

Any ideas? No idea where to begin with this, I only have basic scripting skills with sed/awk etc... At the moment I am using this to get the filename and its following line, but it is giving me too much useless information that I have to strip off with other sed commands.

Code:
grep -A 1 "^Bill" * > test.txt

I guess there's a far more elegant way of getting only the lines I need. Any help would be lovely!
# 2  
Old 10-18-2013
Code:
$ awk '{if ((lastword=="Bill") && ($1=="Jones")) {print lastline ORS $0} lastword=$1; lastline=$0}' file
Bill Blue
Jones Red

EDIT: Actually, slightly neater (and conforming more closely to your requirements):
Code:
awk '{if ((lastline ~ /^Bill/) && ($0 ~ /^Jones/)) {print lastline ORS $0} lastline=$0}' file

# 3  
Old 10-18-2013
sed:
Code:
sed -n 'N;/^Bill.*\nJones/p;D' file

# 4  
Old 10-18-2013
beautiful, thankyou.

two quick further questions, if you don't mind Smilie

1 - how do i run awk recursively on a directory? i have 100s of files in a directory which i need to run this on.
2 - i also need it to dump the filename at the beginning of each line.

grep was handy with this in that the -A flag dumps the filename with the output. not sure with awk...

thanks a million though!

Last edited by majormajormajor; 10-18-2013 at 02:11 PM..
# 5  
Old 10-18-2013
Code:
print FILENAME ":\n" lastline ORS $0

If all the files are in the same directory you can just pass multiple filenames to awk. If they're nested under sub-directories (or the filename list is just too long) you can use find (& possibly xargs), e.g.
Code:
find /home/someuser/adir -name "something.*" -exec awk '{stuff}' {} \;

or
Code:
find /home/someuser/adir -name "something.*" | xargs awk '{stuff}'

# 6  
Old 10-18-2013
apologies if i'm being a nuisance, but i'm a beginner and not really following.

do you mean find the files and pipe it to awk?
ie. this?

Code:
fine -name "*" | awk '{if ((lastword=="Bill") && ($1=="Jones")) {print lastline ORS $0} lastword=$1; lastline=$0}'

doesn't seem to work, i get:
Code:
 ;awk: read error (Is a directory);

i think i posted a confused question to start. as there are about 200 files for it to look through to return the matches, i need it to paste the filename at the beginning of each line, so say it finds a match in the filename 'test2.txt', the return i'd like would be:

Code:
test2.txt;Bill Blue
test2.txt;Jones Red

again, sorry to be a pest.
# 7  
Old 10-18-2013
Code:
find . -type f | xargs awk '{if ((lastline ~ /^Bill/) && ($0 ~ /^Jones/)) {print FILENAME ";" lastline ORS FILENAME ";" $0} lastline=$0}'

On my previous solutions I omitted -type f, which restricts find to just regular files.

EDIT: To clarify, what I meant by 'pass multiple filenames to awk' is just to specify them on the command line, e.g. awk 'stuff' *. However, if you have directories under where you're running the command and your files don't have an easily-globbed set of names (like *.txt) then it's better to use find.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

2. Shell Programming and Scripting

Script to match strings that sometimes are splitted in 2 lines

Hello to all, I have an hexdump -C format as below: 31 54 47 55 48 4c 52 31 5f 52 31 32 31 31 32 ff 44 00 00 0E 01 32 14 56 42 17 47 48 0f ff ff ff 44 00 00 01 32 14 56 00 23 83 95 2f 42 17 47 48 00 0f ff ff 00 15 00 0a 48 00 01 5a 00 02 17 00 00 2f 00 00 30 00 00 31 00 00 ff 34 ff 44 00... (23 Replies)
Discussion started by: Ophiuchus
23 Replies

3. Shell Programming and Scripting

Print only lines where fields concatenated match strings

Hello everyone, Maybe somebody could help me with an awk script. I have this input (field separator is comma ","): 547894982,M|N|J,U|Q|P,98,101,0,1,1 234900027,M|N|J,U|Q|P,98,101,0,1,1 234900023,M|N|J,U|Q|P,98,54,3,1,1 234900028,M|H|J,S|Q|P,98,101,0,1,1 234900030,M|N|J,U|F|P,98,101,0,1,1... (2 Replies)
Discussion started by: Ophiuchus
2 Replies

4. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

5. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

6. Shell Programming and Scripting

Strings from one file which exactly match to the 1st column of other file and then print lines.

Hi, I have two files. 1st file has 1 column (huge file containing ~19200000 lines) and 2nd file has 2 columns (small file containing ~6000 lines). ################################# huge_file.txt a a ab b ################################## small_file.txt a 1.5 b 2.5 ab ... (4 Replies)
Discussion started by: AshwaniSharma09
4 Replies

7. Shell Programming and Scripting

Shell script: returning the file with the most lines

Hey I am relatively new to Linux and shell scripting, looking for a spot of help with a script I am working on. I am writing a script that counts the number of lines in all the files in a directory, sorts them by line number and then returns ONLY the file with the most lines. Right now I can... (11 Replies)
Discussion started by: Breakology
11 Replies

8. Programming

Returning Strings from C program to Unix shell script

Hi, I'm having a requirement where I need to call a C program from a shell script and return the value from the C program to shell script. I refered a thread in this forum. But using that command in the code, it is throwing an error clear_text_password=$(get_password) Error: bash:... (24 Replies)
Discussion started by: venkatesh_sasi
24 Replies

9. Shell Programming and Scripting

Returning filename and matching lines

What I'm trying to do is to search through a list of files, and output the filename, followed by the lines that matched the pattern. I'm matching the string "letters.moreletters" in any one of searched files, and the output I'm trying to get is: program_1.txt 10 dsdsd sdsd dsd... (2 Replies)
Discussion started by: smb_uk
2 Replies

10. Programming

Returning Strings from C program to Unix shell script

Hi, Iam calling a C program from a Unix shell script. The (C) program reads encrypted username/password from a text file , decrypts and returns the decrypted string. Is there any way i can return the decrypted string to Unix shell program. My shell script uses the output of the program to... (11 Replies)
Discussion started by: satguyz
11 Replies
Login or Register to Ask a Question