Operating System - HP-UX
1753587 Members
6605 Online
108796 Solutions
New Discussion юеВ

Script to find a delimiter and delete the next Character in each line

 
SOLVED
Go to solution
ssheri
Advisor

Script to find a delimiter and delete the next Character in each line

Hi,

Please, May I request a help to fix the below?
=============================================
I have a file in one of my HP-UX boxes. This has more than 10000 lines. In each line I need to find out the last appearnce of a delimiter and delete the immediate next character to that delimiter. The delimiter is ├П and the immedeate character is either Y or N.
=============================================
Please help.
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor
Solution

Re: Script to find a delimiter and delete the next Character in each line

Hi:

Given an input file like:

# cat ./myfile
a|b|c|d
e|f|g|h
i|j|k|l|m|n
o|p|q|r|s|t|u|v|w|x|y|z
1|2|3|N
4|5|6|7|Y

# perl -nle '@F=split m{\|};pop @F;print join "|",@F' myfile
a|b|c
e|f|g
i|j|k|l|m
o|p|q|r|s|t|u|v|w|x|y
1|2|3
4|5|6|7

Now, if you only want this performed if the last field is "Y" or "N" you could do:

# perl -nle '@F=split m{\|};pop @F if $F[@F-1]=~/(Y|N)/;print join "|",@F' myfile

From you post, I'm not exactly sure of the delimiter you use. I assumed that it was the pipe character. Change the 'm{\|}' and the 'join "|"' pieces to use the correct delimiter.

Regards!

...JRF...

Hein van den Heuvel
Honored Contributor

Re: Script to find a delimiter and delete the next Character in each line

>> The delimiter is ├Г and the immedeate character is either Y or N.

if there are only ever is a Y or N, then you can blindly delete the last char.. but that get's rid of the 'z' in Jim's example

# perl -pe 's/(.*├Г )./$1/'

If it must be a Y or N, and at the very end, then you can tighten up with:

# perl -pe 's/(.*├Г )[YN]$/$1/' tmp.txt

If there ever any text following the Y or N, then that needs to be remembered also. and you could use:

# perl -pe 's/(.*├Г )[YN]([^├Г ]*)$/$1$2/' tmp.txt

That last most difficult regexp breaks down to:

(.*├Г ) = match and remember in $1 any repeations(*) of any character (.) up to and including the last ├Г . 'Aggresive' wildcard.

[YN] = a Y or a N, add y and n as needed.

([^├Г ]*)$ = match and remember in $2 any series of non-├Г characters which must be at the end of the line.

Provide a few concrete, and most tricky, input and output lines in a TEXT attachment if more help is needed.

Good luck,

Hein.




ssheri
Advisor

Re: Script to find a delimiter and delete the next Character in each line

Many thanks for your quick response.

The delimiter is ├Г (Upper case I with two dots on the top). I will try as per your suggestions and let you know the results shortly.