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 10-18-2011, 11:12 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
awk andvariable interpolation


Is there a way that I can interpolate variable in the begin statement in awk?

All i am doing here is piping the output through an ls -ltr command and checking for all files from todays date in a directory with awk.
Sure there is a way to do it in bash - but I am trying to figure out how to do it in awk.


Code:
#!/usr/bin/awk -f
BEGIN {
        FS=" ";
        mydate=`/bin/date +%Y-%m-%d`
        }
{ if ($6 ~ /mydate/) {
        print $6 , $8
        sleep 1;

        }
}

Last edited by casperdaghost; 10-18-2011 at 11:48 PM.
 
Old 10-19-2011, 12:43 AM   #2
ChrisCPearson
LQ Newbie
 
Registered: Aug 2010
Posts: 1

Rep: Reputation: 1
In AWK, to pipe the output of a command into a variable:
command | getline [var]
Here's your complete program:

Code:
#!/usr/bin/awk -f
BEGIN {
    "date +%Y-%m-%d" | getline today
}
$6 == today {
    print $6, $8
}
 
1 members found this post helpful.
Old 10-19-2011, 12:59 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by casperdaghost View Post
Is there a way that I can interpolate variable in the begin statement in awk?

All i am doing here is piping the output through an ls -ltr command and checking for all files from todays date in a directory with awk.
Sure there is a way to do it in bash - but I am trying to figure out how to do it in awk.


Code:
#!/usr/bin/awk -f
BEGIN {
        FS=" ";
        mydate=`/bin/date +%Y-%m-%d`
        }
{ if ($6 ~ /mydate/) {
        print $6 , $8
        sleep 1;

        }
}
Just a word of warning: if you have filenames w/ spaces in them
your awk output for $8 won't be quite what you expected.



Cheers,
Tink

Last edited by Tinkster; 10-19-2011 at 01:00 AM.
 
1 members found this post helpful.
Old 10-19-2011, 01:00 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by ChrisCPearson View Post
In AWK, to pipe the output of a command into a variable:
command | getline [var]
Here's your complete program:

Code:
#!/usr/bin/awk -f
BEGIN {
    "date +%Y-%m-%d" | getline today
}
$6 == today {
    print $6, $8
}

Hi, welcome to LQ,

And nice first post. ;}



Cheers,
Tink

Last edited by Tinkster; 10-19-2011 at 03:44 AM.
 
1 members found this post helpful.
Old 10-19-2011, 03:31 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
As per Tink's warning, you may have even been given this link before as why you should avoid
parsing ls.
 
1 members found this post helpful.
Old 10-19-2011, 08:57 AM   #6
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
Hey - great work guys.
 
  


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
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 10:06 PM
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 03:18 AM
bash for loop in perl - command interpolation casperdaghost Programming 6 03-19-2010 07:39 AM
Tick, jiffies and time interpolation Kirilla Linux - Kernel 1 02-27-2009 09:04 AM
Cubic interpolation leonidg Programming 5 03-06-2005 04:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:26 AM.

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