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

Peek at next line, but don't consume it

getline reads in the next line and increments the NR counter by 1. After using getline, awk resumes work with the next line. This is the desired behavior in most cases. In my special situation I need only to peek the next line and depending on its…
Chris
  • 2,987
  • 2
  • 20
  • 21
12
votes
3 answers

awk print without a file

How to print using awk without a file. script.sh #!/bin/sh for i in {2..10};do awk '{printf("%.2f %.2f\n", '$i', '$i'*(log('$i'/('$i'-1))))}' done sh script.sh Desired output 2 value 3 value 4 value and so on value indicates the quantity…
Kay
  • 1,957
  • 2
  • 24
  • 46
12
votes
4 answers

How to use printf to print a character multiple times?

Using printf, one can print a character multiple times: $ printf "%0.s-" {1..5} ----- In awk I know that I can do something like: $ awk 'BEGIN {while (i++ < 5) printf "-"}' ----- But I wonder if awk's printf allows this as well. I went through the…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
12
votes
6 answers

convert a fixed width file from text to csv

I have a large data file in text format and I want to convert it to csv by specifying each column length. number of columns = 5 column length [4 2 5 1 1] sample observations: aasdfh9013512 ajshdj 2445df Expected Output aasd,fh,90135,1,2 ajsh,dj,…
Ashish
  • 441
  • 1
  • 5
  • 11
11
votes
7 answers

Printing long integers in awk

I have a pipe delimited feed file which has several fields. Since I only need a few, I thought of using awk to capture them for my testing purposes. However, I noticed that printf changes the value if I use "%d". It works fine if I use "%s". Feed…
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
11
votes
4 answers

GAWK Script - Print filename in BEGIN section

I am writing a gawk script that begins #!/bin/gawk -f BEGIN { print FILENAME } I am calling the file via ./script file1.html but the script just returns nothing. Any ideas?
jonseager
  • 317
  • 2
  • 8
11
votes
4 answers

SED or AWK replace all with patterns from another file

I am trying to do pattern replacement using SED script but its not working properly sample_content.txt…
Dhanabalan
  • 572
  • 5
  • 19
10
votes
7 answers

Sed replace pattern with line number

I need to replace the pattern ### with the current line number. I managed to Print in the next line with both AWK and SED. sed -n "/###/{p;=;}" file prints to the next line, without the p;, it replaces the whole line. sed -e "s/###/{=;}/g" file used…
ghaschel
  • 1,313
  • 3
  • 20
  • 41
9
votes
1 answer

awk FPAT variable: Working

I have been able to understand from the GNU page of GAWK that it can handle delimiters in data using the FPAT variable but I can't make through how this works. For a CSV file the FPAT value is: FPAT = "([^,]+)|(\"[^\"]+\")" Using the data:…
rahul
  • 561
  • 1
  • 5
  • 13
8
votes
2 answers

Is there an awk equivalent of INT_MIN and INT_MAX?

In C and Java, there are defined constants representing the maximum and minimum values an integer can hold. Are there such constants in awk? If so, what are their names? The awk manual indicates that awk can support arbitrary precision integer…
merlin2011
  • 71,677
  • 44
  • 195
  • 329
7
votes
2 answers

awk split() function uses regular expression or exact string constant?

If we have ip=192.168.0.1 and we call split(ip, myArray, "."), myArray will contains "192" at position 1, "168" at position 2, "0" at position 3 and "1" at position 4. My question is that why does awk not interpreted the "." as the "any character"…
7
votes
3 answers

Can RS be set "empty" to split string characters to records?

Is there a way in awk—gawk most likely—to set the record separator RS to empty value to process each character of a string as a separate record? Kind of like setting the FS to empty to separate each character in its own field: $ echo abc | awk -F ''…
James Brown
  • 36,089
  • 7
  • 43
  • 59
7
votes
1 answer

Are fields defined in the END block in AWK?

What would happen when using $1, $2 ... in the END block, like: awk '{print $3}END{print $1 $2}' I found that $1 and $2 retain the values from the last record. Is this behaviour guaranteed by the standard or is it implementation-specific?
Mostafa Alayesh
  • 111
  • 1
  • 9
7
votes
5 answers

Can I pass an array to awk using -v?

I would like to be able to pass an array variable to awk. I don't mean a shell array but a native awk one. I know I can pass scalar variables like this: awk -vfoo="1" 'NR==foo' file Can I use the same mechanism to define an awk array? Something…
terdon
  • 3,260
  • 5
  • 33
  • 57
7
votes
3 answers

Should I always use GAWK over AWK?

I see that all features of AWK are included in GAWK, besides using a system that doesn't have GAWK installed, is there ever a good reason I should use AWK versus GAWK? Does AWK have better performance over GAWK?
b w
  • 91
  • 1
  • 5
1
2
3
65 66