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 Code:
Tue May 7 14:56:27 EST 2013 text1 Code:
$ sed -i 's/^/`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 |
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 Code:
sed -i "s/^/$(date)/" file.txt |
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 Code:
sed -i "s/^/`${var}`/g" list.txt Code:
sed -i "s/^/$var/" list.txt |
You don't need to execute variables:
Code:
$ var="$(date)" |
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 If I am not clear enough, please comment. |
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 |
Yes I am using same command but it gives me following error
Code:
sed: -e expression #1, char 6: unknown option to `s' 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 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. |
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 Bash:
Sed: |
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? |
Quote:
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)' Code:
$ var="$(pwd)" - 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) |
Thanks for the tutorial & explanation.
|
All times are GMT -5. The time now is 09:29 AM. |