problem with awk printf
Hi all,
I have a problem executing this very simple awk script....Please help me to solve this problem <CODE> BEGIN { fs="-" printf "Enter the date in mm-dd-yyyy format } { printf "Date is %d, Month is %d, Year is %d" ,$2, $1,$3 } END { } </CODE> if the input is say 11-28-2010 then output is Date is 0 Month is 11 year is 0 :( |
Hi,
This should work: Code:
awk 'BEGIN { FS="-" - You are missing a " at the end of the first printf command, - The printf syntax you use is not correct (EDIT: The parentheses are optional) Hope this helps. |
Hi -
Let's break it down, shall we? Code:
echo '11-28-2010' | \ Code:
echo '11-28-2010' | awk '{print $1 "," $2 "," $3}' Your original "printf "%d"" truncated the result the moment it encountered the first non-number in your input string. And there were NEVER any "$2" or "$3" arguments following the truncated "$1". Code:
echo '11-28-2010' | awk 'BEGIN {FS="-"}; {print $1 "," $2 "," $3}' "BEGIN {}", with a capital "FS"; followed by your "{printf xyz}" clause |
May want to throw an exit after the last printf in drunna's example or you have to hit ctrl-c or ctrl-d to stop input.
@paulsm4 - you seemed to have missed the setting of FS to a dash. |
Hi, Grail -
No, I didn't miss the "FS". The "FS", of course, was the entire point. I just sometimes click "post" before I'm done - I think you probably saw an early (incomplete) version of my post. |
Quote:
Code:
A simple printf statement looks like this: |
Hi,
Quote:
Quote:
|
woow.. thanks all for the replies.. :) :)
The mistake was fs instead of FS... |
You're welcome :)
|
I have one more problem....
How this command works ?? ----awk ‘a !~ $0; {a=$0}’----- when i run this command with say hello.txt file as an argument its eliminating all the duplicate entries.. its just working great.. but how is this working ??? how the task of parsing and comparing is just done with this simple command ???? and also.. if i want to write an AWK script to this i.e. for eliminating the duplicate lines in a file.. how to write... if i just write the above command in a file and run as -----awk -f <filename> hello.txt----- then its not working !!!! Thanks :) |
Hi,
Quote:
The print statement can be omitted, if it is awk will print the line (if appropriate). The command looks to see if the variable a is not equal to the current line. If so (not equal) it stores the new current line in a and prints that line. If not it skips that line and compares the next line. Quote:
Run as: awk -f filename hello.txt Example: Code:
$ cat filename |
oh.. thanks a lot again.. but
how this works then for non sorted inputs ??? -------awk ‘! a[$0]++’------ but i really wonder how the tool is designed so efficiently ??? !!! :) because for non sorted lines say i have number 1 to 100 in first line and then from 101st line i have 1 to 100 again.. so it has to maintain how many variable ? is this really efficient ? and also i tried putting this ! a[$0]++ in a file and run as awk -f file hello.txt and it worked fine.. :) please provide me with some inputs how this works ?? :) |
Hi,
An array is used in your latest example. The current line is only printed and added to the array if it isn't already stored in the array. You might have noticed already that omitting the ! will show only the duplicate lines. As in your previous example the print statement is assumed by awk. Hope this clears things up a bit. |
Pretty mush the same idea as the previous code just this time it is storing the value in an array.
Arrays in awk are associative by nature, ie they are all based on a string even when a digit is used. So each line is stored in the array and the value for the array increased by 1. Due to the not (!) at the beginning when the first value of the array equates to 0 it is now notted to be true and hence the value is printed. Hope that helps. |
yeah... my doubts are cleared.. thanks a lot for all replies.. :)
|
All times are GMT -5. The time now is 04:30 AM. |