Formating problem in bash while loop
file format get changed using while loop.
samplefile Code:
Code:
30-08-2011 00:00:12 PV6 80 68 13 15 3160 while read line do echo -e `date "+%d-%m-%Y" -d '1 day ago'` $line >> $mpath done < $samplefile Output comming like this. 30-08-2011 00:00:12 PV6 80 68 13 15 3160 30-08-2011 00:00:21 DT3 12 226 4 31 6248 30-08-2011 00:00:26 VI1 49 11 8 25 1525 30-08-2011 00:00:35 CX6 65 552 13 14 3168 30-08-2011 00:00:39 PV5 76 89 16 32 2929 I don't want tab format get disturb, it will give problem while uploading data to database. |
Quote:
|
Perhaps you are referring to the tab that you did not place after the date?
|
See the single & two, three digit numbers are not in straight line. It should be aligned.
Code:
30-08-2011 00:00:12 PV6 80 68 13 15 3160 |
If you use [code][/code] tags we will be able to see the formatting issue you are referring to.
Having looked a little further your issue is related to a lack of quoting to preserve the format. |
-----------------------
is this possible ? while read line do echo -e `date "+%d-%m-%Y" -d '1 day ago'` $line >> $mpath done < $samplefile Can we split the $line with delimiters like ‘,’ ?? how ? Example Code:
30-08-2011, 00:00:12, PV6, 80, 68, 13, 15, 3160 |
I have done simply this.
awk -v ft=`date "+%d-%m-%Y" -d '1 day ago'` '{print ft,",",$1,",",$2,",",$3,",",$4,",",$5,",",$6,",",$7}' $fpname Code:
30-08-2011, 00:00:12, PV6, 80, 68, 13, 15, 3160 |
Well personally I thought putting your original echo in quotes would have solved the whole problem.
Then my next question would be why do you care about splitting on comma? You don't split on the tabs previously. You just want to grab the whole line and put a comma after the new entry, just like putting a tab in the previous version. Sheesh ... why abuse awk this way ... if you are printing the entire line then use the entire line ($0). Plus awk also has a date builtin manipulator: Code:
awk '{print strftime("%d-%m-%Y,",systime()-86400)$0}' $fpname |
Thanks grail.
in my file some fields are not aligned (Zero), due to this comma. using this : awk '{print strftime("%d-%m-%Y,",systime()-86400)$0}' $fpname output: Code:
31-08-2011,23:59:33 VI1 9 142 9 17 7062 awk -v ft=`date "+%d-%m-%Y" -d '1 day ago'` ' {OFS=",";} {print ft,$1,$2,$3,$4,$5,$6,$7}' $fpname Code:
31-08-2011,23:59:33,VI1,9,142,9,17,7062 |
Well from post #6 you implied the file was already comma separated, but I am guessing that is not the case and it is still tab separated ... yes?
So, in awk: Code:
awk '{$0 = strftime("%d-%m-%Y ",systime()-86400)$0}$1=$1' OFS="\t" $fpname Code:
while read line Code:
line="${line// /,}" |
All times are GMT -5. The time now is 07:34 AM. |