1833467 Members
2553 Online
110052 Solutions
New Discussion

Shell script Query

 
SOLVED
Go to solution
Amith_2
Frequent Advisor

Shell script Query

Hello,

I want to replace the value “99” which is in 79th position of a data file to spaces. Can any one help me how to achieve this in a script. It should be done only if the value is 99.

Regards
Amith
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor

Re: Shell script Query

Are the data line-oriented? (i.e each record terminated with a LF or LF/CR pair or is each record fixed-length with no separator. Is this a pure textfile containing no binary data. When you define the problem more precisely, the solutions often are obvious.
If it ain't broke, I can fix that.
Geoff Wild
Honored Contributor

Re: Shell script Query

As Clay says - we need to know the format of the file...

You could try:

cat yourfile | sed 's/99/ /g' > newfile

Rgds...Geoff

Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
James R. Ferguson
Acclaimed Contributor

Re: Shell script Query

Hi Amith:

*IF* you have an Ascii text file, then the following will replace the string of "99" with spaces _only_ when it is in the 79th position (counting the first character as *zero*):

# perl -ple 'substr($_,79,2," ") if substr($_,79,2)==99' file

If you count the 79th position from character one, then change the 79-to-78.

Regards!

...JRF...

Dennis Handly
Acclaimed Contributor
Solution

Re: Shell script Query

An awk solution would be:
awk '{
if (substr($0, 79, 2) == "99") {
$0 = substr($0,1,78) " " substr($0,81)
}
print
}'

awk allows access to positions beyond the end of a string and returns null. (awk's substr counts from 1.)
Amith_2
Frequent Advisor

Re: Shell script Query

I am attaching the file which contains a record and i need to change the value 99 starting from 79th position to spaces.
I tried the commands but none is working
Dennis Handly
Acclaimed Contributor

Re: Shell script Query

>I am attaching the file which contains a record and i need to change the value 99 starting from 79th position to spaces.

It works according to what you said and we guessed. How do you number to 79th position? (JRF asked) Start from 1 or 0? I assumed 1 and the length was just 2.
...4999990...
...499 90...

(Note: My script make a copy to stdout with the changes.)