I have a somehow big file (almost 3000 lines long and thirteen columns). Some lines have no value at all or, at least, are incomplete. The columns' values that have no data are marked with a "-" and the corresponding line (the line that owns that value) should be discarded and not used.
Due to text formatting problems, any excerpt of the real data cannot be posted here. Sorry.
The first and second columns doesn't interest us, since they are just countries' names (first column) and year (the second one).
I take two columns per calculation and if some column value is missing (if it has a "-"), then, the corresponding line (the line that owns that value) must be discarded.
In order to discard the line, I use a simple conditional statement:
That is, if some column has a "-", the value (or line) is not used. $COL1 and $COL2 are defined through the command line.
Using the third and the thirteenth columns of the dataset aforementioned, I got this as a result:
It appears that you are trying to take COL1 and COL2 from the environment; I don't see them defined in your programme, so that was my assumption. Im not sure that is possible. I don't usually use #!/bin/awk, and in the few tests I tried with the version I have installed here (GNU Awk 3.1.6) I was not able to use COL1 or COL2 from the environment in this manner.
I generally wrap my awk programmes inside of a shell script, and then the variables from the environment can be passed in. Something like this small example will print lines from your file that don't have a dash in the desired columns:
I used the code you provided from the command line:
On the code above, I set up col1 with the third column and col2 with the thirteenth column of the dataset I have here. Therefore, if anyone of those columns have a "-", then, that column value should be discarded, as well as the corresponding line.
As you can see on the image, there are lines that have a thirteenth column (the last column) with a "-" and are printed and they should not be printed.
Quote:
It appears that you are trying to take COL1 and COL2 from the environment;
Then, what should I do to set up COL1 and COL2 from the environment?
Thank you so much for your reply!
Marcelo
---------- Post updated at 11:41 PM ---------- Previous update was at 11:28 PM ----------
Quote:
Originally Posted by rdcwayx
I test your code in my env (CYGWIN +GNU AWK 3.1.8) without problem.
Agree with agama, use below code, you needn't update anything in reg.awk
As you can see on the image, there are characters missing on the countries' names and the first and second columns are mixed, let alone that there are values of the thirteenth column that are printed even though those values are the no-value marker "-".
I think the problem is that you don't need the $ with the -v assignments to hard assign them values. Try it this way:
If that doesn't work, then I'd replace the print statement with this to see what awk sees exactly in the 3rd and 13th columns, but try the previous change first.
to see exactly what awk beleives to be in the fields designated by col1 and col2.
You can take the values from the environment, but you'll have to assign them using the -v options. There are other ways, but they involve messy quoting and can be the cause of odd problems as a result.
Hi,
I am using korn shell.
until ]
do
echo "\$# = " $#
echo "$1"
shift
done
To the above script, I passed 2 parameters and the program control doesn't enter inside "until" loop. If I change it to until ] then it does work.
Why numeric comparison is not working with -ne and works... (3 Replies)
So, I have a files with entries in this format:
servername,username,sub_username,useless_datapoint,mail_size
So, a few sample lines:
server_a,bob,jane,useless,112351
server_a,bob,jim,useless,421193
server_a,bob,bob,useless,0029385
server_a,karen,will,useless,112351... (3 Replies)
Does anyone know how will I make awk's split work with empty or null separator/delimiter?
echo ABCD | awk '{ split($0,arr,""); print arr; }'
I need output like:
A
B
C
D
I am under HP-UX (5 Replies)
If there exists a field in stdin, print it, otherwise, print hello.....
These print nothing:
cat /dev/null | awk '{if ( length > 0 ) print $1; else print "hello"}'
cat /dev/null | awk '{if ( $1 ) print $1; else print "hello"}'But the scripts work if I run them directly in a terminal:
... (8 Replies)
Hi. I've been playing around a bit. This isn't for any practical purpose-- it's really just a theoretical exercise. I wrote this little thing:
foreach num ( 6 5 4 )
awk -v "number=$num" 'BEGIN{for(x=0;x<$number;x++) printf "-"; printf "\n"}'
end
I would expect the following output:
... (3 Replies)
Howdie everyone...
I have a shell script RemoveFiles.sh
Inside this file, it only has two commands as below:
rm -f ../../reportToday/temp/*
rm -f ../../report/*
My problem is that when i execute this script, nothing happened. Files remained unremoved. I don't see any error message as it... (2 Replies)
cat .servers | while read LINE; do
ssh jason@$LINE $1
done
exit 1
./command.ksh "ls -l ~jason"
Why does this ONLY iterate on the first server in the list? It's not doing the command on all the servers in the list, what am I missing?
Thanks!
JP (2 Replies)