Operating System - HP-UX
1833832 Members
1885 Online
110063 Solutions
New Discussion

Inserting a column with awk

 
SOLVED
Go to solution

Inserting a column with awk

Hello everybody,
I want to insert a column in a .txt file, and I been using the command awk '{print "test" ,$0}' file.txt
the problem is that "test" is not going to be fix anymore, it is a variable now... so, if I use the command awk '{print $test ,$0}' file.txt it doesn´t work in the way i want..
Thanks for your help.
2 REPLIES 2
Hein van den Heuvel
Honored Contributor
Solution

Re: Inserting a column with awk

So make it a variable.

Check out : man awk
Look for -v

awk -v text=$TEXT '{print text, $0}' file .ext

(drop the comma if you need no space)

Also read up on the ENVIRON array in that man page.

Finally, you can use straigh shell substitution, but the escaping of the rest gets tricky:

$ export TEXT=test
$ awk "{print \"$TEXT\", \$0}" file.ext

hth,
Hein




Re: Inserting a column with awk

Thanks. Answered so quickly.