2

I am trying to add column with the name of the file into the end of each line. Every line has a

Name Surname   some_number

Name and surname are separated with space and number is separated from Surname by tab.

I am doing it with this script but it does not work, it only adds filename to start of the line.

$1=temp
nawk -v F'\t' -v OFS='\t' '{$2=$2" "$temp} 1' $temp

PS OS is SunOS.

erizo
  • 347
  • 1
  • 3
  • 8

2 Answers2

1

Try with:

awk '{ printf( "%s %s\n", $0, FILENAME ); }' infile
Birei
  • 35,723
  • 2
  • 77
  • 82
  • It prints only the filename for me – erizo Oct 10 '12 at 08:33
  • @erizo: This command is run from command line, not inside a shell script. In that context `$0` has a different meaning. It means the whole line. – Birei Oct 10 '12 at 08:47
  • it does not really work with my files. I have files like filename.dat. When I run this in cmd like awk '{ printf( "%s %s\n", $0, FILENAME ); }' filename.dat I get only filename in every row. – erizo Oct 10 '12 at 12:09
  • Make sure your input file doesn't end with `^M` (aka `\r` aka DOS style line feeds), since that will cause Solaris to reset back to the beginning of the line and overwrite whatever was already printed. (Normally you won't see this because it will be immediately followed by the newline that moves to the next line, but when you have awk insert the filename between the `\r` and `\n` you'll see this problem.) – alanc Oct 13 '12 at 01:28
1

awk '{print $0, FILENAME}' file

Ed Morton
  • 188,023
  • 17
  • 78
  • 185