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

Shell variable interpreted wrongly in awk

In following code I am trying to pass shell varibale to awk. But when I try to run it as a.sh foo_bar the output printed is "foo is not declared" and when I run it as a.sh bar_bar the output printed is " foo is declared" . Is there a bug in awk or…
Anil Rana
  • 33
  • 2
3
votes
3 answers

Match a pattern and print only non zero field using awk

I have a file like this, and I want to search for pattern matching "/4126/" and print only the month and year and the amount (the amount is not always in Jan 2014 as in example below). awk -F! '/4126/ {print $0}' prints the entire line But I…
adam1969in
  • 117
  • 2
  • 9
3
votes
3 answers

Using a variable in a regular expression

I am using some sub(), gsub() functions to replace content stored in a variable. Say for example: $ awk 'BEGIN {str="hello bee"; patt="llo"; gsub(patt,"XX",str); print str}' heXX bee This replaces in the string contained in str all occurrences of…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
3
votes
1 answer

AWK array from split not sorted

I have this (demo) text in the variable ArtTEXT. {1}: Reporting Problems and Bugs. {2}: Other freely available awk implementations. {3}: Summary of installation. {4}: How to disable certain gawk extensions. {5}: Making Additions To gawk. {6}:…
Hanan Cohen
  • 383
  • 2
  • 15
3
votes
2 answers

How to set the field separator to an empty string?

The awk manual indicates that both -v FS and -F are equivalent ways to set the field separator. The GNU Awk User’s Guide -> 4.5.4 Setting FS from the Command Line: FS can be set on the command line. You use the `-F' argument to do so. (...) The…
fedorqui
  • 275,237
  • 103
  • 548
  • 598
3
votes
4 answers

In sed or awk, how do I handle record separators which *may* span multiple lines?

My log file is: Wed Nov 12 blah blah blah blah cat1 Wed Nov 12 blah blah blah blah Wed Nov 12 blah blah blah blah Wed Nov 12 blah blah blah blah cat2 more blah blah even more blah blah Wed Nov 12 blah blah blah blah cat3 Wed Nov 12…
Aaron Fi
  • 10,116
  • 13
  • 66
  • 91
3
votes
1 answer

AWK: redirecting script output from script to another file with dynamic name

I know I can redirect awk's print output to another file from within a script, like this: awk '{print $0 >> "anotherfile" }' 2procfile (I know that's dummy example, but it's just an example...) But what I need is to redirect output to another file,…
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110
3
votes
2 answers

Set awk output to variable in fish shell

I am using fish shell and I can't seem to set the output of awk to a variable. set installed_version (scala -version 2>&1 | awk 'NR==1{ print $5 }') Any ideas why that's the case? Edit: This works though set foo (java -version 2>&1 | awk 'NR==1{…
aa8y
  • 3,854
  • 4
  • 37
  • 62
3
votes
2 answers

Need to calculate standard deviation from an array using bash and awk?

Guys I'm new to awk and I'm struggling with awk command to find the standard deviation. I have got the mean using the following: echo ${GfieldList[@]} | awk 'NF {sum=0;for (i=1;i<=NF;i++)sum+=$i; print "Mean= " sum / NF; }' Standard Deviation…
Karthik
  • 69
  • 2
  • 8
3
votes
3 answers

Print columns using string variable in AWK

I am trying to use one variable in my AWK (or GAWK) program to print multiple columns. I am taking the columns to print from the command line: gawk -v cols=1,2,3 -f sample.awk -F, I want to be able to set this variable in my BEGIN{} block, and…
DJElbow
  • 3,345
  • 11
  • 41
  • 52
3
votes
3 answers

Replace column value in csv file with (g)awk with delimiter containing strings

I'm using gawk 4.0.1 and I know how to replace a column value in a CSV file, for example: > ROW='1,2,3,4,5,6' > echo $ROW | gawk -F, -vOFS=, '$2="X"' 1,X,3,4,5,6 However, I am dealing with a file which has strings containing the delimiter. Reading…
gospes
  • 3,819
  • 1
  • 28
  • 31
3
votes
1 answer

How can I check if a GNU awk coprocess is open, or force it to open without writing to it?

I have a gawk program that uses a coprocess. However, sometimes I don't have any data to write to the coprocess, and my original script hangs while waiting for the output of the coprocess. The code below reads from STDIN, writes each line to a…
DavidR
  • 810
  • 2
  • 8
  • 16
3
votes
2 answers

Renaming files based on number in first line

I used the awk answer from Theodros Zelleke, from split a fasta file and rename on the basis of first line, as a template for the code below: awk '/[[:digit:]]/ {OUT=substr($0,1) ".txt"}; OUT {print > OUT}' /path/to/file The above code resulted in…
iembry
  • 962
  • 1
  • 7
  • 23
3
votes
3 answers

Pivot table in AWK

I need to transform elements from an array to column index and return the value of $3 for each column index. I don´t have access to gawk 4 so I cannot work with real multidimensional arrays. Input Name^Code^Count Name1^0029^1 Name1^0038^1 …
eh2deni
  • 69
  • 1
  • 1
  • 4
3
votes
2 answers

Awk and dollar sign in record separator

I'm trying to parse a file that has, for whatever reason, the string "&($)" as record separator and "(@)$" as a field separator. I couldn't get awk to parse the file by specifying these as RS and FS in the BEGIN block. I'm using gnu awk 3.1.7 and it…
KumarM
  • 1,669
  • 1
  • 18
  • 26