Search and Replace by record position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and Replace by record position
# 1  
Old 11-01-2011
Search and Replace by record position

Hi All,
I have a file that I would like to search for data and replace other data by record position number:

Quote:
Example Data file:
1112223333BRAD PIT PO BOX 111 PHOENIX AZ10567000025103111
9994442222CLARK KENT PO BOX 222 LAS VEGAS NV27407000025102511
7778884444STEVE JOBS PO BOX 444 SUN CITY AZ10473000015100111

Pos. 1-10 Phone#
Pos. 11-21 Name
Pos. 22-32 Address
Pos. 33-43 City
Pos. 44-45 State
Pos. 46-50 Zip
Pos. 51-56 Amt
Pos. 57-62 Date
Example data may not be properly aligned once i post thread..
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..
Code:
$1="CLARK KENT"
$2="MJ WOO"

awk -v s="$1" -v r="$2" 'function convert(str) {return (length(str)>11)?substr(str,1,11):sprintf("%-11s",str)}
        {if ($0~s) sub(convert(s),convert(r))}1' FILENAME


Last edited by macastor; 11-01-2011 at 07:36 PM..
# 2  
Old 11-01-2011
Code tags preserve whitespace, quotes do not.

Try something like:
Code:
sed '/CLARK KENT/s/^\(.\{50\}\).\{6\}/\1000155/' infile


Last edited by CarloM; 11-01-2011 at 08:02 PM..
# 3  
Old 11-01-2011
Hmmm? So I tested the sed command and didn't get any results. Am I doing something wrong??

Quote:
$a="CLARK KENT"
$b="000099"

sed '/$a/s/^\(.\{50\}\).\{6\}/\1$b/' infile
# 4  
Old 11-01-2011
You have to use double quotes on the sed command - the shell won't expand any variables in single quotes.

e.g on bash:
Code:
$ 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


Last edited by CarloM; 11-01-2011 at 08:19 PM..
# 5  
Old 11-02-2011
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.
# 6  
Old 11-02-2011
You can use printf:
Code:
#!/bin/bash

a="CLARK KENT"
b="DEMING"
b1="$(printf %-11s $b)"

sed "/${a}/s/^\(.\{32\}\).\{11\}/\1${b1}/" infile

Also, note that you need to change the 2 numbers in the regular expression to match the size of the preceding text and of the text you're replacing.
# 7  
Old 11-02-2011
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... Smilie

Code:
a="BRAD PIT"
b="LAS VEGAS"
c="`(printf %-20s $b)`"

sed "/${a}/s/^\(.\{32\}\).\{11\}/\1${c}/" infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shift record from one position to another

Hi All, I have a file and it is a fixed length file. I want to move the values from 42,6 ( where 6 is length) to the 36th position Original file: 00000100000100000100000100000100001 000870 ... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

2. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

3. Shell Programming and Scripting

Replace a string for every record after the 1st record

I have data coming in the below format for each record <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><testdetials>....</test_sox> <?xml version="1.0" encoding="UTF-8" standalone="no"?><test_sox... (8 Replies)
Discussion started by: dsravanam
8 Replies

4. Shell Programming and Scripting

Search for a string at a particular position and replace with blank based on position

Hi, I have a file with multiple lines(fixed width dat file). I want to search for '02' in the positions 45-46 and if available, in that lines, I need to replace value in position 359 with blank. As I am new to unix, I am not able to figure out how to do this. Can you please help me to achieve... (9 Replies)
Discussion started by: Pradhikshan
9 Replies

5. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

6. UNIX for Dummies Questions & Answers

Search a string in the file and then replace another string after that position

Hi I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall: E.g: Actual File content: Hello Name: Nitin Raj Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies

7. Shell Programming and Scripting

search and replace fixed length record file

Hi I need to be search a file of fixed length records and when I hit a particular record that match a search string, substitute a known position field In the example file below FHEAD000000000120090806143011 THEAD0000000002Y0000000012 P00000000000000001234 TTAIL0000000003... (0 Replies)
Discussion started by: nedkelly007
0 Replies

8. UNIX for Dummies Questions & Answers

Search for a string and replace the searched string in the same position in samefile

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
27 Replies

9. Shell Programming and Scripting

Search for a string and replace the searched string in the same position

Hi All, My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
15 Replies

10. Shell Programming and Scripting

Add 'ENDEND' on end of each record at position is 14-20

I have file format like below and I'm trying to modify this file. I need to add 'ENDEND' end of each record. 01 ASH01 1CTCTL EDPPOO STAND 01 ASH08 0020 A1TH 101 01 ASH09 0022 A1TH 102 01 ASH09 0022 A1TH 103 01 ASH02 2CTCTL ... (5 Replies)
Discussion started by: naveenkcl
5 Replies
Login or Register to Ask a Question