Replace part of string-delete last letter of it
Hi to all
I would like to delete/(replace with nothing) the last letter of the strings that have the 2 or 3 numbers between "_P" and "X,Y or Z" in the follow pattern. Code:
YAN_P83Y Code:
sed '/P[0-9][0-9]|[0-9][X-Z]/ s/[X-Z]//g' inputfile Code:
YAN_P83 Thanks in advance |
Why do you think that regex will match 2 or 3 ?. Why not 1 (also) in that case ?.
That says match exactly 3 digits. Try Code:
sed -r '/P[0-9]{2,3}[X-Z]/ s/[X-Z]//g' inputfile |
First, you need extended regexes to use the "alternation" operator......e.g. sed -r 'stuff' <file>
Second, I'm not sure how this: "P[0-9][0-9]|[0-9][X-Z]" gets interpreted. I think it reads as: "P", followed by any digit, then any digit or any digit, then "X", "Y", or "Z". Try this: sed '/P[0-9]{2}[0-9]?[X-Z]/s/[X-Z]//g' filename ("P", followed by 2 digits, then a third optional digit, then "X", "Y", or "Z".) But, this still replaces the leading "Y", so some more work is needed. e.g.: "X$" means the "X" at the end of the line. |
if you have Python
Code:
#!/usr/bin/env python Code:
# more file |
In perl
In Perl using chop the last character has been deleted from the string.
|
Cgcamal didn't specify all the details. E.g., must "_" be present? Is X,Y,Z the last character of the line? Should it be retained after 1 or 4 digits? Must "sed" be used?
If Perl is allowed, it's easy: Code:
perl -wpe 's/(_P\d{2,3})[XYZ]/$1/' Thangappan is right, "chop" drops the last character, at first the newline. You could do this: Code:
perl -wne 'chop;chop;print $_,"\n"' /Quigi |
Quote:
Code:
perl -wpe 's/(_P\d{2,3}.\n/$1\n/' |
Quote:
The examples would be done right even by s/.$// -- simply dropping the last character. |
That seems to work as it's supposed to:
Code:
sed '/P[0-9]\{2,3\}[X-Z]/ s/\(P[0-9]*\)\([X-Z]\)/\1/g' filename |
Hi again, many thanks to all for help me in this question. Sorry for not put more details the sample inputfile before.
Now I come back with a more detailed inputfile that would be as follow: # The strings wanted are surrounded by other columns on left and on right. #inputfile Code:
Some other text syg00 (Thanks): Code:
sed '/P[0-9]{2}[0-9]?[X-Z]/ s/[X-Z]//g' inputfile #This script doesn´t change anything in the inputfile Code:
sed '/P[0-9]{2}[0-9]?[X-Z]/s/[X-Z]//g' inputfile #This script doesn´t change anything in the inputfile Code:
With script below, many thanks for your help, but I don´t have Python: Quigi (Thanks): Code:
perl -wpe 's/(_P\d{2,3})[XYZ]/$1/' inputfile # This script works! Code:
$ perl -wpe 's/(_P\d{2,3}.\n/$1\n/' inputfile # With this script I get the next error Code:
sed '/P[0-9]\{2,3\}[X-Z]/ s/\(P[0-9]*\)\([X-Z]\)/\1/g' inputfile #This script works on Cygwin and UWIN either, sycamorex or someone else: 1- How it works this regex you have used? May you explain it a little bit? Code:
sed '/P[0-9]\{2,3\}[X-Z]/ s/\(P[0-9]*\)\([X-Z]\)/\1/g' filename Thanks again to all, Best regards. |
If you want to understand sed, start here: http://www.grymoire.com/Unix/Sed.html#uh-0
|
Quote:
Sorry, I was modifying somebody else's regular expression - this form: Code:
perl -wpe 's/(_P\d{2,3}).\n/$1\n/' input_file |
Quote:
Apply the substitutions only for lines matching the following pattern: Quote:
The substitutions: Quote:
1) So the first container (1) is: Quote:
2. The second 'container' is: Quote:
Then we replace what has been matched in both containers with the first container only: \1 (omitting whatever was matched in the second container). If you put \2 instead, it would leave only what was matched in the second container. I hope it makes sense. If not you can read something about back referencing in sed. Sorry, need to go back to work - big boss is watching. |
SED has a marvelous ability to give you totally inscrutable code----the Grymoire site (linked earlier) saved me. It is missing a few nuances, so read the official manual also.
|
Hi guys, many thanks to all, I´ve learned a lot from your different solutions and your references to study will be useful without doubt.
sycamorex, the "container" trick/option is 90% what I needed to get the desired result using sed in the way I was original trying. Many thanks to all again. best regards |
All times are GMT -5. The time now is 12:46 PM. |