Questions tagged [gawk]

gawk (short for GNU awk) is a free implementation of awk with manifold useful extensions.

gawk (short for GNU awk) is a free implementation of awk with manifold useful extensions.

AWK is an interpreted programming language designed for text processing and typically used as a data extraction and reporting tool. It is a standard feature of most Unix-like operating systems.

Source: Wikipedia

See also .

Reference

981 questions
138
votes
10 answers

How to use multiple arguments for awk with a shebang (i.e. #!)?

I'd like to execute an gawk script with --re-interval using a shebang. The "naive" approach of #!/usr/bin/gawk --re-interval -f ... awk script goes here does not work, since gawk is called with the first argument "--re-interval -f" (not splitted…
Dr. Hans-Peter Störr
  • 25,298
  • 30
  • 102
  • 139
102
votes
13 answers

how to use sed, awk, or gawk to print only what is matched?

I see lots of examples and man pages on how to do things like search-and-replace using sed, awk, or gawk. But in my case, I have a regular expression that I want to run against a text file to extract a specific value. I don't want to do…
Stéphane
  • 19,459
  • 24
  • 95
  • 136
94
votes
4 answers

awk - concatenate two string variable and assign to a third

In awk, I have 2 fields: $1 and $2. They are both strings that I want to concatenate and assign to a variable.
user3738926
  • 1,178
  • 1
  • 10
  • 17
49
votes
2 answers

GNU awk: accessing captured groups in replacement text

This seems like it should be dirt simple, but the awk gensub/gsub/sub behavior has always been unclear to me, and now I just can't get it to do what the documentation says it should do (and what experience with a zillion other similar tools suggests…
Pointy
  • 405,095
  • 59
  • 585
  • 614
33
votes
5 answers

print every nth line into a row using gawk

I have a very huge file in which I need to obtain every nth line and print it into a row. My data: 1 937 4.320194 2 667 4.913314 3 934 1.783326 4 940 -0.299312 5 939 2.309559 6 936 3.229496 7 611 -1.41808 8 …
user1269741
  • 437
  • 1
  • 5
  • 7
30
votes
2 answers

Print all Fields with AWK separated by OFS

Is there a way to print all records separated by the OFS without typing out each column number. #Desired style of syntax, undesired result [kbrandt@glade: ~] echo "1 2 3 4" | gawk 'BEGIN { OFS=" :-( "}; {print $0}' 1 2 3 4 #Desired result,…
Kyle Brandt
  • 26,938
  • 37
  • 124
  • 165
27
votes
5 answers

Why does awk "not in" array work just like awk "in" array?

Here's an awk script that attempts to set difference of two files based on their first column: BEGIN{ OFS=FS="\t" file = ARGV[1] while (getline < file) Contained[$1] = $1 delete ARGV[1] } $1 not in Contained{ print…
merlin2011
  • 71,677
  • 44
  • 195
  • 329
26
votes
3 answers

Is it possible to append an item to an array in awk without specifying an index?

I realize that awk has associative arrays, but I wonder if there is an awk equivalent to this: http://php.net/manual/en/function.array-push.php The obvious workaround is to just say: array[$new_element] = $new_element However, this seems less…
merlin2011
  • 71,677
  • 44
  • 195
  • 329
23
votes
11 answers

Bash: Parse CSV with quotes, commas and newlines

Say I have the following csv file: id,message,time 123,"Sorry, This message has commas and newlines",2016-03-28T20:26:39 456,"It makes the problem non-trivial",2016-03-28T20:26:41 I want to write a bash command that will return only the time…
Jacob Horbulyk
  • 2,366
  • 5
  • 22
  • 34
22
votes
6 answers

Can field separator in awk encompass multiple characters?

Can I use a field separator consisting of multiple characters? Like I want to separate words which contain quotes and commas between them viz. "School","College","City" So here I want to set my FS to be ",". But I am getting funny results when I…
yudistrange
  • 515
  • 1
  • 3
  • 15
20
votes
2 answers

How to print awk's results with different colors for different fields?

This file has 3 fields. I wanted e.g. the first 2 fields in green, and the third in white (NB : black background), so I tried : awk '{print "\033[0;32m"$1"\033[0m", "\033[0;32m"$2"\033[0m", "\033[0;37m"$3"\033[0m"} }' chrono.txt and everything was…
ThG
  • 2,361
  • 4
  • 22
  • 33
18
votes
6 answers

Sort associative array with AWK

Here's my array (gawk script) : myArray["peter"] = 32 myArray["bob"] = 5 myArray["john"] = 463 myArray["jack"] = 11 After sort, I need the following result : bob 5 jack 11 peter 32 john 463 When i use "asort", indices are lost. How to sort…
Phil
  • 478
  • 1
  • 4
  • 12
18
votes
3 answers

Using linux command "sort -f | uniq -i" together for ignoring case

I am trying to find unique and duplicate data in a list of data with two columns. I really just want to compare the data in column 1. The data might look like this (separated by a tab): What are you doing? Che cosa stai facendo? WHAT ARE YOU…
Steve3p0
  • 2,332
  • 5
  • 22
  • 30
17
votes
2 answers

single space as field separator with awk

I am dealing with a file where fields are separated by a single space. awk interprets the FS " " as "one or more whitespace", which misreads my file when one of the fields is empty. I tried using "a space not followed by a space"( " (?! )" ) as FS…
asachet
  • 6,620
  • 2
  • 30
  • 74
14
votes
9 answers

Parsing a CSV file using gawk

How do you parse a CSV file using gawk? Simply setting FS="," is not enough, as a quoted field with a comma inside will be treated as multiple fields. Example using FS="," which does not work: file contents: one,two,"three, four",five "six,…
MCS
  • 22,113
  • 20
  • 62
  • 76
1
2 3
65 66