search a replace each line- help needed ASAP

can someone help me with the find and replace command.

I have a input file which is in the below format:

0011200ALN00000000009EGYPT 000000000000199900000
0011200ALN00000000009EGYPT 000000000000199900000
0011200ALN00000000008EGYPT 000000000000199800000
0011200ALN00000000009EGYPT 000000000000199900000
0011200ALN00000000006EGYPT 000000000000199800000
0011200ALN00000000007EGYPT 000000000000199800000

I need to replace anything that has 9� in column 21 and �1999' in column 42to be changed to have �1900'. It should keep the rest of the file that does not match this criteria intact.

output of this change needs to be like this:

0011200ALN00000000009EGYPT 000000000000190000000
0011200ALN00000000009EGYPT 000000000000190000000
0011200ALN00000000008EGYPT 000000000000199800000
0011200ALN00000000009EGYPT 000000000000190000000
0011200ALN00000000006EGYPT 000000000000199800000
0011200ALN00000000007EGYPT 000000000000199800000

Can anyone help me with this?

i will be very helpful.

Thanks,
sandeep

Try...

awk '{print substr($0,1,41) (substr($0,42,4)=="1999"?"1900":substr($0,42,4)) substr($0,46)}' file1 > file2

Hi,

0011200ALN00000000009EGYPT 000000000000199900000
0011200ALN00000000009EGYPT 000000000000199900000
0011200ALN00000000008EGYPT 000000000000199800000
0011200ALN00000000009EGYPT 000000000000199900000
0011200ALN00000000006EGYPT 000000000000199800000
0011200ALN00000000007EGYPT 000000000000199800000

I got the output same as the input with no chnages.

Thanks,
Sandeep

Hello Sandeep,
I would suggest that you replace the complete string "000000000000199900000", if it is not a problem.

The command to use would be within vi and something like this
%s/old_word/new_word /g [this is within "vi". Command used to replace text (occurring any no. of times in the line) in the whole file (ignore /g if only first occurance in the line is to be replaced]

But be sure to make a copy of the original file before trying out this command.

Let me know how it goes.

Regards
Bimal

Hi Bimal,

Thanks for looking into this.

I need to schedule this job and hence doing it from Vi editor will be manual. Hence i will not be using that, and the records that i have pasted here are just some sample records, so replacing the complete string "000000000000199900000" will be a problem.

Thanks again

Sandeep

You gave a wrong position for the year so the solution of Ygor doesn't work. Try this one:

awk 'substr($0,21,1)==9{print $1,substr($2,1,12)"1900"substr($2,17);next}1' file

Regards

thanks so much

can you please explain this for me.. i am new to awk,

Thanks,
Sandeep

Sure,

awk 'substr($0,21,1)==9{print $1,substr($2,1,12)"1900"substr($2,17);next}1' file

substr($0,21,1)==9 -> if postition 21 of the line = 9 then do everything between the braces

print $1 -> print first field (before the space)

substr($2,1,12) -> print position 1 to 12 of second field (after space)

"1900"substr($2,17);next} -> print "1900" and position 17 of second field untill the end of the string and read the next line

1 -> print other lines

Go here for some tutorials:

http://www.unix.com/answers-frequently-asked-questions/13774-unix-tutorials-programming-tutorials-shell-scripting-tutorials.html

Regards

thanks so much:)

I had one last question on the above requirement. I tried a few changes in the awk code but did not work.

awk 'substr($0,21,1)==9{print $1,substr($2,1,12)"1900"substr($2,17);next}1' file

this will replace anything that has "9� in column 21 with �1900' in column 42.

what if i need to replace only records that has "9� in column 21 AND '1999' in column 42 ( matches both this criteria ) with �1900' in column 42.

Please advise

Thanks,
Sandeep

awk 'substr($0,21,1)==9 && substr($0,42,4)==1999{print $1,substr($2,1,12)"1900"substr($2,17);next}1' file

Regards

Thanks so much

Hi,

In my prevoius example file there was only one small space

example:

0011200ALN00000000009EGYPT 000000000000199900000

what If my file has spaces like the below file:

0011200ALN00000000009EGYPT 000000000000199900000
0011200ALN00000000009EGYPT 000000000000000000000
146920000100000000009DE SOTO 000000000001999000000

Can't there be a generic script written for this

Please advise?

Thanks,
Sandeep

Just operate on $0 all the time. $0 is the entire input line in awk, $1 $2 $3 etc is split into individual fields (on whitespace by default).

awk 'substr($0,21,1)==9 && substr($0,42,4)==1999{
  print substr($0,0,41) "1900" substr($0, 46); next }
1'

The offsets in your latest example don't match what was posted before; do you have multiple adjacent spaces there? Please use code tags when posting material where spaces are significant.

The vi tip also works; you can use sed instead of vi, although the regular expressions would be getting rather clumsy.

Hi Era,

If "9� is in column 21 and �00000000� in column 123 then i need to Change the �00000000� starting in column 123 to �19000101�.
This needs to works irrespective of if there are any spaces or not in the file.

The tried your code and it does not give me the required output.

The file is as below:

0011200ALN00000000009EGYPT 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000062004O000000000000000000000000000000000000000000000000390440000000000000450140460440000000000000190010000000000000000000

This is just an example record and spaces vary in the file. But the "9� in column 21 and �00000000� in column 123 remain in the saem positions irrespective of wherever the space is.

Can you please relook into this one.

Will be very helpfull.

If the offsets are different then change them. Based on what you posted before, the script looks for "9" in column 21 and "1999" in columns 42-45 (starting at 42, 4 positions). It's easy enough to change those.

Again, please use code tags when posting examples with significant spaces.

Hi era,

Thanks so much for the quick reply

I made the required chnages as below in ur code:

awk 'substr($0,21,1)==9 && substr($0,123,8)==00000000{print substr($0,0,122) "19000101" substr($0, 131); next }1'

But this does not give me a result.

Can you please guide me on how i can use code tags when posting examples. Sorry but i am using this for the first time.

I linked to this up in my earlier reply: The UNIX Forums - vB Code List

You might need to put the 000000 in double quotes. (My mawk doesn't require that, but it seems like a possible problem.)

Hi era,

I have attached the file to this post.

the file name is 123F.txt

Thanks,
Sandeep

It doesn't give a result because your file has no record that fits the condition.