LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 08-31-2011, 07:08 AM   #1
k_balaa
LQ Newbie
 
Registered: Apr 2011
Location: India
Posts: 28

Rep: Reputation: 0
Talking 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.

Last edited by k_balaa; 08-31-2011 at 10:46 PM.
 
Old 08-31-2011, 07:26 AM   #2
hi2arun
Member
 
Registered: Apr 2010
Distribution: Fedora
Posts: 109
Blog Entries: 4

Rep: Reputation: 33
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?
 
Old 08-31-2011, 08:26 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,580

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Perhaps you are referring to the tab that you did not place after the date?
 
Old 08-31-2011, 09:22 PM   #4
k_balaa
LQ Newbie
 
Registered: Apr 2011
Location: India
Posts: 28

Original Poster
Rep: Reputation: 0
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 

Last edited by k_balaa; 09-01-2011 at 12:17 AM.
 
Old 08-31-2011, 09:54 PM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,580

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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.

Last edited by grail; 08-31-2011 at 10:01 PM.
 
1 members found this post helpful.
Old 09-01-2011, 12:18 AM   #6
k_balaa
LQ Newbie
 
Registered: Apr 2011
Location: India
Posts: 28

Original Poster
Rep: Reputation: 0
-----------------------

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
 
Old 09-01-2011, 03:28 AM   #7
k_balaa
LQ Newbie
 
Registered: Apr 2011
Location: India
Posts: 28

Original Poster
Rep: Reputation: 0
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....

Last edited by k_balaa; 09-01-2011 at 03:32 AM.
 
Old 09-01-2011, 04:17 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,580

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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
 
Old 09-01-2011, 08:44 AM   #9
k_balaa
LQ Newbie
 
Registered: Apr 2011
Location: India
Posts: 28

Original Poster
Rep: Reputation: 0
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
 
Old 09-01-2011, 09:42 AM   #10
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,580

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
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.
 
  


Reply

Tags
readline, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 8 04-05-2011 09:18 AM
[SOLVED] Bash Script Problem - Stops after While loop K_REY_C Linux - Newbie 7 05-13-2010 06:59 PM
Bash infinite loop problem? stp001 Linux - Newbie 6 03-15-2010 06:56 AM
bash while loop problem MadMusician_uk Programming 3 04-20-2006 11:52 AM
bash for loop problem deadlock Programming 5 09-04-2003 04:32 AM


All times are GMT -5. The time now is 02:05 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration