Reputation: 987
the file looks like this:
>5BW0 B
CHHHHHHHHHHHHHHHHHHHHHHHCCCCCCEEEEEEEEEECCEEEEEEEEEEECCCCCCEEEEEEEEECCCCCCCHHHHCCEEEEEEEC
However the second line could be empty.
I'm trying to find the files with empty second lines.
So far I came up with this in a for loop:
cat file1 | sed -n '2p' | grep '^$'
the problem is that I'm not saving the filename so that I can print it afterwards. How can I fix this?
Upvotes: 3
Views: 229
Reputation: 58381
This might work for you (GNU sed):
sed -n '2{/\S/!F}' file
If the second line of a file does not contain a non-whitespace character, output the name of the file.
Or you may prefer:
sed '2!d;/^$/F;d' file
Upvotes: 0
Reputation: 133458
With single GNU awk
you could try following awk
program. Written and tested in GNU awk
. I have passed 2 Input_files named file1 and file2 in this program you can pass number of files as per your requirement also.
awk 'FNR==3{nextfile} FNR==2 && !NF{print FILENAME;nextfile}' file1 file2
OR as per Ed sir's recommendation above could be shorten to:
awk 'FNR==2{ if (!NF) print FILENAME; nextfile }' file1 file2
Explanation: Simply checking if its 3rd line of a file then move to next file anyways(which means 2nd line is NOT empty). Then checking 2 more conditions if line is 2nd line and is empty then print file name and move to nextfile.
NOTE: In case you need to find files which are having 2nd line as an empty and print their names then make this above code as a script.awk
code and pass it to run in find
command too.
Upvotes: 3
Reputation: 72639
What about iterating over file names and looking if the second line is empty as follows. The ;2q
part quits sed
after line 2 to avoid reading the rest of possibly many gigalines of data for nothing.
for file in file1 file2; do
if test -z "$(sed -n "2p;2q" "$file")"; then
echo "2nd line of $file is empty"
fi
done
You could also pipe a list of file names into a loop:
find . -name '*.txt' |
while read -r file; do
if test -z "$(sed -n "2p;2q" "$file")"; then
echo "2nd line of $file is empty"
fi
done
Upvotes: 1