Parsing a file with a known delimiter

S

stevob

hi,

using perl, how would i take a file like:

something=else
nothing=other

and be able to split apart the line using "=" as the delimiter, and
assign each part to a $1, $2 value. I.e. $1=something and $2=else
This would be in a loop, so the next time around, $1=nothing and
$2=other. Basically, I need to be able to go thru each line and using
the "=" delimiter, I need to print out the line in reverse order, i.e.
else=something and other=nothing. Any help would be greatly
appreciated.

stevo
 
E

Eric J. Roode

(e-mail address removed) wrote in @z14g2000cwz.googlegroups.com:
hi,

using perl, how would i take a file like:

something=else
nothing=other

and be able to split apart the line using "=" as the delimiter, and
assign each part to a $1, $2 value. I.e. $1=something and $2=else
This would be in a loop, so the next time around, $1=nothing and
$2=other. Basically, I need to be able to go thru each line and using
the "=" delimiter, I need to print out the line in reverse order, i.e.
else=something and other=nothing. Any help would be greatly
appreciated.

I would use a regular expression:

perl -i.bak -lpe 's/^([^=]+)=(.*)$/$2=$1/' your_filename_here

That expression is
^ anchor to start of line
( capture
[^=]+ a run of characters that aren't "="
) end capture
= an "=" sign
(.*)$ the rest of the string, captured in $2

--
Eric
`$=`;$_=\%!;($_)=/(.)/;$==++$|;($.,$/,$,,$\,$",$;,$^,$#,$~,$*,$:,@%)=(
$!=~/(.)(.).(.)(.)(.)(.)..(.)(.)(.)..(.)......(.)/,$"),$=++;$.++;$.++;
$_++;$_++;($_,$\,$,)=($~.$"."$;$/$%[$?]$_$\$,$:$%[$?]",$"&$~,$#,);$,++
;$,++;$^|=$";`$_$\$,$/$:$;$~$*$%[$?]$.$~$*${#}$%[$?]$;$\$"$^$~$*.>&$=`
 
M

MrReallyVeryNice

hi,

using perl, how would i take a file like:

something=else
nothing=other

and be able to split apart the line using "=" as the delimiter, and
assign each part to a $1, $2 value. I.e. $1=something and $2=else
This would be in a loop, so the next time around, $1=nothing and
$2=other. Basically, I need to be able to go thru each line and using
the "=" delimiter, I need to print out the line in reverse order, i.e.
else=something and other=nothing. Any help would be greatly
appreciated.

stevo
If I had to complete this task, I would look at modules available at
CPAN. Have you looked at:

http://search.cpan.org/search?query=ini&mode=all


MrReallyVeryNice
 
J

Jeff

hi,

using perl, how would i take a file like:

something=else
nothing=other

and be able to split apart the line using "=" as the delimiter, and
assign each part to a $1, $2 value. I.e. $1=something and $2=else
This would be in a loop, so the next time around, $1=nothing and
$2=other. Basically, I need to be able to go thru each line and using
the "=" delimiter, I need to print out the line in reverse order, i.e.
else=something and other=nothing. Any help would be greatly
appreciated.

What did you try?
What happened when you tried it?

Despite the fact that this question reeks of "do my homework for me", you'll find you will
get more help, and learn more, if you show your efforts first, and allow people here to
help you learn from your own mistakes.

~Jeff
 
S

stevob

Thanks guys for the replies. I was able to use what Eric showed me,
and do some reading on regex for perl, and I was able to figure it out.
Thanks again.

s
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top