parsing a line.

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.

parsing a line.

Postby azfar » Mon Dec 07, 2009 3:33 pm

I want to parse a string but I am getting it.

The string is below

Code: Select all

31200912011320000100000001810001
31200912011704000200000001810001


and I want to break it like

Code: Select all

31,2009,12,01,1320,0001,0000000181,0001
31,2009,12,01,1704,0002,0000000181,0001


My token are like this.

Code: Select all

Fixed Value > Year > Month > Date > Staus > UID > Fixed Value


any help will be appreciated.
Azfar Hashmi
Email : azfarhashmi@hotmail.com
azfar
Captain
 
Posts: 598
Joined: Tue Mar 23, 2004 1:16 am
WLM: azfarhashmi@hotmail.com
Yahoo Messenger: azfarhusain@yahoo.com
Location: Karachi

Postby lambda » Tue Dec 08, 2009 11:47 am

well, if it's just a fixed-length string, it should be easy to take apart. show us your code. where are you stuck?
Watch out for the Manners Taliban!
Isn't it amazing how so many people can type "linuxpakistan.net" into their browsers but not "google.com"?
lambda
Major General
 
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Website: http://www.hungry.com/~fn/
Location: Lahore

Re: parsing a line.

Postby LinuxFreaK » Tue Jan 26, 2010 12:05 pm

Dear azfar,
Salam,

azfar wrote:I want to parse a string but I am getting it.

The string is below

Code: Select all

31200912011320000100000001810001
31200912011704000200000001810001


and I want to break it like

Code: Select all

31,2009,12,01,1320,0001,0000000181,0001
31,2009,12,01,1704,0002,0000000181,0001


My token are like this.

Code: Select all

Fixed Value > Year > Month > Date > Staus > UID > Fixed Value


any help will be appreciated.


Below command will help you.

# echo "31200912011320000100000001810001" | sed -r 's/^(.{2})(.{4})(.{2})(.{2})(.{4})(.{4})(.{10})(.{4})$/\1,\2,\3,\4,\5,\6,\7,\8/'

Best Regards.
Farrukh Ahmed
LinuxFreaK
Site Admin
 
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
ICQ: 82075802
Website: http://www.linuxpakistan.net/wiki/index.php?pagename=LinuxFreak
WLM: f4fahmed@hotmail.com
Yahoo Messenger: f4fahmed@yahoo.com
AOL: linuxpakistan@aol.com
Location: Karachi

Postby azfar » Mon Mar 22, 2010 11:52 am

Thanks Farrukh, I thought I had thanked you, any way meanwhile I have done it like below

Code: Select all

cat list.dat | cut -b 3-6,7-8,9-10,11-14,17-18,26-28 --output-delimiter='       ' | sort +5 -6 > data
Azfar Hashmi

Email : azfarhashmi@hotmail.com
azfar
Captain
 
Posts: 598
Joined: Tue Mar 23, 2004 1:16 am
WLM: azfarhashmi@hotmail.com
Yahoo Messenger: azfarhusain@yahoo.com
Location: Karachi


Return to “%s” Programming

Who is online

Users browsing this forum: No registered users and 2 guests

cron