Using files where is stored some data, I'd like read into these files and print 5 specific lines that match a pattern, in other files. But if the files are not corrects, I'd like to print 5 lines containing whatever message ("ERROR" for instance), to differentiate and to know which file is good or not.
Here is how I'm doing it:
if grep -q 'PATTERN' outputfile;
then
grep --color 'ANOTHER_PATTERN' outputfile > tmpfile
else
printf 'ERROR %.0s\n' {1..5} > tmpfile
fi
So, my question is how does the commandprintf 'ERROR %.0s\n' {1..5} really works ? The code is actually working but I'm not comfortable using commands that I don't fully understand.
I know %s is used to print strings, \nis for a newline but I don't know why we need to use the .0s. I guess it's a "trick" to print a blank space next to "ERROR" instead of the number (1,2..,5) because if I just use
%s\n {1..5} I obtain:
ERROR 1
ERROR 2
...
and so on until 5. My second question would be : How can I print 5 lines with the ERRORmessage without using this ? Thanks in advace for any tips.
ERROR %.0s. The string%.0sis a format that tells you how to print strings (s). the part.0tells how many characters of the string should be printed. In this case ZERO. Seeman printfandman 3 printf