parsing a line.

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
azfar
Captain
Posts: 598
Joined: Tue Mar 23, 2004 1:16 am
Location: Karachi
Contact:

parsing a line.

Post by azfar »

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
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

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"?
LinuxFreaK
Site Admin
Posts: 5132
Joined: Fri May 02, 2003 10:24 am
Location: Karachi
Contact:

Re: parsing a line.

Post by LinuxFreaK »

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
azfar
Captain
Posts: 598
Joined: Tue Mar 23, 2004 1:16 am
Location: Karachi
Contact:

Post by azfar »

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