Subsitute from a position till end of line.

Hi,
Having a following file's content, lets say:

ABC|ANA|LDJ|||||DKD||||||
AJJ|KKDD||KKDK||||||||||||
KKD||KD|||LLLD||||LLD|||||

Problem:

Need to replace pipes from 8th occurrence of pipe till end.

so the result should be:
ABC|ANA|LDJ|||||DKD
AJJ|KKDD||KKDK||||
-------
-------

Can someone give me one line sed or may one line perl script?

Thanks in advance !
:slight_smile:

if you have Python

# more file
ABC|ANA|LDJ|||||DKD||||||
AJJ|KKDD||KKDK||||||||||||
KKD||KD|||LLLD||||LLD|||||

# python -c "for line in open('file').read().split('\n'):print '|'.join(line.split('|')[:9])"
ABC|ANA|LDJ|||||DKD|
AJJ|KKDD||KKDK|||||
KKD||KD|||LLLD|||

Sorry, I don't have python installed !

cut -d \| -f1-8 FILENAME

Thanks !
I used cut to achieve this which is quite slow. Is it possible to achieve this using sed or any other utility?

Nothing is likely to be faster than cut. It's what cut is designed to do; other utilities will have much more overhead.

Thanks !
I was wondering, couldn't we just replace all the pipes(|) from 8th occurrence till end of the line with a new line. I can use this command with Perl as well...I mean I have a Unix shell script in which cut is used but it is quite slow therefore want to simulate the same in Perl. I don't either want to use split and join in Perl. It looks to be quite a bit of extra work.Therefore looking for a utility, may be one liner to get this done....

Any thought!!!

how big is your file? how fast do you want it to go?

300 MB .... should get over in less than 2 minutes....

then cut should be fine, at least on the sample data you provide.

Thanks, but how about achieving this in Perl , not using split and join...

Perl will probably be slower than cut; I thought you wanted fast.

BTW, running cut on a file much larger than yours (1,732,000,000 bytes) took about 1.5 minutes on my 1.6GHz P4 box.

Hmmm...then it is fine...may be I have to fine tune the code..
Thanks Jhonson !