LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Formating problem in bash while loop (http://www.linuxquestions.org/questions/linux-newbie-8/formating-problem-in-bash-while-loop-900397/)

k_balaa 08-31-2011 07:08 AM

Formating problem in bash while loop
 
file format get changed using while loop.

samplefile
Code:


00:00:12 PV6 80  68 13 15 3160
00:00:21 DT3 12  226  4 31 6248
00:00:26 VI1  49  11  8 25 1525
00:00:35 CX6  65 552 13 14 3168
00:00:39 PV5  76  89 16 32 2929

I want it to be
Code:

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

am using

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.

hi2arun 08-31-2011 07:26 AM

Quote:

I want it to be

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


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
what is not happening? Where do you see 'tab' conflicts in your output?

grail 08-31-2011 08:26 AM

Perhaps you are referring to the tab that you did not place after the date?

k_balaa 08-31-2011 09:22 PM

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
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


grail 08-31-2011 09:54 PM

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.

k_balaa 09-01-2011 12:18 AM

-----------------------

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
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


k_balaa 09-01-2011 03:28 AM

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
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

now it's ok for me,i can upload the data with this delimiter....

grail 09-01-2011 04:17 AM

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

k_balaa 09-01-2011 08:44 AM

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
31-08-2011,23:59:36 VI2  78    2 15 15 3789
31-08-2011,23:59:52 VI2  67    2        0  0            0
31-08-2011,23:59:56 VI1  31    6  1 31 4011

i have done like this :
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
31-08-2011,23:59:36,VI2,78,2,15,15,3789
31-08-2011,23:59:52,VI2,67,2,0,0,0
31-08-2011,23:59:56,VI1,31,6,1,31,4011


grail 09-01-2011 09:42 AM

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
In bash:
Code:

while read line
do
    echo -e "$(date '+%d-%m-%Y' -d '1 day ago')\t$line"
done < $fpname

For the awk to be comma separated you can just change the OFS value, whereas in bash you would need a line before the echo like:
Code:

line="${line// /,}"
And of course change the tab in the echo to a comma.


All times are GMT -5. The time now is 04:42 PM.