Debaditya
Debaditya

Reputation: 2497

Pattern Matching in Shell

My input text is in this format

 aa5b    r1     12715
 r2     12221
 aa43b   ew     13721
 eb     122331
 aa4b    ff     1055440

Output must be

aa5    r1     12715
r2    12221
aa43   ew     13721
eb  122331
aa4    ff     1055440

I tried with

 awk -F " " '{print $1}' t1 | grep "^aa*" > t2|sed s/b//g t2

The problem with this code is that the input text is not having a fixed pattern.

Upvotes: 3

Views: 1986

Answers (4)

William Pursell
William Pursell

Reputation: 212208

The specifications are not clear from your description of the problem, but if you just want to delete all occurrences of the character 'b' on lines that begin 'aa', you can use:

sed '/^aa/s/b//g'

Upvotes: 1

bmk
bmk

Reputation: 14137

What about this version with sed?

sed 's/^\(aa[0-9]*\)b/\1/' t1 > t2

Upvotes: 1

Prince John Wesley
Prince John Wesley

Reputation: 63688

awk ' $1 ~ /a/ { $1= substr($1,0,length($1)-1) };1' infile > outfile

Upvotes: 0

Wes Hardaker
Wes Hardaker

Reputation: 22252

It'd be easier to use something like perl and regex:

perl -p -e 's/^(aa[0-9]+)\w+/$1/' t1 > t2

Upvotes: 1

Related Questions