LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Add a column to a file. (https://www.linuxquestions.org/questions/linux-newbie-8/add-a-column-to-a-file-915183/)

Sha_unix 11-23-2011 03:30 PM

Add a column to a file.
 
Hi i have file which looks like below.

I want add 5th column as "command date" ie., which shows today date.

Code:

:
AAAA;BBBB;CCCC;DDDDD
NNNNN;SSSSS;EEEEE;QQQQ

i tried below didnt work

awk '{print $1";"$2";"$3";"$4";"$5=`date`}' <file1>

can anybody help.

colucix 11-23-2011 03:34 PM

Why not sed?
Code:

sed -i.bck "s/$/;$(date)/" file
In awk you have to use getline into a Variable from a Pipe, e.g.
Code:

awk '{ "date" | getline current_time
      close("date")
      printf "%s;%s\n", $0, current_time > FILENAME }' file


Sha_unix 11-23-2011 03:37 PM

Its showing below error. :(

Code:

sed: illegal option -- i

colucix 11-23-2011 03:49 PM

Quote:

Originally Posted by Sha_unix (Post 4532088)
Its showing below error. :(

Code:

sed: illegal option -- i

This means you're not running GNU sed. Well, since the -i option serves only to edit the file in place, you can avoid it and use the good ol' method of temporary files:
Code:

sed "s/$/;$(date)/" file > /tmp/file.new
mv /tmp/file.new file

Please, consider to update your LQ profile including the OS you're running, so that you can receive a faster and suitable help. Is this Solaris, anyway?

Sha_unix 11-23-2011 03:50 PM

Code:

awk '{ "date" | getline current_time
      close("date")
      printf "%s;%s\n", $0, current_time > FILENAME }' file

Can you please explain me what the above commands doing.

colucix 11-23-2011 03:55 PM

Quote:

Originally Posted by Sha_unix (Post 4532103)
Code:

awk '{ "date" | getline current_time
      close("date")
      printf "%s;%s\n", $0, current_time > FILENAME }' file

Can you please explain me what the above commands doing.

It does exactly what you were trying to achieve in your awk code. But it is the correct way to do. You used a shell-like command substitution with backticks, instead the awk's "command substitution" actually uses getline in the form:
Code:

"shell command here" | getline varname
so that the output of the shell command will be stored in varname which can be used later inside awk. It's all explained in the link I posted above.

However, this is a GNU awk extension. If you're not running it on a Linux machine, most likely it will fail. Better the sed solution without the -i option.

Sha_unix 11-23-2011 03:56 PM

Oh Thank you very much.. below command worked.

Code:

sed "s/$/;$(date)/"

If you dont mind, i tried below command to get date in required format. didnt work.

Code:

sed "s/$/;$(date "+%d/%m/%Y")/"

colucix 11-23-2011 04:02 PM

Quote:

Originally Posted by Sha_unix (Post 4532112)
If you dont mind, i tried below command to get date in required format. didnt work.

Code:

sed "s/$/;$(date "+%d/%m/%Y")/"

Always consider the quotes in pair: the problem is the first double quote before + closes the quote at the beginning, the other one opens a new pair of quote together with the ending one. Moreover the replacement string (that is the shell command) contains slashes: this confuses sed, that doesn't understand where the / delimiter really is. In this case you can avoid the double quotes to embed the date format and you can use another delimiter for the s command:
Code:

sed "s:$:;$(date +%d/%m/%Y):" file

Sha_unix 11-23-2011 04:05 PM

Awesome... you made my day... :) Thank you very much once again.

colucix 11-23-2011 04:08 PM

You're welcome! :)


All times are GMT -5. The time now is 06:01 PM.