1826027 Members
3032 Online
109690 Solutions
New Discussion

Re: Help parsing file

 
SOLVED
Go to solution
cbres00
Frequent Advisor

Help parsing file

I have a file that could look like this:

aa/bb/cc/dd/ee|mm|nn|oo|pp|qq

Or this: aa/bb|mm|nn|oo|pp|qq
Or this: aa/bb/cc/dd|mm|nn|oo|pp|qq

The number of slashes in the first part of the record is variable. The number of pipes is known. I need to turn the last slash into a pipe. All other slashes need to be retained. How can I do this?

Regards,
Cathy
Life is too short not to have fun every single day
13 REPLIES 13
James A. Donovan
Honored Contributor
Solution

Re: Help parsing file

If this is always the format then you can use dirname/basename.

OLDNAME="aa/bb/cc/dd/ee|mm|nn|oo|pp|qq"
MYPATH=$(dirname $OLDNAME)
MYFILE=$(basename $OLDNAME)
NEWNAME=$(echo "$MYPATH|$MYFILE")

echo $NEWNAME
Remember, wherever you go, there you are...
cbres00
Frequent Advisor

Re: Help parsing file

I love the out-of-the box thinking here!
Simple, elegant, and brilliant!!

Many thanks!
Cathy
Life is too short not to have fun every single day
RAC_1
Honored Contributor

Re: Help parsing file

part1=$(dirname "aa/bb/cc/dd/ee|mm|nn|oo|pp|qq")
part2=$(basename "aa/bb/cc/dd/ee|mm|nn|oo|pp|qq")

echo "$part1|$part2"

Anil
There is no substitute to HARDWORK
cbres00
Frequent Advisor

Re: Help parsing file

One more thing.....how would you do this for every row in a file? Do a while/read loop?

Regards,
Cathy
Life is too short not to have fun every single day
Hai Nguyen_1
Honored Contributor

Re: Help parsing file

The while loop is:

while read LINE
do
OLDNAME=`echo $LINE`
MYPATH=$(dirname $OLDNAME)
MYFILE=$(basename $OLDNAME)
NEWNAME=$(echo "$MYPATH|$MYFILE")

echo $NEWNAME

done < input_file

Hai
curt larson_1
Honored Contributor

Re: Help parsing file

sed 's!\(.*\)/\(.*\)/\1|\2!'
H.Merijn Brand (procura
Honored Contributor

Re: Help parsing file

# perl -ne'($dir,$file)=m{^(.*)/(.*)$};if(($file=~tr/|/|/)==5){print"$dir|$file\n"}else{print"File '$file' does not have 5 pipes\n"}'

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
cbres00
Frequent Advisor

Re: Help parsing file

Are there any performance differences between the while/do/read solution and the sed solution?

Regards,
Cathy
Life is too short not to have fun every single day
James A. Donovan
Honored Contributor

Re: Help parsing file

The sed solution should be faster, but I think you would have to be modifying at least several hundred to several thousand lines at a time before you really started noticing the difference.
Remember, wherever you go, there you are...
Hein van den Heuvel
Honored Contributor

Re: Help parsing file

Not mearurably untill you need process a million records or more.

For a couple (hundreds) of thousand this is unlikely to be a significant task.

Cheers,
Hein.
cbres00
Frequent Advisor

Re: Help parsing file

Great. This is just too cool.
Thanks to all!
Cathy
Life is too short not to have fun every single day
curt larson_1
Honored Contributor

Re: Help parsing file

the performance difference is going to be in how many context switches your program makes.

while read
do
dirname
basename
done

does 2 context switches for each line

using sed or perl does one for the whole file

and

while read Line
do
start=${Line%/*}
last=${Line##*/}
print "$start|$last"
done

does none
cbres00
Frequent Advisor

Re: Help parsing file

I think I'll use Curt's example, even though everyone's input was great.

Thanks to you all.
Cathy
Life is too short not to have fun every single day