LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 02-15-2023, 03:49 PM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
Writing multiple awk programs in a single file


Have been writing some awk scripts. For each type of file I have a different implementation for what needs to do. Would it be possible to have everything in a single file, and if so, can I see an exampled of how such a thing can be done?

Currently I am using different awk files.
 
Old 02-15-2023, 08:20 PM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,349
Blog Entries: 3

Rep: Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766
It depends on the specific AWK scripts which you have and how you decide to merge them but in principle it can be done.

I'd write some example but LQ is blocking it with some javascript:

Code:
 Icon for www.linuxquestions.org www.linuxquestions.org
Checking if the site connection is secure
Enable JavaScript and cookies to continue
www.linuxquestions.org needs to review the security of your connection before proceeding.
The example would have been above and started with #!/usr/bin/awk -f as the shebang.

Client-side processing is inappropriate on so many levels.
 
Old 02-15-2023, 09:34 PM   #3
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,152
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
I can't post this at all with a Qt5/Qt6webengine browser. I can post this using webkit2gtk.

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

BEGIN { printf "%s\n","This is an awk script test." }
Example2.awk
Code:
#!/usr/bin/awk -f 
 
BEGIN {
    x=0

    do {
        print x;
        x+=1;
    }
    while(x<=10)
}
Just something that I noticed.
 
1 members found this post helpful.
Old 02-16-2023, 01:01 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,033

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
Using more than one file is ok. I mean putting everything into one file is not necessary, but if you want you can do that.
Code:
CMD1='BEGIN {
    x=0

    do {
        print x;
        x+=1;
    }
    while(x<=10)
}'
CMD2='BEGIN { printf "%s\n","This is an awk script test." }'
CMD3='.....'

awk "${CMD1}" filename
awk "${CMD2}" oth_file
....
 
Old 02-16-2023, 02:22 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,830

Rep: Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216
There is an awk variable FILENAME
Code:
awk '
# FNR == Line number per file
FNR == 1 { print "Processing line #1 of", FILENAME }
' filea fileb
You can branch on a FILENAME .ext like this:
Code:
awk '
FILENAME != pfn {
  ext=pfn=FILENAME
  sub(/.*[.]/, "", ext)
}
{
  if (ext == "e1") {
    print "Do e1 action on", FILENAME, "line", FNR
  } else if (ext == "e2") {
    print "Do e2 action on", FILENAME, "line", FNR
  } else {
    print "Do other action on", FILENAME, "line", FNR
  }
}
' filea.e1 fileb.e2 filec.e3
But it's not efficient, because it must test/branch for each input line.

Last edited by MadeInGermany; 02-16-2023 at 02:38 AM.
 
Old 02-16-2023, 02:31 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,830

Rep: Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216
More efficient: keep your different awk scripts and test/branch in the calling shell script.
Code:
#!/bin/sh
for fn in filea.e1 fileb.e2 filec.e3
do
  case ${fn##*.} in
  ( e1 ) echo "run awk.e1 on $fn"
  ;;
  ( e2 ) echo "run awk.e2 on $fn"
  ;;
  ( * ) echo "do other action on $fn"
  esac
done
 
Old 02-16-2023, 06:59 AM   #7
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
@MadeInGermany The more efficient version is what I currently have (separate files for different purposes).
I could not improve on the aforementioned strategy. It is a shame that awk and sed suffer such limitations.
In shell scripts one can conveniently use functions for different tasks. Although with awk one can specify
function, they are not identical to bash functions.
 
Old 02-16-2023, 07:52 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,830

Rep: Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216
I do not see your problem.

You can bundle common awk functions in a file, like this:
Code:
awk -f functions.awk -f e1.awk filea.e1
awk -f functions.awk -f e2.awk fileb.e2
 
1 members found this post helpful.
Old 02-16-2023, 08:34 AM   #9
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Can one put BEGIN clauses in functions and whatever? The -f option reads the awk program source from source-file, rather than using a specific function inside an awk file. In a bash script for instance, one can call a specific function, which itself can take positional arguments. And also be able to call a different bash funcion doing completely different things in the same bash file. I cannot see that I can do the same thing for awk files.

One still ends up for multiple files, each of which is quite short. Right? Then it is much easier to just call awk commands from bash without the need to put the awk code in separate awk files.

Last edited by Faki; 02-16-2023 at 08:44 AM.
 
Old 02-16-2023, 09:16 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,830

Rep: Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216
Sure awk has got functions.
Example:
Code:
function abs(num) {
  return (num < 0 ? -num : num)
}

{
  print abs($1), abs($2)
}
Other examples:
https://www.gnu.org/software/gawk/ma...n-Example.html

Last edited by MadeInGermany; 02-16-2023 at 09:19 AM.
 
Old 02-16-2023, 10:23 AM   #11
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
@MadeInGermany Sure, one can use make awk functions and call them within an awk scripts. But can one access specific awk functions from bash? Can one include different BEGIN and END clauses in different awk functions. And can one write patterns and actions in awk functions?

Last edited by Faki; 02-16-2023 at 11:47 AM.
 
Old 02-16-2023, 11:06 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,033

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
Quote:
Originally Posted by Faki View Post
@MadeInGermany Suce, one can use make awk functions and call them within an awk scripts. But can one access specific awk functions from bash? Can one include different BEGIN and END clauses in different awk functions. And can one write patterns and actions in awk functions?
Actually I don't really understand it. awk and bash are two different languages. You can start any number of awk scripts from bash and also you can start any number of scripts from awk too. But you cannot invoke a function written in awk directly in bash.
In such cases you can put your awk scripts into different files (or variables) and run (execute) the one you need. But I think you need to use only one language to implement a functionality, mixing two (or more) will make things definitely harder.
bash is good enough in a lot of cases, but cannot do calculations very well.
 
Old 02-16-2023, 11:34 AM   #13
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,830

Rep: Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216
awk and shell are different languages. They don't share anything (e.g. variables and functions are separate).
The shell has good interfaces to the processes that it runs.

BEGIN and END can run functions.
A function cannot contain BEGIN or END, why should it??

Do you mean
pattern {action}
?
You can always have an if clause in an action block:
{if (pattern) {further action}}
regardless if the action block is in the main (input loop) section or in a BEGIN or END or function.
 
Old 02-16-2023, 12:05 PM   #14
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
I have been scrutinising the possibility of writing various awk functionalities without actually requiring a separate awk file for each task. Bash scripts seem to be much more useful.

Thank you for your statement regarding the non-permissibility of having BEGIN or END clauses in awk functions. My reason for my question was the possibility of having multiple BEGIN clauses in the same file with an option to do a particular one. This to avoid having a separate awk file. My awk files for the different things I do are very small, few tens of lines and was looking forward to the task of putting them together in a single file.

From the discussion, I would be better served using small separate files as I am currently doing. Would using positional argument options possibly handle different BEGIN clauses in an awk file?
 
Old 02-16-2023, 12:37 PM   #15
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
The problem with

Code:
pattern {action}
is that the pattern is always tested. Suppose I do not want the pattern to be tested in certain situations. Could I avoid testing for pattern? Could I just branch so it does nothing?
 
  


Reply

Tags
awk



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] sed inside awk or awk inside awk maddyfreaks Linux - Newbie 4 06-29-2016 01:10 PM
[SOLVED] AWK: how to process data multiple times in awk pix9 Programming 11 04-24-2014 07:31 AM
[SOLVED] Once again... awk.. awk... awk shivaa Linux - Newbie 13 12-31-2012 04:56 AM
BASH or AWK: extract columns in multiple files and combine to a single file cristalp Programming 2 03-15-2012 11:55 AM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 07:23 PM

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

All times are GMT -5. The time now is 03:07 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
Open Source Consulting | Domain Registration