Is there a simple way to add in header & trailer to an existing text file? Same directory same text file. For example the awk ?
Appreciate some help on the explanation on how to process it with awk. Thanks.

Cause what I do know consist quite some steps. Please refer to below:

Suppose I wanted to add header to trailer to an existing file ALERT.txt
The file is in the directory called $Source_Dir

The text file contains column name, so I have two variable Record_Count & Actual_Record_Count to count the record:

Record_Count=`wc -l < ${Source_Dir}/${Output_File_Name} | tr -d " "`
Actual_Record_Count=`expr ${Record_Count} - 1`

First, I create a text file called ALERT with the header to $Target_Dir

echo ${Bus_date} >${Target_Dir}/${Output_File_Name}

Then, I add in the Alert.txt from $Source_Dir to the new file created in $Target_Dir

cat ${Source_Dir}/${Output_File_Name} >> ${Target_Dir}/${Output_File_Name}

Lastly, I continue adding in the trailer to the file Alert.txt in $Target_Dir.
The trailer will contains the record count

echo "EOF${Actual_Record_Count}" >>${Target_Dir}/${Output_File_Name}
  • 4
    printf '%s\n%s\n%s\n' "HEADER" "$(<alert.txt)" "FOOTER" >alert.txt.tmp && mv alert.txt.tmp alert.txt ? – heemayl Mar 15 '17 at 4:01
  • 2
    { echo "HEADER"; cat alert.txt; echo "FOOTER"; } > tmp && mv tmp alert.txt – user218374 Mar 15 '17 at 6:14
  • 1
    Your instinct to use awk seems good to me: you can probably avoid the wc -l and simply use awk's record count NR in an END block e.g. awk -v bus_date="${BUS_DATE}" 'BEGIN{print bus_date} {print} END{printf "EOF%d\n", NR}' file. If BUS_DATE really is just a datestamp, you may be able to generate that internally as well (at least with GNU awk). – steeldriver Mar 15 '17 at 12:44
  • 1
    I’d probably use ed(1) to avoid the temporary file but @RakeshSharma’s solution is decent too (heemay!’s has massive overhead for larger files). – mirabilos Mar 15 '17 at 19:59
  • 1
    see also How to add a header and/or footer – don_crissti Mar 17 '17 at 18:36

So, you have some shell variable, $Bus_date that you want to add on a line by itself as the header. Then there should follow the contents of $Source_Dir/$Output_File_Name, followed by the string EOFNN where NN is the number of lines in that file minus one.

Using awk:

awk -v head="$Bus_date" 'BEGIN { print head } { print } END { print "EOF" NR-1 }' "$Source_Dir/$Output_File_Name" >"$Target_Dir/$Output_File_Name"

The script is:

BEGIN { print head }
      { print }
END   { print "EOF" NR-1 }

On the command line, we set the awk variable head to the value of $Bus_date. This value is outputted before we start reading from the given file, by the BEGIN block.

The { print } block ensures that the given file is passed through as is, without modification.

The END block uses the built-in variable NR which holds the number of records (lines) read so far. In the END block, NR will be the number of lines in the file that was read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.