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!

Change a value in a column

Status
Not open for further replies.

Zteev

Programmer
Jul 28, 2003
17
US
Hi!

I have this file :
------------------
24123 9 0
43411 8 1
23433 7 0
23234 3 3

I'd like my AWK script to parse it and if a zero is found on the third column, to replace it by a blank character.

Actually my script look like this :
-----------------------------------
#!/usr/bin/awk -f

{
if ($3 == 0) {
$3=""
print
}
else {
print
}
}


When I run this script over my file, it returns :
-------------------------------------------------
24123 9
43411 8 1
23433 7
23234 3 3


It works, but when printing the modified lines, it doesn't space up the columns correctly. Is there any way to have the results lined up like the original file? I'd like them to be lined up exactly like the original file, even if the columns are not spaced up equally (i.e : two spaces between the first and second column, then four space between the second and third column..)

How can I do that?

Thanks,

Zteev

 
Something like this ?
awk '{x=$0;sub(/ 0$/," ",x);print x}' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 

This awk command changes every line with zero at the end of line and prints every line:

Code:
awk '/0$/ { $0 = substr($0, 1, length - 1) } { print }' inputfile

KP.
 
That works great! But what if the value to blank would be in the middle column? :)

Zteev
 
Something like this ?
nawk '{x=$0;gsub(/ 0( |$)/," ",x);print x}' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Oops, lack of one char space in the gsub.
Something like this ?
nawk '{x=$0;gsub(/ 0( |$)/," ",x);print x}' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
blancs and tabs are awk's IFS, so if $2 is a blanc
$2 will be $3, and $3 will be empty.

i invite you to think about your code ...
50 years long we are writing stupidities
we, not our computers are intelligent
they, not we are performant
NOTA: both codes run PERFECTLY :(((

A-STUPID-STATEMENT
{
# this is a numerical compare, and
# you have no idea what $3 contains
# could produce side-effects
if ($3 == 0) {
# assumed the 'if' statement is correct
# why are you empty an empty value ?
$3=""
print
}
else {
print
}
}
A-LESS-STUPID-STATEMENT
{
# the '*1' (could be '+0') is a sanity check,
# because this is an integer comparation
if ($3*1 == 0) $3="z";
print;
}

:) guggach
 
guggach, Your code results in...
[tt]
24123 9 z
43411 8 1
23433 7 z
23234 3 3
23234 0 3
23234 3 3
[/tt]
...which is not what is required, i.e.[ol]
[li]it does not space the columns correctly, [/li]
[li]it does not replace zeroes with blanks.[/li]
[/ol]
 
Ygor: why soooooo friendly
sure my code DOES NOT DO what they expect
it's only a little reflection


:) guggach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top