Hi All,
I have a file that I would like to search for data and replace other data by record position number:
Example search..
search for "CLARK KENT" and replace Amt data "000025" with "000155"???
I'm able to search and replace unique data but, came to a stump when wanting to replace data that can be similar throughout other records.
Example code of search routine..
You have to use double quotes on the sed command - the shell won't expand any variables in single quotes.
e.g on bash:
$ cat x.sh
#!/bin/bash
a="CLARK KENT"
b="000099"
sed "/${a}/s/^\(.\{50\}\).\{6\}/\1${b}/" infile
$ ./x.sh
1112223333BRAD PIT PO BOX 111 PHOENIX AZ10567000025103111
9994442222CLARK KENT PO BOX 222 LAS VEGAS NV27407000099102511
7778884444STEVE JOBS PO BOX 444 SUN CITY AZ10473000015100111
Awesome! Thanks CarloM.
Small problem though.... How would I pad data I replaced with spaces. Lest say I would like to search for name "CLARK KENT" and replace the city "LAS VAGAS" with "DEMING". When I do the sed command you showed me it shifts all the data to the left.
Thanks again CarloM, but now lets say I change "PHOENIX" to "LAS VEGAS" it separates "LAS" as a 20 characters and "VEGAS" as the next 20 characters. Sorry to keep making this harder than what it probably is...
a="BRAD PIT"
b="LAS VEGAS"
c="`(printf %-20s $b)`"
sed "/${a}/s/^\(.\{32\}\).\{11\}/\1${c}/" infile
Thank you so much CarloM! I was wondering though what is it in the printf command that pads the data with space? I was wondering if say I wanted to pad with zero's instead of spaces.