How can I add the current date or time to end of each line in file?
13,505
Solution 1
How about :
cat filename | sed "s/$/ `date`/"
Solution 2
The problem with this
awk -v v1=$var ' { printf("%s,%s\n", $0, v1) } ' data.txt > data.txt
is that the >
redirection happens first, and the shell truncates the file. Only then does the shell exec awk, which then reads an empty file.
Choose one of these:
sed -i "s/\$/ $var/" data.txt
awk -v "date=$var" '{print $0, date}' data.txt > tmpfile && mv tmpfile data.txt
However, does your $var
contain slashes (such as "10/04/2011 12:34") ? If yes, then choose a different delimiter for sed's s///
command: sed -i "s@\$@ $var@" data.txt
Author by
MRTim2day
Updated on June 05, 2022Comments
-
MRTim2day almost 2 years
I have a file called
data.txt
.I want to add the current date, or time, or both to the beginning or end of each line.
I have tried this:
awk -v v1=$var ' { printf("%s,%s\n", $0, v1) } ' data.txt > data.txt
I have tried this:
sed "s/$/,$var/" data.txt
Nothing works.
Can someone help me out here?
-
fedorqui over 10 yearsGood one, I guess you can even use
awk -v date="$(date)" '$0=$0date' file
. -
s3n0 about 4 yearsOr use
cat filename | sed "s/^/`date` /"
to add a timestamp to the beginning of each line.