LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 03-02-2009, 11:36 PM   #1
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Rep: Reputation: 15
column re-alignment - space delimited to comma delimited


G'day,

How would I rearrange a list of entries from this:

192.168.1.23 2009/03/02 01:12:22 00:12:21:32:32:65

To:

2009-03-02 01:12:22,192.168.1.23,00:12:21:32:32:65

thanks & regards
 
Old 03-03-2009, 11:59 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by hattori.hanzo View Post
G'day,

How would I rearrange a list of entries from this:

192.168.1.23 2009/03/02 01:12:22 00:12:21:32:32:65

To:

2009-03-02 01:12:22,192.168.1.23,00:12:21:32:32:65

thanks & regards
There are lots of different ways. You can do it with bash, perl, ruby, python, C....what language? What have you tried already?
 
Old 03-03-2009, 01:14 PM   #3
Udi
Member
 
Registered: Jan 2009
Posts: 165

Rep: Reputation: 44
You can easily pass the list to a bash script or a bash function and loop over them:

for arg; do ...; done

You can also access the parameters as $1, $2, $3...
 
Old 03-03-2009, 02:32 PM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
This can be easily done with sed and awk, but I suspect this may be homework, so you'll have to try on your own first, maybe post what you might do.

Check here:
http://www.grymoire.com/Unix/

I'll give you a hint:

With awk you can use C-style printf statements.

Code:
awk '{ printf("%s", $1) }'
$1 is the first column delimited by whitespace ($2 is the second, etc.), %s tells it to format the output as a string, so maintain its current format in most cases.
 
Old 03-03-2009, 02:45 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by hattori.hanzo View Post
G'day,

How would I rearrange a list of entries from this:

192.168.1.23 2009/03/02 01:12:22 00:12:21:32:32:65

To:

2009-03-02 01:12:22,192.168.1.23,00:12:21:32:32:65

thanks & regards
G'day matey =} from the west-island ;)


Code:
echo 192.168.1.23 2009/03/02 01:12:22 00:12:21:32:32:65 | awk '{print gensub(/\//, "-", "g", $2)" "$3","$1","$4}'
2009-03-02 01:12:22,192.168.1.23,00:12:21:32:32:65
Close enough to what you want? ;)



Cheers,
Tink
 
Old 03-03-2009, 04:36 PM   #6
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Original Poster
Rep: Reputation: 15
Hello mates,

Thanks for the responses. This is what I have so far in perl which appears to be working.

Code:
print ($time,",",$mac,",",$ip,",",$name,"\n");
Lovely, I will check out the awk statement, as bash would be my preference. 8-)

Now I just need to extract : from the mac address...

cheers,
 
Old 03-03-2009, 04:44 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Code:
echo 192.168.1.23 2009/03/02 01:12:22 00:12:21:32:32:65 | awk '{print gensub(/\//, "-", "g", $2)" "$3","$1","gensub( /:/, "","g",$4)}'


Cheers ;}
 
Old 03-03-2009, 04:45 PM   #8
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Original Poster
Rep: Reputation: 15
Quote:
Now I just need to extract : from the mac address...
Dont worry about this, the function I am using has this ability so its all good.

regards,
 
Old 03-03-2009, 04:46 PM   #9
hattori.hanzo
Member
 
Registered: Aug 2006
Posts: 168

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tinkster View Post
Code:
echo 192.168.1.23 2009/03/02 01:12:22 00:12:21:32:32:65 | awk '{print gensub(/\//, "-", "g", $2)" "$3","$1","gensub( /:/, "","g",$4)}'


Cheers ;}
Wow. that was quick. nice one.. thanks mate. :-)
 
Old 03-05-2009, 12:54 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
echo "192.168.1.23 2009/03/02 01:12:22 00:12:21:32:32:65" | awk '{gsub(/\//,"-",$2);print $2,$3","$1","$4}'
 
  


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
Manipulating comma delimited CSV - newbie help with sed etc jonnymorris Programming 16 09-19-2008 06:14 AM
separating a comma delimited line mrobertson Programming 7 07-27-2005 01:56 PM
delimited and if nstanley Linux - Newbie 11 03-01-2004 09:35 AM
Comma-Delimited Website Filenames Apocalypse General 1 11-09-2003 09:05 AM
comma delimited file cdragon Programming 5 06-21-2002 07:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:39 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