DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 6th July 2011
Oko's Avatar
Oko Oko is offline
Fsck Surgeon
 
Join Date: May 2008
Location: Kosovo, Serbia
Posts: 764
Thanked 36 Times in 32 Posts
Default AWK passing the argument to system command

I was wondering if somebody could tell me if there is a way to pass argument to a system command in AWK. Namely, let suppose that I want to print calendar for September-November of 2011. I can do that with

Code:
#!/usr/bin/awk -f

BEGIN {FS=" "; OFS=""}
{
        system("cal 8 2011")
        system("cal 9 2011")
        system("cal 10 2011")
        system("cal 11 2011")
}
However I would like to be able to do something like
Code:
#!/usr/bin/awk -f

BEGIN {FS=" "; OFS=""}
{
{ for (i = 8; i <= 11; i++)
        system("cal $i 2011")
}
}
I also tried

Code:
#!/usr/bin/awk -f

BEGIN {FS=" "; OFS=""}
{
{ for (i = 8; i <= 11; i++)
        system("cal"  i "2011")
}
}
but no luck.

Anybody knows how to do this?

P.S. I am not interesting in the shell script or anything else. I want to do this with AWK.
Reply With Quote
  #2   (View Single Post)  
Old 6th July 2011
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,052
Thanked 118 Times in 93 Posts
Default

Code:
#!/usr/bin/awk -f

BEGIN {
  for (i=8; i<12; i++)
    system("cal" " " i " " 2011)
}
... or ...
Code:
#!/usr/bin/awk -f

BEGIN {
  for (i=8; i<12; i++) {
    while("cal" " " i " " 2011 | getline)
      print $0
  }
}
__________________
religions, worst damnation of mankind
"If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds

Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”.
vermaden's: links resources deviantart spreadbsd
Reply With Quote
  #3   (View Single Post)  
Old 6th July 2011
s0xxx's Avatar
s0xxx s0xxx is offline
Package Pilot
 
Join Date: May 2008
Posts: 194
Thanked 47 Times in 22 Posts
Default

If your numbers change often, you could make it an alias in .kshrc (or whatever shell you're using) as:
Code:
alias ca="awk 'BEGIN{for(i=ARGV[1]; i<ARGV[2]; i++) print \"cal \"i\" 2011\"}'"
That char escaping is needed. And then call it, for example, as:
Code:
$ ca 5 12
Apart from using awk's ARGV array, there is also "-v" option on (POSIX) compliant awk's for passing of shell variables to awk:

Code:
$ cat <<eof>test.awk
> #!/usr/bin/awk -f
>
> BEGIN{
> for(i=a; i<=b; i++) print "cal "i" 2011"}
> eof

$ ./test.awk -va=8 -vb=12
cal 8 2011
cal 9 2011
cal 10 2011
cal 11 2011
cal 12 2011
Cheers
__________________
The best way to learn UNIX is to play with it, and the harder you play, the more you learn.
If you play hard enough, you'll break something for sure, and having to fix a badly broken system is arguably the fastest way of all to learn. -Michael Lucas, AbsoluteBSD
Reply With Quote
  #4   (View Single Post)  
Old 6th July 2011
Oko's Avatar
Oko Oko is offline
Fsck Surgeon
 
Join Date: May 2008
Location: Kosovo, Serbia
Posts: 764
Thanked 36 Times in 32 Posts
Default

Guys thank you so much. I realized how stupid I was as soon as I saw vermaden's answer. I cooked this based on your answers.

Code:
# This AWK script is written to automate generating of course calendar
# used for my syllabi
       
       
BEGIN {FS=" "; OFS="";
print "This program prints TeX calendar for the range of the months for the given year";
printf ("%s", "What is the year for which you want calendar? ") > "/dev/tty"
getline year < "/dev/tty";
printf ("%s", "Enter the first month for which you want calendar as number [1-12].  ") > "/dev/tty";
getline first_month < "/dev/tty";
printf ("%s", "Enter the last month for which you want calendar as number [1-12].  ") > "/dev/tty";
getline last_month < "/dev/tty";
{for (i=first_month; i<=last_month; i++)
        system("cal" " " i " " year)}   
}
{}
My goal is to automatically generate the snippet of TeX code which I use in my Syllabi. Something like follows just without lesson number (for examle without \tiny{6.6})
Code:
\begin{center}
\begin{small}
\begin{tabular}{||l|l|l|l|l||l|l|l|l|l||}
\hline
\multicolumn{5}{||c||}{January}&\multicolumn{5}{c||}{February}\\
\hline
\begin{minipage}{1cm}{\begin{center}Mon\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Tue\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Wed\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Thu\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Fri\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Mon\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Tue\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Wed\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Thu\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Fri\end{center}}\end{minipage}\\
\hline
\tiny{3} & \tiny{4} & \tiny{5} & \tiny{6} & \tiny{7} &
\tiny{} & \tiny{1}  & \tiny{2} & \tiny{3} & \tiny{4} \\
        &          & \tiny{Intro}&        &            &
        &          &\tiny{1.4, 1.5} &          &        \\
        &          &\tiny{R.2, R.4} &          &          &
        &          &           &          &          \\
\hline
\tiny{10} & \tiny{11} & \tiny{12} & \tiny{13} & \tiny{14} &
\tiny{7} & \tiny{8}  & \tiny{9} & \tiny{10} & \tiny{11} \\
\tiny{R.4, R.5}&     &\tiny{R.7} &          &            &
\tiny{1,6, 1.7}     &     & \tiny{2.1, 2.2} &         &          \\
        &                     &          &          &          &
        &                     &          &          &          \\
\hline
\tiny{17} & \tiny{18} & \tiny{19} & \tiny{20} & \tiny{21} &
\tiny{14} & \tiny{15}  & \tiny{16} & \tiny{17} & \tiny{18} \\
\tiny{MLK} &        &\tiny{R.8}  &       &                 &
\tiny{2.3} &        & \tiny{2.5}   &         &             \\
\tiny{Day} &                       &          &            &       &
        &              &          &           &       \\
\hline
\tiny{24} & \tiny{25} & \tiny{26} & \tiny{27} & \tiny{28} &
\tiny{21} & \tiny{22}  & \tiny{23} & \tiny{24} & \tiny{25} \\
\tiny{1.1, 1.2} &       &\tiny{1.2, 1.3} &           &        &
\tiny{3.1, 3.2} &       &\tiny{Review}       &        &       \\
        &                     &          &            &       &
        &                    &          &             &      \\
\hline
\tiny{31} & \tiny{} & \tiny{} & \tiny{} & \tiny{} &
\tiny{28} & \tiny{}  & \tiny{} & \tiny{} & \tiny{} \\
\tiny{\bf Exam 1} &           &       &         &       &
\tiny{\bf Exam 2}   &          &    &         &      \\
        &          &          &        &     &
        &          &          &          &          \\
\hline
\multicolumn{5}{||c||}{March}&\multicolumn{5}{c||}{April}\\
\hline
\begin{minipage}{1cm}{\begin{center}Mon\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Tue\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Wed\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Thu\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Fri\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Mon\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Tue\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Wed\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Thu\end{center}}\end{minipage}&
\begin{minipage}{1cm}{\begin{center}Fri\end{center}}\end{minipage}\\
\hline
\tiny{} & \tiny{1} & \tiny{2} & \tiny{3} & \tiny{4} &
\tiny{} & \tiny{}  & \tiny{} & \tiny{} & \tiny{1} \\
        & \tiny{Midterm}&\tiny{3.4, 4.1}&   &      &
        &          &          &        &          \\
        &          &          &         &          &
        &          &          &         &          \\
\hline
\tiny{7} & \tiny{8} & \tiny{9} & \tiny{10} & \tiny{11} &
\tiny{4} & \tiny{5}  & \tiny{6} & \tiny{7} & \tiny{8} \\
\tiny{4.3, 4.5}&          &\tiny{5.4} &          &   &
\tiny{No} & \tiny{No}&\tiny{No}  &\tiny{No} & \tiny{No}\\
        &                     &          &          &          &
\tiny{Classes}& \tiny{Classes}& \tiny{Classes}& \tiny{Classes}&
\tiny{Classes}\\
\hline
\tiny{14} & \tiny{15} & \tiny{16} & \tiny{17} & \tiny{18} &
\tiny{11} & \tiny{12}  & \tiny{13} & \tiny{14} & \tiny{15} \\
\tiny{6.1, 6.2}&     &     \tiny{6.3} &       &      &
\tiny{Review} &      & \tiny{\bf Exam 3} &       & \\
        &              &          &            &       &
        &              &          &            &       \\
\hline
\tiny{21} & \tiny{22} & \tiny{23} & \tiny{24} & \tiny{25} &
\tiny{18} & \tiny{19}  & \tiny{20} & \tiny{21} & \tiny{22} \\
\tiny{6.4}&          &\tiny{6.5}   &      &         &
\tiny{8.1}&     & \tiny{Review}     &      &     \\
        &       &       &        &       &
        &       &       &     &\\
\hline
\tiny{28} & \tiny{29} & \tiny{30} & \tiny{31} & \tiny{} &
\tiny{25} & \tiny{26}  & \tiny{27} & \tiny{28} & \tiny{29} \\
\tiny{6.6}&          &\tiny{6.7} &        &      &
\tiny{Review}&        &\tiny{Review}&        &      \\
        &          &             &        &     &
        &          &             &        &          \\
\hline
\end{tabular}
\end{small}
\end{center}
Reply With Quote
  #5   (View Single Post)  
Old 6th July 2011
vermaden's Avatar
vermaden vermaden is offline
Administrator
 
Join Date: Apr 2008
Location: pl_PL.lodz
Posts: 1,052
Thanked 118 Times in 93 Posts
Default

Welcome mate
__________________
religions, worst damnation of mankind
"If 386BSD had been available when I started on Linux, Linux would probably never had happened." Linus Torvalds

Linux is not UNIX! Face it! It is not an insult. It is fact: GNU is a recursive acronym for “GNU's Not UNIX”.
vermaden's: links resources deviantart spreadbsd
Reply With Quote
  #6   (View Single Post)  
Old 6th July 2011
IdOp's Avatar
IdOp IdOp is offline
Too dumb for a smartphone
 
Join Date: May 2008
Location: twisting on the daemon's fork(2)
Posts: 557
Thanked 14 Times in 13 Posts
Default

A bit late to the party, but awk's sprintf() can be useful for this sort of thing,

Code:
for( j=9; j<12; j++ ) { system( sprintf( "cal %d 2011 ", j ) ) }
Reply With Quote
  #7   (View Single Post)  
Old 6th July 2011
Oko's Avatar
Oko Oko is offline
Fsck Surgeon
 
Join Date: May 2008
Location: Kosovo, Serbia
Posts: 764
Thanked 36 Times in 32 Posts
Default

Quote:
Originally Posted by IdOp View Post
A bit late to the party, but awk's sprintf() can be useful for this sort of thing,

Code:
for( j=9; j<12; j++ ) { system( sprintf( "cal %d 2011 ", j ) ) }
It is never too late Thank you so much!!! I love playing with AWK
Reply With Quote
Reply

Thread Tools
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
mount-msdosfs /dev/dao: : Invalid argument harold FreeBSD General 5 12th December 2010 06:59 AM
problem passing /dev/fd/n to programs TerryP Programming 2 15th April 2009 08:24 AM
Mount DVD - invalid argument. maxrussell FreeBSD General 17 7th January 2009 08:46 AM
Scripted sysinstall fails with "Command 'system' failed" PeterSteele FreeBSD Installation and Upgrading 0 13th November 2008 11:31 PM
passing arg from bootloadder to kernel l2fl2f FreeBSD General 16 15th May 2008 12:38 PM


All times are GMT. The time now is 07:20 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick