Questions tagged [mawk]

20 questions
15
votes
5 answers

How to print own script name in mawk?

In bash $0 contains the name of the script, but in awk if I make a script named myscript.awk with the following content: #!/usr/bin/awk -f BEGIN{ print ARGV[0] } and run it, it will only print "awk". Besides, ARGV[i] with i>0 is used only for…
cipper
  • 363
  • 2
  • 11
8
votes
4 answers

Why does regex `"\.pdf"` match `/.../pdf.../...` in gawk, but not in mawk?

From How can I extract only the pid column and only the pathname column in the lsof output? awk '{ for (i=9; i<=NF; i++) { if ($i ~ "string" && $1 != "wineserv" && $5 == "REG" && $NF ~ "\.pdf") { $1=$2=$3=$4=$5=$6=$7=$8="" print …
Tim
  • 98,580
  • 191
  • 570
  • 977
6
votes
2 answers

Portable check for array

Gawk has "isarray": if (isarray(x)) print "is array" else print "is scalar" However Mawk and "gawk --posix" do not: fatal: function 'isarray' not defined This can cause problems: x x[1] fatal: attempt to use scalar 'x' as an…
Zombo
  • 1
  • 5
  • 43
  • 62
5
votes
2 answers

Converting date timestamp from 12 hour into 24 using awk

I am working with a csv file which contains data in the following structure: "12345","BLAH","DEDA","0.000","1.111","2.22222","3.3333333,"15/12/2017 4:26:00 PM" I want to convert the 12 hour time into 24 hour time. The following shows what I am…
GustavMahler
  • 480
  • 5
  • 14
4
votes
2 answers

Number formatting and rounding issue with awk

I want to get the exact number when I try to find the average of a column of values. For example, this is the column of input…
rplee
  • 369
  • 3
  • 14
4
votes
1 answer

How to specify regex quantifiers with mawk?

I'm familiar with the concept of specified bounded regex quantifiers as follows: Quantifier Legend Example Sample Match {3} Exactly three times \D{3} ABC {2,4} Two to four times \d{2,4} 156 {3,} …
user332459
3
votes
1 answer

Walking Multidimensional arrays in Mawk

I'm able to do this properly in gawk, but when I tried to post my code to the machine where it will run, I realized it was using mawk... $ cat multidim.gawk # test of multidimensional arrays // { A[1][1]="A11" A[1][2]="A12" …
mgjk
  • 656
  • 1
  • 10
  • 22
3
votes
1 answer

Split out individual characters using the null string

I read this in the Gawk manual: GNU EXTENSIONS [...] The ability to split out individual characters using the null string as the value of FS, and as the third argument to split(). However this seems to not be the case. This works as expected: $…
Zombo
  • 1
  • 5
  • 43
  • 62
3
votes
2 answers

Combine two operations in one command - awk

I need to change - FROM Car Bus - TO Helicopter Airplane This two commands are sufficient. awk -i inplace '{sub(/Car/,"Helicopter")}1' file awk -i inplace '{sub(/Bus/,"Airplane")}1' file And this command is sufficient too. sed -e…
Sabrina
  • 31
  • 1
  • 2
2
votes
1 answer

Regular Expressions within a string in AWK using if/then control structure?

PROBLEM I want to specifically use AWK (GAWK, MAWK, whatever) in an AWK script to sum up the values in field 9 for every record that contains "runner__.jpx" (the underscores are placeholders for two digits 0-9) in field 6 and "good" in FIELD…
user46865
2
votes
1 answer

Is there a reference for the assembler-like output produced by `awk -W dump`?

The command awk -W dump ... produces a dump of "assembler-like" code (as shown here [link], or by reading the man). Some of the instructions are easy to guess at their meaning such as jz, which is produced by an if statement and is clearly a jump…
Allen
  • 206
  • 2
  • 12
1
vote
1 answer

Unexpected split behavior

I read about Awk split behavior here: [...] the fs argument to the split function (see String Functions) shall be interpreted as extended regular expressions. These can be either ERE tokens or arbitrary expressions, and shall be interpreted in…
Zombo
  • 1
  • 5
  • 43
  • 62
1
vote
1 answer

awk (mawk): regular expression compile failed (missing operand)

I want to use the following regex with awk to validate phone numbers: echo 012-3456-7890 | awk '/^\(?0[1-9]{2}\)?(| |-|.)[1-9][0-9]{3}( |-|.)[0-9]{4}$/ {print $0}' But I am getting the following error: awk: line 1: regular expression compile failed…
sci9
  • 517
  • 2
  • 7
  • 19
1
vote
2 answers

awk find lines with two matches

File contains Today is 1 nice day This is a perfect day Tomorrow is 1/2 cool 1/2 hot day What is Toy Story Tonight is a dark night How do I find lines starts with "To" and end with "day" None of these commands are working awk '/^To[*]day$/ {print}'…
Rina
  • 11
  • 2
1
vote
1 answer

awk - change value

File contains TIMEOUT=abc TIMEOUT=5 TIMEOUT= xTIMEOUT=5 I need to change from "TIMEOUT=5" to "TIMEOUT=9" This command works. But changes 4th line too, which is not desired awk '$0 ~ "TIMEOUT=[:0-9:]" { $0 = "TIMEOUT=9" } {print}' file And…
Shila
  • 11
  • 1
1
2