How to replace character in a file based on another file.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace character in a file based on another file.?
# 1  
Old 06-26-2013
How to replace character in a file based on another file.?

Hi Gurus,

I have really hard job need you guys to help.
i have two files
one is data file, one is instruction file like below
Code:
0000000010000233154825032720204abc  BC15
0000000010000233154825032720204defg DB15

Code:
1   9    o
10  6   r
16  5   o
21  10   r
31

the requirement is based on the instruction file, replace certain charactors with 'X',
in instruction file the first column is position in the string, second the charactors need to be replace. the third column is inidcator if the charactor need to be replaced or not. "o" means not replace, "r" means replace.
the instruction file can change as
Code:
1      o
10     r
16     o
21     r
31     o

this means from begining of the line until 10th charactor not replacement, from 10th to 15th replace with X, from 16th to 20 no replacement.
the expected result as following:
000000001xxxxxx15482xxxxxxxxxxabc BC15
000000001xxxxxx31548xxxxxxxxxxfefg DB15

Last edited by ken6503; 06-26-2013 at 05:08 PM..
# 2  
Old 06-26-2013
Please recheck your specification carefully, as at least the first one does not make sense. If you start at pos. 1 and leave 9 chars untouched, you get to pos 10. But then, replacing 15 chars, yould end up at pos 24, and you have passed pos 16 that'd be required in the next instruction line?
# 3  
Old 06-26-2013
Quote:
Originally Posted by RudiC
Please recheck your specification carefully, as at least the first one does not make sense. If you start at pos. 1 and leave 9 chars untouched, you get to pos 10. But then, replacing 15 chars, yould end up at pos 24, and you have passed pos 16 that'd be required in the next instruction line?
You are right.
the data is correct now.

Thanks
# 4  
Old 06-26-2013
Here is an awk approach:
Code:
awk '
        NR == FNR {
                if ( NF == 3 )
                        A[++c] = $1 "," $2 "," $3
                if ( NF == 2 )
                        A[++c] = $1 "," $2
                next
        }
        {
                R = $0
                for ( i = 1; i <= c; i++ )
                {
                        n = split ( A[i], V, "," )
                        s = V[1]
                        e = V[2]
                        if ( n == 3 )
                        {
                                if ( V[n] == "r" )
                                {
                                        while ( s <= e )
                                        {
                                                k = k ? k "x" : "x"
                                                s++
                                        }
                                        T = sprintf("%s",substr(R,1,V[1]-1))
                                        T = sprintf("%s%s",T,k)
                                        T = sprintf("%s%s",T,substr(R,V[2]+1))
                                }
                                R = T ? T : R
                                T = ""
                                k = ""
                        }
                }
                print R
        }
' inst file

Input
Code:
$ cat inst
1   9    o
10  15   r
16  20   o
21  30   r
31

$ cat file
0000000010000233154825032720204abc  BC15
0000000010000233154825032720204defg DB15

Output
Code:
$ ./ken.awk
000000001xxxxxx31548xxxxxxxxxx4abc  BC15
000000001xxxxxx31548xxxxxxxxxx4defg DB15

Note: I haven't added the logic for handling 2 column instruction file. I hope you can work on that.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 06-26-2013
Try also:
Code:
awk     'FNR==NR        {if ($NF == "r") C[$1]=$2; next}
                        {for (i in C)
                           for (j=i; j<i+C[i]; j++) $j="x"}
         1
        ' instr FS="" OFS="" data
000000001xxxxxx31548xxxxxxxxxx4abc  BC15
000000001xxxxxx31548xxxxxxxxxx4defg DB15

Note that your sample output does NOT reflect the specified logics to your given input file.
Note also that this approach does not do a consistency check on the instructions; it merely ignores the "o"- lines.
This User Gave Thanks to RudiC For This Post:
# 6  
Old 06-26-2013
Thanks both of you. the code works perfect.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace character in a file.

I have a data like this in a file. 05/08/2017,U,01,116326001 05/08/2017,U,01,116226001 05/08/2017,U,01,116726323 05/08/2017,U,01,116236001 I want replace the date(1st column) of all records. Ex: 05/08/2017 to 04/02/2017 Please use CODE tags when displaying sample input, sample output,... (1 Reply)
Discussion started by: Artlk
1 Replies

2. UNIX for Advanced & Expert Users

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

3. UNIX for Dummies Questions & Answers

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

4. UNIX for Dummies Questions & Answers

Replace character string in txt file using input file(S)

Hi I have a large txt file on my AIX server and I need to replace some text using two other files. So filename1 has about 500 lines similar to: txtcode SYStem100 I have the string I want to change in string2 and the new stringname in string3. Does anyone know a way of doing this? I have... (1 Reply)
Discussion started by: Grueben
1 Replies

5. Shell Programming and Scripting

File character adjustment based on specific character

i have a reqirement to adjust the data in a file based on a perticular character the sample data is as below 483PDEAN CORRIGAN 52304037528955WAGES 50000 89BP ABCD MASTER352 5434604223735428 4200 58BP SOUTHERN WA848 ... (1 Reply)
Discussion started by: pema.yozer
1 Replies

6. Shell Programming and Scripting

Replace character based on reference file problem asking

I got two files right now, input file (target file), reference file 1 (query file) reference file 1 (long list of data) KOLOPWMOPOPO ADASDASD ADSASDASDAD . . target file (one long liner content) ADASDASDTYUKOKOLOPWMOPOPOOPLUAADSASDASDADPOPOUYADADASDASD desired output file content ... (1 Reply)
Discussion started by: patrick87
1 Replies

7. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

8. Shell Programming and Scripting

File Move Based on 1st Character of File Name

I need some help with a unix script to mv image files to subdirectories based on the 1st character of the filename. See example below... /images/main 1191.jpg 9999.jpg A101.jpg A102.jpg B201.jpg c333.jpg ... Z999.jpg I would like to move to the following: ... (11 Replies)
Discussion started by: srdconsulting
11 Replies

9. Shell Programming and Scripting

How to replace a character in the file?

Hi All, I have file contains the following information. chem00s4.mis.amat.com ] Critical 3/21 chem00s4.mis.amat.com ] Normal 3/22 chem00s4.mis.amat.com ] Normal 3/23 chem00s4.mis.amat.com ] Normal 3/24 chem00s4.mis.amat.com ] Critical 3/25... (2 Replies)
Discussion started by: ntgobinath
2 Replies

10. Shell Programming and Scripting

replace words in file based on another file

Hello, Can someone kindy help me solve this problem..I am using SunOS shell script I got a file A with following content: This is my correct document. I wrote 111 This is my incorrect word , 222 This is my wrong statement 333 This is my correct document 444 This is my correct document 555... (9 Replies)
Discussion started by: kinmak
9 Replies
Login or Register to Ask a Question