LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Add today's date in every line of the file (https://www.linuxquestions.org/questions/linux-newbie-8/add-todays-date-in-every-line-of-the-file-4175460991/)

bloodstreetboy 05-07-2013 04:32 AM

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.

druuna 05-07-2013 04:42 AM

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.

bloodstreetboy 05-07-2013 05:03 AM

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.

druuna 05-07-2013 05:05 AM

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


bloodstreetboy 05-07-2013 05:19 AM

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.

druuna 05-07-2013 05:39 AM

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.

bloodstreetboy 05-07-2013 05:47 AM

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.

druuna 05-07-2013 05:53 AM

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:

bloodstreetboy 05-07-2013 06:04 AM

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?

druuna 05-07-2013 06:24 AM

Quote:

Originally Posted by bloodstreetboy (Post 4946521)
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)

bloodstreetboy 05-07-2013 06:29 AM

Thanks for the tutorial & explanation.


All times are GMT -5. The time now is 09:29 AM.