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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
08-31-2011, 07:08 AM
#1
LQ Newbie
Registered: Apr 2011
Location: India
Posts: 25
Rep:
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 .
08-31-2011, 07:26 AM
#2
Member
Registered: Apr 2010
Distribution: Fedora
Posts: 109
Rep:
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?
08-31-2011, 08:26 AM
#3
Guru
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,392
Perhaps you are referring to the tab that you did not place after the date?
08-31-2011, 09:22 PM
#4
LQ Newbie
Registered: Apr 2011
Location: India
Posts: 25
Original Poster
Rep:
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 .
08-31-2011, 09:54 PM
#5
Guru
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,392
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.
09-01-2011, 12:18 AM
#6
LQ Newbie
Registered: Apr 2011
Location: India
Posts: 25
Original Poster
Rep:
-----------------------
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
09-01-2011, 03:28 AM
#7
LQ Newbie
Registered: Apr 2011
Location: India
Posts: 25
Original Poster
Rep:
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 .
09-01-2011, 04:17 AM
#8
Guru
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,392
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
09-01-2011, 08:44 AM
#9
LQ Newbie
Registered: Apr 2011
Location: India
Posts: 25
Original Poster
Rep:
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
09-01-2011, 09:42 AM
#10
Guru
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,392
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.
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 10:45 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News