Help in adding text before columns in shell script

Hello,

Can someone please help in below requirement.
My requirement is to add date before to first column,some text before 1st,2nd coulmns and insert a new column in between 2 and 3 columns.

input file.

aa  123    dddd
aa  667    kdkdk
ddj 738    kkkk
aa  123    dddd
aa  667    kdkdk
aa  123    dddd
aa  667    kdkdk

expecting output file:

2016-07-29 06:23:12 type1=aa type2=123 ABC dddd
2016-07-29 06:23:12 type1=aa type2=667 ABC  djdkd
2016-07-29 06:23:14 type1=ddj type2=738 ABC kkkk
2016-07-29 06:23:15 type1=aa type2=123 ABC kdkdk
2016-07-29 06:23:17 type1=aa type2=667 ABC dddd

Thank You.

What have you tried so far?

tried this

awk '{print "type1=" $1,"type2="$2,$3}'

but don't know how to add to date before 1st column and new column in 2 and 3rd.

tried this for date but not working.

DATE=$(date +"%m-%d-%Y")
awk'{$DATE,print "type1=" $1,"type2="$2,$3}'

You're not too far off. awk can't handle shell variables immediately. Did you consider awk 's -v option to convey (shell variables') values into awk variables? Then, print the awk variable holding the date value just in front of the other fields.

Your DATE is not correct, the time is missing. The new column can be just written, as you wrote any other text in your print. DATE can be handed over to awk with -v:

$ DATE=$(date +"%Y-%m-%d %H:%M:%S")
$ awk -v date="$DATE" '{print date, "type1=" $1, "type2="$2, "ABC", $3}' infile
2016-07-29 09:23:56 type1=aa type2=123 ABC dddd
2016-07-29 09:23:56 type1=aa type2=667 ABC kdkdk
2016-07-29 09:23:56 type1=ddj type2=738 ABC kkkk
2016-07-29 09:23:56 type1=aa type2=123 ABC dddd
2016-07-29 09:23:56 type1=aa type2=667 ABC kdkdk
2016-07-29 09:23:56 type1=aa type2=123 ABC dddd
2016-07-29 09:23:56 type1=aa type2=667 ABC kdkdk
1 Like

Hello Cva2568,

You could make Zaxxon's command like as follows too, where we could mention the date within awk 's variable itself.

awk -v date="$(date +"%Y-%m-%d %H:%M:%S")" '{print date, "type1=" $1, "type2="$2, "ABC", $3}' Input_file

Thanks,
R. Singh