Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changing records

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi

I have a file with lines of records. eg.
075784993839383847120000
I want to change the last 4 zeros using a variable I have set how do I do it.

Thanks
Dean
 
Hi Dean!

You can do it like this if the records are *always*
going to end with 4 zeros:

nawk '{
var = "done"
sub(/0000$/, var)
print
}' infile > outfile

This will only work if you have nawk, gawk, or another
modern version of awk that will accept the sub command.

You can also do it with sed:

sed 's/0000$/done/' infile > outfile

Hope this helps!
[sig]<p>flogrr<br><a href=mailto:flogr@yahoo.com>flogr@yahoo.com</a><br><a href= > </a><br> [/sig]
 
Hi

I forgot to add after the zeros there is about 50 spaces. I have tried the above soloution but I would have to put 50 dots in the comand, what I thought of is using the substr command to pick up the column and then use sub to change it eg. sub(substr($0,10,4), ($total)) but this does not seem to work. any ideas please.

Dean
 
Dean-

We'll have to to it this way then to capture everything
after the 4 zeros. This assumes a fixed column count
before the 4 zeros start!

nawk 'BEGIN{FS = OFS = &quot;&quot;}

{
var = &quot;done&quot;

first = substr($0,1,20)
third = substr($0,25)

$0 = sprintf(&quot;%s%s%s&quot;, first, var, third)

print

}' infile > outfile

This is based on your first example where there are 20
columns before the 4 zeros start.

[sig]<p>flogrr<br><a href=mailto:flogr@yahoo.com>flogr@yahoo.com</a><br><a href= > </a><br> [/sig]
 
Or you could go with flogrr's 1st example, but just look for &quot;0000 &quot; in the string, and stick a space on the end of the variable you're substituting ...

nawk '{
var = &quot;done &quot;
sub(/0000./, var)
print
}' infile > outfile


Greg. [sig][/sig]
 
Note if you were subsitute print $1 for for print then that would automaticaly exclude any white space (assuming no gaps in the middle of the number).
I had something already in use, something like this in ksh:

export VAR1=&quot;whatever&quot;
cat inf | awk '{print $1}' | sed -e &quot;s/string/$VAR1/&quot; > outf [sig]<p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>Top man[/sig]
 
I Think this will realy help you


awk '{
var=&quot;VARR&quot;
#when there are lines that don't have the last 4 zeros.
if( substr( $1, length( $1) - 3) == &quot;0000&quot;)
print substr( $1, 1, length( $1) - 4) var
else
print $0
}'

awk '{
var=&quot;VARR&quot;
#when all lines have the last 4 zeros.
print substr( $1, 1, length( $1) - 4) var
}'


succes [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top