LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   problem with awk printf (https://www.linuxquestions.org/questions/linux-newbie-8/problem-with-awk-printf-846991/)

culin 11-28-2010 02:36 AM

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
:(

druuna 11-28-2010 02:52 AM

Hi,

This should work:

Code:

awk 'BEGIN { FS="-"
  printf("Enter the date in mm-dd-yyyy format: ")
}
{
  printf("Date is %d, Month is %d, Year is %d" ,$2, $1,$3)
}'

- It should be FS instead of 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.

paulsm4 11-28-2010 02:59 AM

Hi -

Let's break it down, shall we?
Code:

echo '11-28-2010' | \
 awk '{printf "Date: %d, Month: %d, Year: %d\n", $2, $1, $3}'

Date: 0, Month: 11, Year: 0

<= OK, we're just seeing "11". That's the problem we want to fix.

Code:

echo '11-28-2010' |  awk '{print $1 "," $2 "," $3}'

11-28-2010,,

<= The REASON is that we're only getting one ARGUMENT
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}'

11,28,2010

<= Here's the syntax you need:
"BEGIN {}", with a capital "FS"; followed by your "{printf xyz}" clause

grail 11-28-2010 03:10 AM

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.

paulsm4 11-28-2010 03:22 AM

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.

colucix 11-28-2010 03:35 AM

Quote:

Originally Posted by druuna (Post 4173297)
- The printf syntax you use is not correct.

Actually both are correct, with and without parentheses. From the GNU awk manual:
Code:

A simple printf statement looks like this:

    printf format, item1, item2, ...

The entire list of arguments may optionally be enclosed in parentheses.
The parentheses are necessary if any of the item expressions use the
‘>’ relational operator; otherwise, it can be confused with a redirection.

:)

druuna 11-28-2010 03:47 AM

Hi,
Quote:

Originally Posted by colucix (Post 4173325)
Actually both are correct, with and without parentheses. From the GNU awk manual:

You are correct (of course). From the SED/AWK book:
Quote:

The full syntax of the printf statement has two parts:

printf ( format-expression [, arguments] )

The parentheses are optional. .......
I learned myself always to use the parentheses, sorry for the possible confusion.

culin 11-28-2010 03:52 AM

woow.. thanks all for the replies.. :) :)
The mistake was fs instead of FS...

druuna 11-28-2010 03:54 AM

You're welcome :)

culin 11-28-2010 03:56 AM

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 :)

druuna 11-28-2010 04:08 AM

Hi,

Quote:

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 ????
That only works on an already sorted file.

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:

file and run as -----awk -f <filename> hello.txt----- then its not working !!!!
Content of filename: a !~ $0; {a=$0}

Run as: awk -f filename hello.txt

Example:
Code:

$ cat filename
a !~ $0; {a=$0}

$ cat hello.txt
1
1
2
2
3
3
4
5

$ awk -f filename hello.txt
1
2
3
4
5

Hope this helps.

culin 11-28-2010 04:43 AM

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 ?? :)

druuna 11-28-2010 04:56 AM

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.

grail 11-28-2010 04:58 AM

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.

culin 11-28-2010 05:08 AM

yeah... my doubts are cleared.. thanks a lot for all replies.. :)


All times are GMT -5. The time now is 04:30 AM.