LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 05-07-2013, 04:32 AM   #1
bloodstreetboy
Member
 
Registered: May 2012
Posts: 198
Blog Entries: 3

Rep: Reputation: 37
Add today's date in every line of the file


I want to add today's date in every line of the file where line starts.
Example :
Code:
file contains
text1
text2
text3
Now it should be
Code:
Tue May  7 14:56:27 EST 2013 text1
Tue May  7 14:56:27 EST 2013 text2
Tue May  7 14:56:27 EST 2013 text3
I am using following commands but it is not working
Code:
$ sed -i 's/^/`date`/g' file.txt
$ sed -i 's/^/echo `date`/g' file.txt
&
suppose date is stored in a variable then how to add date in every line of file using variable.
I am using following commands but it is not working
Code:
$ sed -i 's/^/`$var`/g' file.txt
$ sed -i 's/^/echo `$var`/g' file.txt
$ sed -i 's/^/`${var}`/g' file.txt
$ sed -i 's/^/echo `${var}`/g' file.txt
$ sed -i 's/^/`$(var)`/g' file.txt
$ sed -i 's/^/echo `$(var)`/g' file.txt
Please help.
 
Old 05-07-2013, 04:42 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
You are using single quotes, which prevent the shell from executing/expanding the date command.

Give this a try (double quotes instead of single):
Code:
sed -i "s/^/`date`/" file.txt
Or even better:
Code:
sed -i "s/^/$(date)/" file.txt
The same is true for using variables inside a sed statement.
 
1 members found this post helpful.
Old 05-07-2013, 05:03 AM   #3
bloodstreetboy
Member
 
Registered: May 2012
Posts: 198
Blog Entries: 3

Original Poster
Rep: Reputation: 37
Thanks for your answer, it is working for commands but it is not working for variables.
suppose
var=$(date)
now if I run
echo $var
The output is today's date, it is perfect that's what I want but if I use $var in my script, it does not add date in the file.
If I use
Code:
sed -i "s/^/`$var`/g" list.txt
It says
Tue: command not found
If I use
Code:
sed -i "s/^/`${var}`/g" list.txt
It says
Tue: command not found
If I use
Code:
sed -i "s/^/$var/" list.txt
It says
sed: -e expression #1, char 6: unknown option to `s'
I don't know, where I am wrong.
 
Old 05-07-2013, 05:05 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
You don't need to execute variables:
Code:
$ var="$(date)"
$ echo $var
di mei 7 12:04:02 CEST 2013
$ sed "s/^/$var /" Schuur/infile    # $var instead of `$var`
di mei  7 12:04:02 CEST 2013 text1
di mei  7 12:04:02 CEST 2013 text2
di mei  7 12:04:02 CEST 2013 text3

Last edited by druuna; 05-07-2013 at 05:06 AM. Reason: added space
 
Old 05-07-2013, 05:19 AM   #5
bloodstreetboy
Member
 
Registered: May 2012
Posts: 198
Blog Entries: 3

Original Poster
Rep: Reputation: 37
Thanks again, you almost nailed it but last question
Suppose it is about command pwd and the path is stored hard coded instead of using command, I mean
var=$(pwd)
if I run
echo $var
it prints output of the pwd command that is /home/user/Desktop/documents
But if the path is stored hard coded in the script like
var=/home/user/Desktop/documents/
Now if I run
echo $var
it prints /home/user/Desktop/documents/, it is perfect that's what I want but if I use $var in my script, it does not add the path in the file.
I am using
Code:
$ sed "s/^/$var /g" list.txt
but it does add the path at start of the every line in the file.
If I am not clear enough, please comment.
 
Old 05-07-2013, 05:39 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
I'm not sure I understand you.

Are you talking about the list.txt not changing? If so, use sed's -i option:
Code:
$ sed -i.bak "s/^/$var /" list.txt
This will make a backup of the original file (with .bak added) and make the changes in list.txt.
 
Old 05-07-2013, 05:47 AM   #7
bloodstreetboy
Member
 
Registered: May 2012
Posts: 198
Blog Entries: 3

Original Poster
Rep: Reputation: 37
Yes I am using same command but it gives me following error
Code:
sed: -e expression #1, char 6: unknown option to `s'
Ok I am trying to explain it in short.
var=/home/user/Desktop/documents/
I want to add /home/user/Desktop/documents/ at start of the every line in the file, one method if I use
Code:
sed -i 's/^/\/home\/user\/Desktop\/documents\//g' file.txt
It works fine but for long paths where directory name is 40 characters with special characters ,it is irritating and time consuming(I don't want to search and use backspace behind every special character).
That's why I have stored the path in variable, now I want to add this path at start of every line in the file using variable.
 
Old 05-07-2013, 05:53 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Ok, I get what you are running into: The variable also has forward slashes.

You can change the separator that sed uses, which will make sure you do not need to escape the forward slashes in the variable.

This example uses % as separator instead of /:
Code:
sed "s%^%$var %" infile
EDIT: Maybe these links will help:
Bash:
Sed:

Last edited by druuna; 05-07-2013 at 05:58 AM.
 
1 members found this post helpful.
Old 05-07-2013, 06:04 AM   #9
bloodstreetboy
Member
 
Registered: May 2012
Posts: 198
Blog Entries: 3

Original Poster
Rep: Reputation: 37
Smooth.. like a butter...
Man, You are Linux... because you know everything about yourself.

Can you please explain what did you do? Why did you use percentage instead of forward slashes?
& what is the significance of single quote(') & double quote ("), what mistake was I doing in my date with sed?
 
Old 05-07-2013, 06:24 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Quote:
Originally Posted by bloodstreetboy View Post
Can you please explain what did you do? Why did you use percentage instead of forward slashes?
& what is the significance of single quote(') & double quote ("), what mistake was I doing in my date with sed?
The problems you ran into are not related.

Before a command is run, the shell (bash in most cases) will try to interpret the complete line. Bash make a distinction when seeing double or single quotes. If single quotes are used then bash does not try to expand/execute the content between the single quotes:
Code:
$ var='$(pwd)'
$ echo $var
$(pwd)
If bash sees double quotes it will expand/execute before the command is executed:
Code:
$ var="$(pwd)"
$ echo $var
/home/druuna
Also have a look here:
- Bash Scripting Introduction HOWTO - 3. Using Quotes


About sed and the forward slashes as separator: You can use any separator you want, but do keep in mind that if the separator itself is present in the search and/or replace string you need to escape it.

I used % because that was not present in the string, I could also have used | or # or .......
Also have a look here:
- sed & awk - 5.3 Substitution (especially the remarks about the delimiter)
 
2 members found this post helpful.
Old 05-07-2013, 06:29 AM   #11
bloodstreetboy
Member
 
Registered: May 2012
Posts: 198
Blog Entries: 3

Original Poster
Rep: Reputation: 37
Thanks for the tutorial & explanation.
 
  


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] IF Statement that compares today's date with a date entered Icewolf0927 Programming 4 08-13-2011 10:21 PM
How can I read a file line by line and add it to a loop in another file? astroumut Linux - Newbie 7 08-24-2009 04:37 AM
script help - pull date format out of file name and compare to today MaureenT Linux - General 5 11-06-2008 11:14 AM
Add a timestamp to photos by reading the file date...? FSHero Linux - Software 4 09-14-2007 12:15 AM
Bash: add date to output line blizunt7 Programming 7 11-10-2004 03:45 AM


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