Questions tagged [gawk]

gawk is the GNU implementation of awk. The awk utility interprets a special-purpose programming language that makes it possible to handle simple data-reformatting jobs with just a few lines of code.

The awk utility interprets a special-purpose programming language that makes it possible to handle simple data-reformatting jobs with just a few lines of code.

Links

257 questions
73
votes
1 answer

Difference between gawk vs. awk

Trying to understand the differences between the two functions gawk vs. awk? When would one use gawk vs awk? Or are they the same in terms of usage? Also, could one provide an example?
chrisjlee
  • 8,283
  • 16
  • 49
  • 54
72
votes
5 answers

Skip the first 6 lines/rows in a text file with awk

How can I skip the first 6 lines/rows in a text file (input.txt) and process the rest with awk? The format of my awk script (program.awk) is: BEGIN { } { process here } END { } My text file is like this: 0 3 5 0.1 4.3 2.0 1.5 1.5 3.0 0.3…
amatek
  • 833
  • 1
  • 6
  • 7
18
votes
4 answers

how to print quote character in awk

I have a file str.txt with the following sample records. 31,2713810299,1,11-Aug-15 19:52:10 32,2713810833,1,11-Aug-15 21:36:18 Now I want to print output with awk as below. cat str.txt|awk -F, '{print substr("$4",1,9)}' - The output should…
user3548033
  • 593
  • 2
  • 12
  • 25
15
votes
5 answers

How could a sequence of random dates be generated, given year interval?

What is needed here is a command that generates six dates given a range of years (1987 to 2017). For example: 12/10/1987 30/04/1998 22/02/2014 17/08/2017 19/07/2011 14/05/2004 How it could be done, with sed, gawk, etc?
Raymond83
  • 183
  • 1
  • 9
12
votes
4 answers

awk assign to multiple variables at once

I'm trying to pull two numerical values out of a string and assign them to variables using awk (gawk is what I'm using specifically). I want to pull the major and minor version numbers out of a tmux version string into awk variables, e.g.: input:…
villapx
  • 652
  • 2
  • 6
  • 19
11
votes
1 answer

awk condition of true and false

I came across that if we use awk 0 inputfile, it will not print anything cause 0 means false of the condition. If we use awk 1 inputfile, it will print everything as 1 means true for each line awk interpret. If we use awk any_string inputfile, it…
sylye
  • 657
  • 1
  • 7
  • 13
10
votes
2 answers

Is there any way to use the tr command in place?

I want to remove all non-english alphanumerals from a file. tr -sc '[:alnum:][:punct:]' ' '
user199046
10
votes
3 answers

Separator between statements in awk

From Gawk's manual: When awk statements within one rule are short, you might want to put more than one of them on a line. This is accomplished by separating the statements with a semicolon (‘;’). This also applies to the rules themselves.…
Tim
  • 98,580
  • 191
  • 570
  • 977
10
votes
2 answers

gawk inplace and stdout

Is it possible to use gawk's -i inplace option and also print things to stdout? For example, if I wanted to update a file, and if there are any changes print the name of the file and the changed lines to stderr I could do something like find -type f…
Eric Renouf
  • 18,141
  • 4
  • 49
  • 65
9
votes
2 answers

Handling 3 Files using awk

Consider following files: file1: boo,8,1024 foo,7,2048 file2: foo,0,24,154 noo,0,10,561 file3: 24,154,7,1024,0 What I need is to go to File1 and check if $2==7; if true, take $1, $2 and $3 from File1; now I have to compare if $1 from File1 equal…
Eng7
  • 1,681
  • 3
  • 18
  • 38
8
votes
1 answer

Where is awklib?

I am working my way through GAWK: Effective AWK Programming: A User’s Guide for GNU Awk, Edition 5.0; March, 2019. I installed gawk using the following commands: sudo apt-get update sudo apt-get install gawk On page 44 of the book there is the…
Argent
  • 209
  • 1
  • 4
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
8
votes
2 answers

awk exit code if regular expression did not match

I want to get exit code 1 if the 4th column did not match the regular expression, but it seems that awk will return 0, even though the regular expression did not match. Any idea how to make awk return 1 if the regexp did not match? root@server:~#…
8
votes
3 answers

String replacement using a dictionary

What is a good way to do string replacements in a file using a dictionary with a lot of substituend-substituent pairs? And by a lot, I actually mean about 20 – not much, but many enough so that I want to organize them neatly. I kind of want to…
k.stm
  • 689
  • 1
  • 11
  • 24
7
votes
4 answers

Extract string from each line of a file

I have a file where each line contains a sentence where one word is found between the character > and <. For example: Martin went shopping at >Wallmart< and lost his wallet French food >tastes< great I am looking for a command to run from the…
ZakS
  • 285
  • 1
  • 4
  • 13
1
2 3
17 18