LinuxQuestions.org
Visit the LQ Articles and Editorials section
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: 167

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
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,410

Rep: Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531Reputation: 2531
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
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,973
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
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: 167

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: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,973
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
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: 167

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

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,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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


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


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