user3670277
user3670277

Reputation: 41

awk — getting minus instead of FILENAME

I am trying to add the filename to the end of each line as a new field. It works except instead of getting the filename I get -.

Base file:

070323111|Hudson

What I want:

070323111|Hudson|20150106.csv

What I get:

070323111|Hudson|-

This is my code:

mv $1 $1.bak
cat $1.bak | awk '{print $0 "|" FILENAME}' > $1

Upvotes: 1

Views: 54

Answers (1)

fedorqui
fedorqui

Reputation: 290025

- is the way to present the filename when there is not such info. Since your are doing cat $1.bak | awk ..., awk is not reading from a file but from stdin.

Instead, just do:

awk '...' file

in your case:

awk '{print $0 "|" FILENAME}' $1.bak > $1

From man awk:

FILENAME

The name of the current input file. If no files are specified on the command line, the value of FILENAME is “-”. However, FILENAME is undefined inside the BEGIN rule (unless set by getline).

Upvotes: 5

Related Questions