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
7
votes
3 answers

gawk / awk: piping date to getline *sometimes* won't work

I'm attempting to convert dates from one format to another: From e.g. "October 29, 2005" to 2005-10-29. I have a list of 625 dates. I use Awk. The conversion works -- most of the time. Hovewer, sometimes the conversion won't happen at all, and the…
KajMagnus
  • 11,308
  • 15
  • 79
  • 127
7
votes
5 answers

gawk FS to split record into individual characters

If the field separator is the empty string, each character becomes a separate field $ echo hello | awk -F '' -v OFS=, '{$1 = NF OFS $1} 1' 5,h,e,l,l,o However, if FS is a regex that can possibly match zero times, the same behaviour does not…
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
7
votes
1 answer

multidimensional arrays in awk

I tried creating a pseudo-multidimensional array in awk. # Calculate cumulative context score BEGIN { FS=OFS="\t" } { a[$2+FS+$7,$3]+=$6 } END { for (i,j) in a { print i,j,a[i,j] } } Output: awk: ccstscan.awk:9: END { for…
WYSIWYG
  • 494
  • 6
  • 23
6
votes
2 answers

Replacing two strings using awk

I want to replace @@ with ^ and ¤¤ with a newline in a file. To do this I wrote the code below, but it feels like there is a more elegant solution then calling gawk twice. Can anyone tell me if there is one? cat test.txt | gawk '{ gsub("@@", "^");…
rickythefox
  • 6,601
  • 6
  • 40
  • 62
6
votes
3 answers

AWK/GAWK performance

I have a 84 million-line XML that I am processing with 'gawk' in Red Hat Linux. (OK, some people would recommend to use other tools rather than GAWK, but my XML doesn't have multiline tags or any other peculiarities that make GAWK not a good choice…
Mike Duke
  • 101
  • 1
  • 6
6
votes
3 answers

Split a string directly into array

Suppose I want to pass a string to awk so that once I split it (on a pattern) the substrings become the indexes (not the values) of an associative array. Like so: $ awk -v s="A:B:F:G" 'BEGIN{ # easy, but can these steps be combined? …
dawg
  • 98,345
  • 23
  • 131
  • 206
6
votes
1 answer

awk: function to escape regex operators from a string

Need a function to escape a string containing regex expression operators in an awk script. I came across this 'ugly' solution: function escape_string( str ) { gsub( /\\/, "\\\\", str ); gsub( /\./, "\\.", str ); gsub( /\^/, "\\^", str…
Lacobus
  • 1,590
  • 12
  • 20
6
votes
2 answers

How to skip a directory in awk?

Say I have the following structure of files and directories: $ tree . ├── a ├── b └── dir └── c 1 directory, 3 files That is, two files a and b together with a dir dir, where another file c stands. I want to process all the files with awk (GNU…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
6
votes
6 answers

Can awk skip files which do not exist, race-free?

Is there a way to make awk (gawk) ignore or skip missing files? That is, files passed on the command line that no longer exist in the file system (e.g. rapidly appearing/disappearing files under /proc/[1-9]*). By default, a missing file is a fatal…
shambolic
6
votes
3 answers

The differences between gawk and mawk (column width)

I have a file: To jest długi string z wieloma polskimi literami ąółżęś kodowany w UTF8, żeby było śmieszniej, haha. ą a Example gawk: gawk '{printf "%-80s %-s\n", $0, length}' file In gawk, I get the correct result: To jest długi string z…
Tedee12345
  • 1,182
  • 4
  • 16
  • 26
6
votes
2 answers

Timestamp to Epoch in a CSV file with GAWK

Looking to convert human readable timestamps to epoch/Unix time within a CSV file using GAWK in preparation for loading into a MySQL DB. Data Example: {null};2013-11-26;Text & Device;Location;/file/path/to/;Tuesday, November 26 12:17…
Keiron
  • 117
  • 1
  • 8
6
votes
4 answers

Separating output records in AWK without a trailing separator

I have the following records: 31 Stockholm 42 Talin 34 Helsinki 24 Moscow 15 Tokyo And I want to convert it to JSON with AWK. Using this code: #!/usr/bin/awk BEGIN { print "{"; FS=" "; ORS=",\n"; OFS=":"; }; { if (…
AlexStack
  • 16,766
  • 21
  • 72
  • 104
6
votes
1 answer

Change regex delimiter in awk patterns

Is is possible to change the default regex delimiter (slash) to other characters? I tried to do it using with sed syntax but it didn't work. $ gawk '\|bash| { print } ' backup.sh gawk: |bash| { print } gawk: ^ syntax error The regex I am trying has…
Anvesh
  • 395
  • 1
  • 2
  • 10
5
votes
3 answers

Simple trouble with awk and regex

echo xx y11y rrr | awk '{ if ($2 ~/y[1-5]{2}y/) print $3}' Why I cannot get any output? Thank you.
znlyj
  • 1,109
  • 3
  • 14
  • 34
5
votes
3 answers

Concatenate two columns of a text file

I have a tsv file like 1 2 3 4 5 ... a b c d e ... x y z j k ... How can I merge two contiguous columns, say the 2nd and the 3rd, to get 1 2-3 4 5 ... a b-c d e ... x y-z j k ... I need the…
Arch Stanton
  • 382
  • 5
  • 14
1 2
3
65 66