Operating System - HP-UX
1836207 Members
2290 Online
110096 Solutions
New Discussion

find and replace for strings in shell scripts

 
SOLVED
Go to solution
Prasad Govenkar
Occasional Contributor

find and replace for strings in shell scripts

I have a file , and each line has values seperated by pipe"|".

Now I have to replace the string after the 3th Pipe, with another string.
How do I do the same in unix shell script?

Suposse the file has contents as below.[ 2 lines]

B100|ABCD|1234|AM20|TEST
B100|ABCDAM20|1234|AM20|TEST

Now I want to replace the AM20 with TPID after the 3rd Pipe, and not in any other place

if I use sed command like this..
echo $LINE | sed -e 's/AM20/TPID/' >>CPQFILE.tmp

it will also replace ABCDAM20 in the second line as ABCDTPID which I DONT want to happen..
PS: I am reading the file line by line and outputting in a temp file.



9 REPLIES 9
Muthukumar_5
Honored Contributor

Re: find and replace for strings in shell scripts

Hai,

We can use the check pattern as |AM20|. It will be replaced with |TPID|

echo | sed -e 's/|AM20|/|TPID|/g'

It will do globally on the full file.

Regards,
Muthukumar.
Easy to suggest when don't know about the problem!
Simon Hargrave
Honored Contributor
Solution

Re: find and replace for strings in shell scripts

Try this: -

echo $LINE | awk -F\| 'BEGIN{OFS="|"}$4=="AM20"{$4="TPID"}{print $0}'
Muthukumar_5
Honored Contributor

Re: find and replace for strings in shell scripts

Hai,

We can do the above with tr command too.
Use the command setup as like,

echo $LINE | tr -c '|AM20|' '|TPID|'

It will work too.

Regards,
Muthukumar.
Easy to suggest when don't know about the problem!
Thierry Poels_1
Honored Contributor

Re: find and replace for strings in shell scripts

hi,

with vi:
%s/\(^.*|.*|.*|\)AM20\(|.*\)/\1TPID\2

with ed:
,s/\(^.*|.*|.*|\)AM20\(|.*\)/\1XXX\2

good luck,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Ionut Grigorescu_2
Super Advisor

Re: find and replace for strings in shell scripts

Hi,

what about using awk?

awk -F\| '{$4="your_string";print}' your_file
If it weren't for STRESS I'd have no energy at all
Allan Prentice
New Member

Re: find and replace for strings in shell scripts

Try this, which doesn't care about the field contents:

sed -e 's/^\([^|]*|[^|]*|[^|]*|\)\([^|]*\)\(.*\)$/\1NEWSTUFF\3/' < oldfile > newfile
Allan Prentice
New Member

Re: find and replace for strings in shell scripts

Or if you use Ionut's prettier awk script, you need to add the output field separator:

awk -F\| '{OFS="|";$4="your_string";print}' your_file
Rory R Hammond
Trusted Contributor

Re: find and replace for strings in shell scripts

I like simons answer the best. You should give him 10

cut -f 1-3 -d"|" test > test1

cut -f4 -d"|" test |sed -e"s/AM20/xxx/"> test2

cut -f "5-" -d"|" test > test3

paste -d "|" test1 test2 test3 > test

Rory
There are a 100 ways to do things and 97 of them are right
Fred Ruffet
Honored Contributor

Re: find and replace for strings in shell scripts

nobody to promote perl ? here it is :
$>cat file
B100|ABCD|1234|AM20|TEST
B100|ABCDAM20|1234|AM20|TEST
$>cat run
#!/opt/perl/bin/perl
while () {
($one,$two,$three,$four,$five)=split /\|/,$_;
$four=~ s/^AM20$/TPID/g;
print join '|', $one,$two,$three,$four,$five;
}
$>./run < file
B100|ABCD|1234|TPID|TEST
B100|ABCDAM20|1234|TPID|TEST

regards,

Fred
--

"Reality is just a point of view." (P. K. D.)