Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 10000I 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 10000How would i go about doing that? Thank you

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.

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

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

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

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

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

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.

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 10000Of 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.

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.

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.

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.

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

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

![]() |
![]() |
![]() |

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