LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-23-2011, 04:30 PM   #1
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Rep: Reputation: Disabled
Question 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.
 
Old 11-23-2011, 04:34 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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

Last edited by colucix; 11-23-2011 at 04:37 PM.
 
Old 11-23-2011, 04:37 PM   #3
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
Its showing below error.

Code:
sed: illegal option -- i
 
Old 11-23-2011, 04:49 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by Sha_unix View Post
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?
 
Old 11-23-2011, 04:50 PM   #5
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-23-2011, 04:55 PM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by Sha_unix View Post
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.
 
Old 11-23-2011, 04:56 PM   #7
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
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")/"
 
Old 11-23-2011, 05:02 PM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by Sha_unix View Post
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
 
1 members found this post helpful.
Old 11-23-2011, 05:05 PM   #9
Sha_unix
Member
 
Registered: Sep 2011
Posts: 46

Original Poster
Rep: Reputation: Disabled
Awesome... you made my day... Thank you very much once again.
 
Old 11-23-2011, 05:08 PM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
You're welcome!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Need to add a column to a text file Thaidog Programming 2 04-28-2011 10:09 AM
[SOLVED] how to add blank lines whenever the value of a given column alters in a data file? vcmota Programming 9 01-11-2011 09:47 PM
Read text file column by column RVF16 Programming 11 05-31-2009 08:16 AM
add id in the first column alaios Linux - General 1 11-19-2004 07:49 AM
add new column Eddie9 Linux - General 2 04-09-2002 01:05 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration