LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-28-2010, 03:36 AM   #1
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Rep: Reputation: 32
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
 
Old 11-28-2010, 03:52 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.

Last edited by druuna; 11-28-2010 at 04:53 AM. Reason: Fixed a mistake about printf syntax
 
1 members found this post helpful.
Old 11-28-2010, 03:59 AM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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

Last edited by paulsm4; 11-28-2010 at 04:08 AM.
 
1 members found this post helpful.
Old 11-28-2010, 04:10 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
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.
 
Old 11-28-2010, 04:22 AM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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.
 
Old 11-28-2010, 04:35 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by druuna View Post
- 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.
 
Old 11-28-2010, 04:47 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,
Quote:
Originally Posted by colucix View Post
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.
 
Old 11-28-2010, 04:52 AM   #8
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
woow.. thanks all for the replies..
The mistake was fs instead of FS...
 
Old 11-28-2010, 04:54 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You're welcome
 
Old 11-28-2010, 04:56 AM   #10
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
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
 
Old 11-28-2010, 05:08 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
 
Old 11-28-2010, 05:43 AM   #12
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
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 ??

Last edited by culin; 11-28-2010 at 05:46 AM.
 
Old 11-28-2010, 05:56 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
 
1 members found this post helpful.
Old 11-28-2010, 05:58 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
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.
 
1 members found this post helpful.
Old 11-28-2010, 06:08 AM   #15
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
yeah... my doubts are cleared.. thanks a lot for all replies..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk printf ignoring '\n' MrUmunhum Linux - General 2 01-25-2010 03:00 PM
printf \b not working as expected in AWK Rockydell Programming 5 11-13-2009 08:28 AM
printf doesn't work in awk MheAd Programming 4 09-24-2008 02:46 PM
printf problem??? Mistro116@yahoo.com Programming 2 11-13-2005 09:45 PM
need help w/ awk & printf rickenbacherus Programming 3 05-13-2004 08:21 AM


All times are GMT -5. The time now is 07:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration