LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-08-2012, 12:55 AM   #1
r_arun
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Rep: Reputation: 0
Append a new column to the end of the file


Hi All,

I am new to Linux. I've been trying to add a column to the end of the file. its today's date i need to append to it.

Now i have file like this

1 0.0 SUCCEEDED null null
1 0.0 SUCCEEDED null null

i need the same file with date at the last column

1 0.0 SUCCEEDED null null 2012-02-08
1 0.0 SUCCEEDED null null 2012-02-08

I tried the following thing, but it doesn't get reflected in the file
sed "s:$:\t$(date +%Y-%m-%d):" DW.txt

Please hlep me out.

Thanks,
Arun
 
Old 02-08-2012, 01:03 AM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 210Reputation: 210Reputation: 210
I think what you have above should work, if you pass a -i to sed.

Or, do this:

Code:
while read line; do echo $line $(date "+%Y-%m-%d"); done < DW.txt
 
1 members found this post helpful.
Old 02-08-2012, 01:09 AM   #3
r_arun
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by jhwilliams View Post
I think what you have above should work, if you pass a -i to sed.

Or, do this:

Code:
while read line; do echo $line $(date "+%Y-%m-%d"); done < DW.txt
Thanks Williams.

sed -i "s:$:\t$(date +%Y-%m-%d):" DW_1328676975699.txt
This worked!!

Thanks a ton!!

Regards,
Arun
 
Old 02-08-2012, 10:09 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by r_arun View Post
I tried the following thing, but it doesn't get reflected in the file
sed "s:$:\t$(date +%Y-%m-%d):" DW.txt
It doesn't get reflected because sed, like most commands, doesn't alter the original text by default. It only prints the modified contents to stdout.

The traditional way to handle it is simply to redirect the output to a temporary file, then replace the original with it. (Note that you can't generally use the same file for both input and output.)

gnu sed also has the "-i" option for editing the file in place. Basically it does all the tempfile work for you transparently behind the scenes.
 
1 members found this post helpful.
Old 02-09-2012, 10:13 AM   #5
r_arun
LQ Newbie
 
Registered: Jul 2010
Posts: 3

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by David the H. View Post
It doesn't get reflected because sed, like most commands, doesn't alter the original text by default. It only prints the modified contents to stdout.

The traditional way to handle it is simply to redirect the output to a temporary file, then replace the original with it. (Note that you can't generally use the same file for both input and output.)

gnu sed also has the "-i" option for editing the file in place. Basically it does all the tempfile work for you transparently behind the scenes.
Thanks for the clear explanation David. It helped me a lot.

Regards,
Arun
 
  


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
bash programming-append single line to end of file pheasand Linux - General 4 02-28-2014 10:41 AM
[SOLVED] bash awk column to end of file alex1986 Programming 8 09-04-2010 07:04 PM
append to end of each file -- -bash: *: ambiguous redirect allele Linux - Software 7 06-03-2009 02:57 PM
Append string to end of file name chellemybelle Linux - Newbie 4 11-26-2007 08:17 PM
how to append columns form a column file in another file adam_blackice Programming 4 09-18-2007 12:33 AM


All times are GMT -5. The time now is 12:54 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