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:
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:
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.
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!
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...
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.
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:
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.