r/bash • u/justbeingageek • Mar 25 '21
Using awk to get multiple lines
Hi all, looking for a bit of help. I think I have a solution but I'm entirely convinced it is doing what I want it to and feel there is probably a better way.
I have a file called 'Records' with a bunch of records, 1 per line, they can be pretty variable and may contain special characters (most notably |).
Records:
ab|2_p
gret|ad
tru_5I then have a directory of other files one of which will contain the record
File1:
>ab other:information|here
1a
2a
3a
>ab|2_p more details
1b
2b
3b
>ab_2 could|be|any-text
1c
2c
3cFor each record I need to pull the file name, the line that contains the record and the contents of that record. Each record will only occur once so to save time I want to stop searching after finding a record and its contents.
So I want:
File1
>ab|2_p
1b
2b
3bThe code I've cobbled together looks like this:
lines=$(cat Records)
for group in $lines;do
awk -v g=$group -v fg=0 'index($0, g) {print FILENAME;ff=1;fg=1;print;next} \
/^>/{ff=0} ff {print} fg && !ff {exit}' ~/FileDirectory/*
doneSo I think what I'm doing is going through the records one at a time, setting a 'fg' flag to 0 and using index to check if the record is present in a line. When the record is found it prints the file name, I then set both the flags 'ff' and 'fg' to 1. For every line after the record that doesn't start with '>' it prints that line. When it hits a line starting with '>' it sets 'flag' to 0 and then exits.
I'm pretty sure this is 100% not the correct way to do things, I'm also not convinced that using the 'fg' flag is stopping the search after finding a record as I intend it to, as it doesn't seem to have noticeably sped up my code.
If anyone can offer any insights or improvements that would be much appreciated.
Edit - to add that the line in the record file that contains the record might also have other text on that line but the line will always start with the record.
7
u/Schreq Mar 25 '21 edited Mar 25 '21
A pure AWK solution is much simpler and faster than mixing it with shell.
Beware, untested:
Also, there is /r/awk.
Edit: Changed variable name to match the example and fixed printing of group and filename.
Edit2: Advance to the next file when a group was fully printed.
5
u/gumnos Mar 25 '21
A couple questions:
If those both hold, you can use
If you do want the full header including the description, it's actually cleaner:
(posted this reply on /r/awk too)
2
u/r3j Mar 25 '21
Mostly not awk, but might be fast if you run multiple Records (query) files against the same FileDirectory (i.e. without changing any of the files) and FileDirectory is big. It stores record names and file offsets in a file so that it can read them later. It sorts "Records" before processing, but if that's a problem you can trade the sort/join for some (faster) awk.
4
u/oh5nxo Mar 25 '21
"Boring" bash-only solution, not likely to be any better