delete last character in all occurences of string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting delete last character in all occurences of string
# 1  
Old 03-09-2010
delete last character in all occurences of string

Hello all,

I have a file containing the following
Code:
p1 q1 p2 q2 p1 p2 p3
pr1 pr2 pr1 pr2
pa1 pa2

I want to remove the last character from all strings that start with 'p' and end with '1'. In general, I do not know what is between the first part of the string and the last part of the string.
the result is then:
Code:
p q1 p2 q2 p p2 p3
pr pr2 pr pr2
pa pa2

I have tried something like this:
Code:
 sed 's/\(p[^ ]*1\)./\1/g' testfile

which will find all occurences of p*1, but I do not know how to get rid of the last character occuring in the string.

Regards,
B
# 2  
Old 03-09-2010
Code:
sed 's/\(p[^ ]*1\)./\1/g' testfile

Should be:
Code:
sed 's/\(p[^ ]*\)1/\1/g' testfile

# 3  
Old 03-09-2010
Code:
awk '{for(i=1;i<=NF;i++) {if($i~/^p.?1$/) gsub("1$","",$i)}}1' FILE

# 4  
Old 03-09-2010
Code:
perl -pe's/\b(p\S*)1\b/$1/g' infile

# 5  
Old 03-09-2010
Wow, three solutions in my three favorite scripting languages!
Thanks! :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete Directories and its files that begin with the same character string

I wrote a shell script program that supposed to delete any directories and its files that begin with the same character string . The program is designed to delete directories and its files that are one day old or less and only the directories that begin with the same character string. The problem... (1 Reply)
Discussion started by: dellanicholson
1 Replies

2. Shell Programming and Scripting

Count occurences of a character in a file by sorting results

Hello, I try to sort results of occurences in an array by using awk but I can't find the right command. that's why I'm asking your help ! :) Please see below the command that I run: awk '{ for ( i=1; i<=length; i++ ) arr++ }END{ for ( i in arr ) { print i, arr } }' dictionnary.txt ... (3 Replies)
Discussion started by: destin45
3 Replies

3. Shell Programming and Scripting

Delete all occurences of a pattern except first

Hi, Have a flat file like below: 1 2 4 1 2 1 3 Need to delete all duplicate instances. Output: 1 2 4 3 Can't use "uniq" as it works on a sorted file. Can't "sort" as need to maintain the original order. (3 Replies)
Discussion started by: vibhor_agarwali
3 Replies

4. UNIX for Advanced & Expert Users

couting occurences of a character inside a string and assigning it to a variable

echo "hello123" | tr -dc '' | wc -c using this command i can count the no of times a number from 0-9 occurs in the string "hello123" but how do i save this result inside a variable? if i do x= echo "hello123" | tr -dc '' | wc -c that does not work...plz suggest..thanks (3 Replies)
Discussion started by: arindamlive
3 Replies

5. Shell Programming and Scripting

Delete first character from a string stored in a variable

Hallo! Example. #!/bin/bash BACKUP_DIR=/home/userx/backups/evolution echo $BACKUP_DIR # delete the first character from the string BACKUP_DIR=$(echo $BACKUP_DIR | cut -c 2-) echo $BACKUP_DIR It works. It does want I want, delete the first character from string in the... (11 Replies)
Discussion started by: linuxinho
11 Replies

6. Shell Programming and Scripting

Count number of occurences of a character in a field defined by the character in another field

Hello, I have a text file with n lines in the following format (9 column fields): Example: contig00012 149606 G C 49 68 60 18 c$cccccacccccccccc^c I need to count the number of lower-case and upper-case occurences in column 9, respectively, of the... (3 Replies)
Discussion started by: s052866
3 Replies

7. Shell Programming and Scripting

Delete parts of a string of character in one given column of a tab delimited file

I would like to remove characters from column 7 so that from an input file looking like this: >HWI-EAS422_12:4:1:69:89 GGTTTAAATATTGCACAAAAGGTATAGAGCGT U0 1 0 0 ref_chr8.fa 6527777 F DD I get something like that in an output file: ... (13 Replies)
Discussion started by: matlavmac
13 Replies

8. HP-UX

count occurences of specific character in the file

For counting the occurences of specific character in the file I am issuing the command grep -o 'character' filename | wc -w It works in other shells but not in HP-UX as there is no option -o for grep. What do I do now? (9 Replies)
Discussion started by: superprogrammer
9 Replies

9. UNIX for Advanced & Expert Users

How to count no of occurences of a character in a string in UNIX

i have a string like echo "a|b|c" . i want to count the | symbols in this string . how to do this .plz tell the command (11 Replies)
Discussion started by: kamesh83
11 Replies

10. UNIX for Dummies Questions & Answers

Delete first 2 character from string

Hi ! All, I have to delete first 2 characters from string. How its possible? Like string value is "2001". I required output as "01" Plaese help me. Nitin (4 Replies)
Discussion started by: nitinshinde
4 Replies
Login or Register to Ask a Question