Replace a numeric values in a certain column

Hi All,

I am trying to replace a certain value from one place in a file . In the below file at position 35 I will have 8 I need to modify all 8 in that position to 7

I tried

awk '{gsub("8","7",$35)}1' infile > outfile  ----> not working 
sed -i 's/8/7'g' infile --- it is replacing all the values  to 7 in the file.

infile:
020000492019010369200000000000000080009999005566005852006266004961000000000000000000000000
020000492019010369200012000809990080000021005566005852006266004961004100004615004490003951
020000492019010369200021131506450080000095005566005852006266004961004301006451006763006043
020000492019010369200033383653250080000121005566005852006266004961009473007622008426007768
020000492019010369200078000082160080000021005566005852006266004961004100004615004490003951
020000492019010369200078742014720080000021005566005852006266004961004100004615004490003951
020000492019010369200078742082950080000099005566005852006266004961002196002760003833002774
020000492019010369200078742104180080000099005566005852006266004961002196002760003833002774


Expected output:

020000492019010369200000000000000070009999005566005852006266004961000000000000000000000000
020000492019010369200012000809990070000021005566005852006266004961004100004615004490003951
020000492019010369200021131506450070000095005566005852006266004961004301006451006763006043
020000492019010369200033383653250070000121005566005852006266004961009473007622008426007768
020000492019010369200078000082160070000021005566005852006266004961004100004615004490003951
020000492019010369200078742014720070000021005566005852006266004961004100004615004490003951
020000492019010369200078742082950070000099005566005852006266004961002196002760003833002774
020000492019010369200078742104180070000099005566005852006266004961002196002760003833002774

Also on a follow up if I want to replace 008 to 009 . 008 is on the same column 33-35. What should I do

Hello arunkumar_mca,

Could you please try following once. This is specifically for 35th character's replacement.
I am using substring function of awk to get first 34 characters and then printing 7 along with rest of the characters of line.

awk '{print substr($0,1,34) "7" substr($0,36)}'  Input_file

Thanks,
R. Singh

Hello arunkumar_mca,

Position of a character(s) is different from column number.
Example:

123456a89

In above example if you talk in normal terms then position of a is 7 and it is NOT 7th column in that line.

Now question is when it becomes 7th column, let us think by default field separator is SPACE(I am talking about awk here). So your 7th position will be "called as " 7th field/column when field delimiter of awk is been set to NULL. Which is again a GNU awk function only. So please clear in your statements here.

Thanks,
R. Singh

Note that the code RavinderSingh13 suggested in post #2 in this thread will change the 35th character on every line; not just on lines where the original character in that position was " 8 ".

For your second question you might want to try:

awk '
{	if(substr($0, 33, 3) == "008")
		print substr($0, 1, 32) "009" substr($0, 36)
	else	print
}' infile > outfile

It wasn't at all clear whether you wanted to make both changes in the same input file. If you do, the order in which you process the changes is very important. If you perform the two operations in the order you specified, the change of " 008 " to " 009 " will never happen since the " 8 " will already have been changed to a " 7 ".

If what you want is to change " 008 " in character positions 33 through 35 (with the first position on a line being character position 1) and if there is a " 8 " in character position 35 where at least one of the characters in positions 33 and 34 is not " 0 ", then you might want to try:

awk '
{	if(substr($0, 33, 3) == "008")
		print substr($0, 1, 32) "009" substr($0, 36)
	else	if(substr($0, 35, 1) == "8")
			print substr($0, 1, 34) "7" substr($0, 36)
		else	print
}' infile > outfile

And, as RavinderSingh13 already said, you need to tell us on every thread that you start what operating system and shell you're using. Both of the above scripts will work on most BSD, Linux, and UNIX operating systems, but will not work on Solaris/SunOS systems. On Solaris/SunOS systems, you'll need to change awk in both of the above scripts to either /usr/xpg4/bin/awk or nawk .

1 Like