Using FNR
and NR
in awk
#!/bin/bash
outfile="$( mktemp combined.txt.XXXXXX )"
echo "Output file: ${outfile}"
awk 'FNR==1 && NR>1 { printf("\n%s\n\n","========") } 1' *.txt > "${outfile}"
echo "Finished."
A line-by-line description:
outfile="$( mktemp combined.txt.XXXXXX )"
Use mktemp
to create an empty new file with a unique name (eg, combined.txt.HDpgMn
). You can use more X
characters for a longer random suffix. Enclose the command in "$(
...)"
to store the new file's name in the variable outfile
.
echo "Saving to file: ${outfile}"
Print the name of the output file. (When the script has finished, you may wish to rename the output file to remove the string of random characters following the .txt
.)
awk 'FNR==1 && NR>1 { printf("\n%s\n\n","========") } 1' *.txt > "${outfile}"
Print...
- a blank line,
- a short line of "=" characters,
- and another blank line
...at the start of each input file, except for the first input file. FNR
counts the input file line numbers, resetting at the start of each file. NR
counts the line numbers and does not reset.
In the awk
statement, the 1
just before the closing single quotation mark evaluates to TRUE
for every line, and performs the default action of printing that line. (In other words, awk '1'
works like cat
.)
echo "Finished."
Inform the user when the script is done. (Not strictly necessary, since you'll see the command prompt anyway, but it doesn't hurt.)