Unix Wiki
Advertisement

Use this perl command

perl -e '{$file="test.txt";$lin=1;$sep=";"} print ++$i."\t$_$/"foreach(split/$sep/,qx[head -$lin $file|tail -1])'

Just change the 3 variables at the start of the perl code with:

  • $file the name of the file (duh)
  • $lin The row where the header is located. Most of the time is 1.
  • $sep The separator used in the file header. This is a regular expression.

Usage[]

Given a file with this text on the 1st line:

Name;Date;OpNumber;ID;Volume;Closing;Open;Min;Max;Mean

The command output will be:

> perl -e '{$file="test.txt";$lin=1;$sep=";"} print ++$i."\t$_$/"foreach(split/$sep/,qx[head -$lin $file|tail -1])'
1 Name
2 Date
3 OpNumber
4 ID
5 Volume
6 Closing
7 Open
8 Min
9 Max
10 Mean

This output could be useful when trying to filter some file with many header columns with awk. See the Obtaining lines from a text file where a column has some value article for more information.

Advertisement