Need help on AWK implementation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on AWK implementation
# 1  
Old 09-30-2009
Tools Need help on AWK implementation

Hi,

I am accepting a string from user. compare this output with the awk output as below...

Code:
echo "\n\n\tDay [01,02....]: \c"
read day
awk '{ if($day == $2) { if ($mon == $1)  { print "Yes" }}}' syslog.txt

I am getting the follwoing error

Code:
awk: Field $() is not correct.
 The input line number is 1. The file is syslog.txt.
 The source line number is 1.

Please suggest the changes and help on using nested if in awk.....

Thanks
# 2  
Old 09-30-2009
Something like this:

Code:
read day
read month

awk -v d=$day -v m=$month '$2==d && $1==m {print "Yes"}' syslog.txt

# 3  
Old 09-30-2009
Tools

Thanks for the suggestion...but it is not giving any output... mu code is somthing like this
Code:
echo "\n\n\tDay [01,02....]: \c"
read day
echo "\n\n\tMonth [Jan, Feb....]: \c"
read mon
cat /var/adm/syslog/syslog.log > syslog.txt
awk -v d=$day -v m=$month '($2==d && $1==m) {print "Yes"}' syslog.txt

syslog.txt is a redirected file which has following output.
Code:
Sep 21 08:54:07 powaihp syslogd: restart
Sep 21 08:54:07 powaihp vmunix:
.
.

I wish to extract first column and compare it with the user's input (mon)and also compare the day with the user's input (day)...... when both match display some message.....
Kindly help.....
# 4  
Old 09-30-2009
The name of the shell variable you used (mon) don't match with the name that you assign to the awk variable (month), it should be:

Code:
echo "\n\n\tDay [01,02....]: \c"
read day
echo "\n\n\tMonth [Jan, Feb....]: \c"
read month
cat /var/adm/syslog/syslog.log > syslog.txt
awk -v d=$day -v m=$month '($2==d && $1==m) {print "Yes"}' syslog.txt

# 5  
Old 10-01-2009
Great.......this is working...

Thank you very much for the suggetion and correction....

---------- Post updated 10-01-09 at 02:48 PM ---------- Previous update was 09-30-09 at 05:27 PM ----------

I am finding it difficult to print else part of the if condition above..

Can somebody help me with syntax for
Code:
 awk -v d=$day -v m=$month '($2==d && $1==m) {print "Yes"}' syslog.txt

As the above command prints yes when the condition is met, it must print No when the condition fails....

Thanks in advance...
# 6  
Old 10-01-2009
Something like this:

Code:
awk -v d=$day -v m=$month '
if( $2==d && $1==m ){
  print "Yes"
}
else {
  print "False"
}' syslog.txt

Or just:

Code:
awk -v d=$day -v m=$month '($2==d && $1==m){print "Yes";next}{print "False"}' syslog.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C: CSV implementation

I have this code from a programming book: #include <stdio.h> #include <string.h> char buf; /* input line buffer */ char* field; /* fields */ char* unquote( char* ); /* csvgetline: read and parse line, return field count */ /* sample input:... (3 Replies)
Discussion started by: totoro125
3 Replies

2. Shell Programming and Scripting

Back up implementation

Is there any command to take create back up of a file as soon as when it is created?If not is it possible to create something like that? (3 Replies)
Discussion started by: Sindhu R
3 Replies

3. UNIX for Advanced & Expert Users

Ipsec implementation

How can i implement Ipsec between two machines in linux_ ubuntu? any link?? suggestion?? (0 Replies)
Discussion started by: elinaz
0 Replies

4. UNIX for Dummies Questions & Answers

Lseek implementation

Hi everybody, i've been googling for ages now and gotten kinda desperate... The question, however, might be rather trivial for the experts: What is it exactly, i.e. physically, the POSIX function (for a file) "lseek" does? Does it trigger some kind of synchronization on disk? Is it just for the... (4 Replies)
Discussion started by: Humudituu
4 Replies

5. Linux

CAPWAP implementation

Hi I'm trying to implement CAPWAP protocol for my application.i'm able to configure my server side but i'm getting error at client(WTP) side as IOCTL error.while running the command #./WTP /mnt/cf/capwap/ : wlan2 Starting WTP... # WTP Loads... (0 Replies)
Discussion started by: ran789
0 Replies

6. Programming

Implementation of dup2

Hi all,I'm reading <Advanced programming in the UNIX environment>,that book asked the reader to implement a function which has same functions with dup2 without calling fcntl.Could anyone give me a tip?Any help will be appreciated.:) (8 Replies)
Discussion started by: homeboy
8 Replies

7. UNIX for Advanced & Expert Users

Malloc Implementation in C

Hey Guys Some of my friends have got together and we are trying to write a basic kernel similar to Linux. I am trying to implement the malloc function in C and I am using a doubly linked list as the primary data structure. I need to allocate memory for this link list (duh...) and I don't feel... (2 Replies)
Discussion started by: rbansal2
2 Replies

8. Programming

Malloc implementation in C

Hey Guys I am trying to implement the malloc function for my OS class and I am having a little trouble with it. I would be really grateful if I could get some hints on this problem. So I am using a doubly-linked list as my data structure and I have to allocate memory for it (duh...). The... (1 Reply)
Discussion started by: Gambit_b
1 Replies

9. UNIX for Advanced & Expert Users

Implementation of ls - i command

It is possible for me to obtain the Inode of the path name using ls -i <pathname> command Can anyone tell me how its implemented... (3 Replies)
Discussion started by: ganapathy.psgit
3 Replies

10. UNIX for Dummies Questions & Answers

Shell Implementation

I want to implement my own simple multi tasking shell in Unix which will take care of redirection (<, >, >>) and piping. I am just unable to get a concrete idea of how exactly I have to start. I have several books...some are.. 1. Maurice Bach- Design Of Unix Operating System 2. Richard... (3 Replies)
Discussion started by: clickonline1
3 Replies
Login or Register to Ask a Question