LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-22-2012, 04:28 PM   #1
tekvaio
LQ Newbie
 
Registered: Sep 2008
Posts: 7

Rep: Reputation: 0
AWK/PERL printing all fields


I have the following line

2010-10-10 14:39:11,634 0000, 1AB :123ABC42-E4CF-1A99-567F-1234567890AB SOMEINFORMATIONHERE [Some Information here]

and would need it in this format

2012-10-22,14:39:11,634,0000,1AB,:760ED8C2-E4CF-1A99-567F-FBC8606973DA,SOMEINFORMATIONHERE,Some Information here

I have tried the following:

awk '{ print $1 "," $2 "," $3 "," $4 "," $5 "," $6 "," $7 "," }'

but i'm hung up with the last field .
As you can see above, I'm using the default Field Separator, which works up until the last field. Any help is appreciated
 
Old 10-22-2012, 04:53 PM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
doh!....
my bad...

Last edited by Habitual; 10-22-2012 at 04:58 PM.
 
Old 10-22-2012, 04:58 PM   #3
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Hi, does this do what you want?

Code:
awk -F [][] '{gsub(/[ ,]+/,",",$1); print $1$2}'
 
Old 10-23-2012, 02:13 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.


Here's a sed solution that appears to work, at least on the above example.

Code:
sed -r -e 's/,?[ ]/,/g' -e 's/[][]//g' -e 's/[,]/ /8g'
Note how you can add a number at the end of the s option to specify which specific instance of the pattern in the line to operate on. Adding a g then makes it mean from that position onwards. (There is no a simple way to go from the start of the line to a specific position, however.)

Here's the output I get:
Code:
2010-10-10,14:39:11,634,0000,1AB,:123ABC42-E4CF-1A99-567F-1234567890AB,SOMEINFORMATIONHERE,Some Information here
 
Old 10-23-2012, 11:45 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Is it just me or did no one notice that the lines of data are not the same and I don't mean replacing spaces with commas.
Specifically:
Code:
:123ABC42-E4CF-1A99-567F-1234567890AB

:760ED8C2-E4CF-1A99-567F-FBC8606973DA
No code supplied has allowed for this, not even the OP's original example.
 
Old 10-23-2012, 12:54 PM   #6
tekvaio
LQ Newbie
 
Registered: Sep 2008
Posts: 7

Original Poster
Rep: Reputation: 0
thanks this.. helped ..
 
Old 10-23-2012, 01:03 PM   #7
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by grail View Post
Is it just me or did no one notice that the lines of data are not the same and I don't mean replacing spaces with commas.
Specifically:
Code:
:123ABC42-E4CF-1A99-567F-1234567890AB

:760ED8C2-E4CF-1A99-567F-FBC8606973DA
No code supplied has allowed for this, not even the OP's original example.
Yes I noticed that. I assumed the the two lines are only presented as an example of input and output format, the second line being a result of processing a different input than the first line, since that was the only possibility to allow a sensible solution based on the information provided. Also, the attempted original poster's solution seems to suggest that the desired result is to only change the format. But I may of course be wrong...
 
Old 10-23-2012, 01:23 PM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I assumed the same as millgates. Although it would've been clearer if the OP had used the same data for both input and output, the desire was clear.


@tekvaio: I'm glad we could help you out, but what we'd really like to hear is what exactly in our posts helped you. It would be polite to give us a bit of detail on what you ended up using and why, and it may also help others who come across the thread later.
 
Old 10-23-2012, 04:02 PM   #9
tekvaio
LQ Newbie
 
Registered: Sep 2008
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
Is it just me or did no one notice that the lines of data are not the same and I don't mean replacing spaces with commas.
Specifically:
Code:

:123ABC42-E4CF-1A99-567F-1234567890AB

:760ED8C2-E4CF-1A99-567F-FBC8606973DA

No code supplied has allowed for this, not even the OP's original example.
Yes I noticed that. I assumed the the two lines are only presented as an example of input and output format, the second line being a result of processing a different input than the first line, since that was the only possibility to allow a sensible solution based on the information provided. Also, the attempted original poster's solution seems to suggest that the desired result is to only change the format. But I may of course be wrong...
yes.. thats correct.. sorry about that, i wrote this post in hurry and wasn't completely accurate at the time.

Quote:
sed -r -e 's/,?[ ]/,/g' -e 's/[][]//g' -e 's/[,]/ /8g'
gave me the desired output . lesson learned..
 
Old 10-25-2012, 12:19 PM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Thank you for the detailed reply. I'm happy I could help.
Don't hesitate to ask if you have any further questions.
 
  


Reply



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
[SOLVED] Awk with missing fields Linux_Kidd Programming 39 04-27-2012 02:27 PM
AWK looping though fields casperdaghost Linux - Newbie 10 12-31-2011 09:31 AM
awk question on handling *.CSV "text fields" in awk jschiwal Programming 8 05-27-2010 06:23 AM
[SOLVED] get fields using awk ashok.g Programming 9 12-09-2009 01:21 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:37 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration