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
4
votes
1 answer

Executable awk script to output its name

I have a simple executable awk script which I'm trying to make to say its own name: $ cat script.awk #!/usr/bin/awk -f BEGIN { print "Hi, I'm " ARGV[0] } when executed: $ chmod u+x script.awk $ ./script.awk Hi, I'm awk Expected (well, desired…
James Brown
  • 36,089
  • 7
  • 43
  • 59
4
votes
2 answers

Dynamically populate multidimensional awk array

I'm working on an Awk/Gawk script that parses a file, populating a multidimensional array for each line. The first column is a period delimited string, with each value being a reference to the array key for the next level. The 2nd column is the…
Justin
  • 1,959
  • 5
  • 22
  • 40
4
votes
2 answers

Awk, gsub, ampersands and unexpected expansion

First, apologies for the potentially duplicate question. I'm new to bash scripting and I can't even figure out some keywords to search with. With that said, I tried to simplify problem description as much as I can: I have a text file (test.txt) that…
Remon
  • 43
  • 3
4
votes
3 answers

Regex "^[[:digit:]]$" not working as expected in AWK/GAWK

My GAWK version on RHEL is: gawk-3.1.5-15.el5 I wanted to print a line if the first field of it has all digits (no special characters, even space to be considered) Example: echo "123456789012345,3" | awk -F, '{if ($1 ~ /^[[:digit:]]$/) print…
dig_123
  • 2,240
  • 6
  • 35
  • 59
4
votes
2 answers

How to slice a variable into array indexes?

There is this typical problem: given a list of values, check if they are present in an array. In awk, the trick val in array does work pretty well. Hence, the typical idea is to store all the data in an array and then keep doing the check. For…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
4
votes
1 answer

How to check if a variable is an array?

I was playing with PROCINFO and its sorted_in index to be able to control the array transversal. Then I wondered what are the contents of PROCINFO, so I decided to go through it and print its values: $ awk 'BEGIN {for (i in PROCINFO) print i,…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
4
votes
3 answers

Gawk regexp to select sequence

sorry for the nth simple question on regexp but I'm not able to get what I need without a what seems to me a too complicated solution. I'm parsing a file containing sequence of only 3 letters A,E,D as in AADDEEDDA EEEEEEEE AEEEDEEA AEEEDDAAA and…
4
votes
1 answer

Awk, ^ backslash not last character on line

I am trying to use gawk in windows to parse a directory which contains CSV files for the blank rows in the second column in each file. I want to take the whole row where the the 2nd column is blank from ALL the source files and output to a csv. ..…
Cace
  • 51
  • 1
  • 4
4
votes
2 answers

normalize column data with maximum value of that column

I have a data file with two columns. I want to find out the maximum data value from the second column and divide each entries of second column witht he maximum value. (So I will get all the entries in second column <= 1.00). I tried with this…
Vijay
  • 965
  • 5
  • 13
  • 27
4
votes
3 answers

How to handle 3 files with awk?

Ok, so after spending 2 days, I am not able solve it and I am almost out of time now. It might be a very silly question, so please bear with me. My awk script does something like this: BEGIN{ n=50; i=n; } FNR==NR { # Read file-1, which…
Bhushan
  • 18,329
  • 31
  • 104
  • 137
4
votes
4 answers

Join multiple files in gawk

I have a large number of files (around 500). Each file contain two columns. The first column is same for every file. I want to join all the files into a single file using gawk. For example, File 1 a 123 b 221 c 904 File 2 a 298 b 230 …
4
votes
2 answers

Sorting Numerically with awk (gawk)

In an attempt to solve a question, I wrote the following gnu-awk script and ran into an issue with sort (should have read the manual first). From the manual: Because IGNORECASE affects string comparisons, the value of IGNORECASE also affects…
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
4
votes
1 answer

AWK script to print line with the largest number of fields

The script below displays the largest number of fields in twister.txt. awk '{if (NF > max) max = NF} END{print max}' twister.txt My question is, How do you display the line itself, which has the largest number of fields in twister.txt.
anansharm
  • 183
  • 2
  • 4
  • 13
4
votes
4 answers

Remove what follows Nth occurrence Using one-liners

I would like to remove what follows the forth occurrence of the character ":" in any field contains it. See the example: Input: 1 10975 A C 1/1:137,105:245:99:1007,102,0 0/1:219,27:248:20:222,0,20 1 19938 T TA ./. …
user1421408
  • 207
  • 2
  • 9
4
votes
1 answer

replace first occurrence of text using awk

The code below replaces the first occurrence of apple with banana. How do I achieve the same using awk / gawk? sed -i "0,/apple/s//banana/" myfile.txt
Santosh Pillai
  • 1,311
  • 1
  • 20
  • 31