Computing.Net > Forums > Unix > Parsing Text

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Parsing Text

Reply to Message Icon

Name: Puma1337
Date: May 5, 2006 at 10:25:01 Pacific
OS: OS X/Fedore Core
CPU/Ram: 1GB RAM
Product: Mac
Comment:

I have a text files with 8000 lines of information set up as follows:

Member Name MBR TYP CNT Phone etc
John Doe 101 1 111-111-1111
123 Fake Street
Fake, FT 10000

I need the Address to be next to the name on all of the entries:

Member Name Address City etc.
John Doe 123 Fake Street Fake, FT 10000

How would i go about doing that? Thank you



Sponsored Link
Ads by Google

Response Number 1
Name: Puma1337
Date: May 5, 2006 at 10:31:08 Pacific
Reply:

If I wasn't clear, all I need is the address to be next to the name instead of under it the phone and other stuff don't matter, they are already correct.


0

Response Number 2
Name: sboffin
Date: May 5, 2006 at 11:52:51 Pacific
Reply:

If you have a blank line between each section I would use a awk script to do that.



0

Response Number 3
Name: Puma1337
Date: May 5, 2006 at 11:56:08 Pacific
Reply:

I dont, they are one right on top of the other. I wasn't the one who created the file, now I'm stuck with parsing it. >.<


0

Response Number 4
Name: nails
Date: May 5, 2006 at 15:15:43 Pacific
Reply:

Is your file divided into 3 line blocks for each name?

If so, here's what I gather from your requirements:

print line 1 with no CR
print line 2 with a comma, a space, and no CR
print line 3 with a CR

If so, does this work:

#!/bin/ksh

lineno=0
while read line
do
((lineno=lineno+1))
if (( lineno == 2 ))
then # need a comma, no CR
printf "%s, " "$line"
elif (( lineno == 3 ))
then # need CR
lineno=0
printf "%s\n" "$line"
else # line 1, no CR
printf "%s" "$line"
fi
done < data.file



0

Response Number 5
Name: FishMonger
Date: May 5, 2006 at 23:25:54 Pacific
Reply:

If all records have the same format as your example, you can use this 1 line Perl command.

perl -pi.bak -e 's/^/ / if /,/; chomp unless $. % 3 == 0' file.txt

In addition to reformating the file, that command will also create a backup copy of the original file. If you don't whant the backup, you can change it to this.

perl -pi -e 's/^/ / if /,/; chomp unless $. % 3 == 0' file.txt


0

Related Posts

See More



Response Number 6
Name: FishMonger
Date: May 5, 2006 at 23:29:58 Pacific
Reply:

Nails,

I haven't forgoten about the article; I've just been very busy. Do you still want me to write it?


0

Response Number 7
Name: nails
Date: May 6, 2006 at 07:54:58 Pacific
Reply:

Fish:

Yes, whenever you can find the time.


0

Response Number 8
Name: Puma1337
Date: May 6, 2006 at 18:08:31 Pacific
Reply:

Thanks for the code. Unfortunately they are not all in the
same format. Some have only the city and not the street
name, some have no address at all, and some have an extra
line with an apartment number. Is there any simple way to
do this? Thanks


0

Response Number 9
Name: FishMonger
Date: May 6, 2006 at 19:58:23 Pacific
Reply:

Based on your random formats, no this won't be very easy, but its doable.

If you can post a more comprehensive example of you actual data, I'll see what I can come up with.


0

Response Number 10
Name: Puma1337
Date: May 8, 2006 at 10:08:25 Pacific
Reply:

Heres what it looks like:

Membership Name
John Doe
123 Fake Street
Faketown, FA 10000
Ann Doe
124 Fake Street
Apt. 26J
Faketown, FA 10000
Tom Doe
124 Fake Street
#12
Faketown, FA 10000

Of course there is no pattern, it is just random. There are other data fields to the left and to the right, but they don't need to be parsed. I hope this helps. Thank you for all the time you have put into this. I really appreciate it.


0

Response Number 11
Name: FishMonger
Date: May 8, 2006 at 10:19:41 Pacific
Reply:

What do you mean by:
"There are other data fields to the left and to the right, but they don't need to be parsed."

Without knowing the EXACT format of your file, it will be IMPOSIBLE for me to write a parser. My crystal ball hasn't worked for a long time.

At this point, I think it might be faster for you to edit the file in a text editor.


0

Response Number 12
Name: Puma1337
Date: May 8, 2006 at 16:26:57 Pacific
Reply:

Is there a way I can send you a sample of the file?


0

Response Number 13
Name: FishMonger
Date: May 9, 2006 at 08:43:48 Pacific
Reply:

Email the file to me and include an example of exactly how you want it formatted and I'll see what I can do. Click on my username to get my address.


0

Response Number 14
Name: FishMonger
Date: May 9, 2006 at 22:20:27 Pacific
Reply:

Wow, the format in the files you emailed me is not even close to what you described in your question. The data fields to the left, right and above that you say don't need to be parsed, must be parsed! You can't just ignore them, afterall they are also part of your desired revised format!

I'll see what I can do, but don't count on anything because this might take more time than I'm willing to put in.


0

Response Number 15
Name: Puma1337
Date: May 10, 2006 at 10:26:49 Pacific
Reply:

Alright, thanks so much for all your help. If you don't have
the time to figure it out, I will do it all by hand. There are
over 150 pages >.<


0

Response Number 16
Name: lchi2000g
Date: May 12, 2006 at 10:48:09 Pacific
Reply:

"Sometimes", using vi map feature to define a couple keys will save you a lot efforts.

Luke Chi


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Parsing Text

parsing a variable to a function www.computing.net/answers/unix/parsing-a-variable-to-a-function-/5613.html

Simple text file parsing? www.computing.net/answers/unix/simple-text-file-parsing/5920.html

Shell script for Text file parsing www.computing.net/answers/unix/shell-script-for-text-file-parsing/4388.html