Hi Experts,
I am trying to add one variable value as the first value in a file speparated by "" (space) delimiter. Can you please let me know how I can do this using bash script.
Following is my file.
1635 ABCD 3m9ka COMPLETE
0526 AJAY 3m1da COMPLETE
0419 INDIA 3m3zi INCOMPLETE
The variable I want to add as the first column in the file is searchdate=`date "+%d/%m/%Y"`.
In Summary , currrent date should be added as the first the first column in the file.
FYI I am using SUN OS
uname -v
Generic_122300-60
Thanks,
Ajay
[LEFT]Hi Ajay,
Below is your answer by sed for date .
#!/bin/bash
searchdate=$(date +%d/%m/%Y)
sed -i "s|^| $searchdate |" myfile
[/LEFT]
While number or character you can do directly as below:
sed -i 's/$/ 1000/' myfile
sed -i 's/^/ 1000/' myfile
birei
April 23, 2012, 9:50am
3
Hi ajaypatil_am,
One way using perl:
$ cat infile
1635 ABCD 3m9ka COMPLETE
0526 AJAY 3m1da COMPLETE
0419 INDIA 3m3zi INCOMPLETE
$ perl -ane 'my ($mday,$mon,$year) = (localtime)[3..5]; $date = sprintf q[%02d/%02d/%4d], ($mday, ++$mon, $year+=1900); printf qq[%s %s\n], $date, qq[@F]' infile
23/04/2012 1635 ABCD 3m9ka COMPLETE
23/04/2012 0526 AJAY 3m1da COMPLETE
23/04/2012 0419 INDIA 3m3zi INCOMPLETE
searchdate=$(date "+%d/%m/%Y")
while read line
do
echo "$searchdate $line" >> output.txt
done < input.txt