Questions tagged [awk]

awk is a special-purpose, interpreted programming language for extracting and reporting data.

The awk utility interprets a special-purpose programming language that makes it easy to handle simple data-reformatting jobs.

Source: The GNU Awk User's Guide

The name comes from the initials of its original developers Aho, Weinberger, Kernighan who wrote this utility back in 1977.

513 questions
8
votes
1 answer

awk works in different ways on desktop and server

Just a simple question. Why this bash command works properly on Ubuntu Server and finds out the ISBN code and doesn't work on my Ubuntu Desktop? Both systems are 22.04 LTS. echo "ISBN 5-02-013850-9" | awk '/ISBN…
dyedfox
  • 343
3
votes
1 answer

How to restrict the decimal places in awk without rounding off

I want to print 0.74 for 0.748. If I use awk '{printf "%0.2f\n",$1}' filename for 0.748 it prints 0.75. But I want 0.74 and remove remaining part. For 77.348 it should be 77.34. Any ideas? There is no gawk -M option to use ROUNDMODE="Z" ?? Even…
3
votes
1 answer

How to apply AWK to a directory path?

I have this command: Folder=./GAS/NNN_/Neutral awk -F / '{print $(NF-1)}' $Folder But I got: awk: read error (Is a directory) I have tried: awk -F / '{print $(NF-1)}' $(Folder) awk -F / '{print $(NF-1)}' $($Folder) awk -F / '{print $(NF-1)}' $(echo…
2
votes
1 answer

How can I express just the last sum

I want to see just last sum here, but it shows me everything. awk '{print sum += $1} END {print sum}' file.dat This is the output: 1.2 3.6 7.3 7.3
2
votes
2 answers

awk: matching strings to decimal values and summing them

I have strings that are supposed to match with numbers. For example, one is equal to 1 Now, I have a file that separates columns with ; and I want to write an awk expression that checks if the SUM of the first column $1 is equal to $2. Here is an…
Plaurs
  • 23
2
votes
1 answer

Skip records containing no field separator in awk

In awk, I am using / as the field separator. However,there are some records in the input file that do not contain this field separator. How does awk behave for such records? Is there any way to tell awk to skip such records?
Satish
  • 21
  • 1
2
votes
1 answer

Help using Awk to change a file

I am using awk to simply change some text in a file, while maintaining the values as they are currently in the file. The problem is a few of the values are changing. the values are maintained if i use %f - however, i would like to eliminate the…
janet
  • 589
2
votes
1 answer

using variable in awk

Hello I would like to count number of words with specific lengths. I'm using this command. awk 'length == 2' mydict.txt | wc -l this code gives what I want but if try to put variable instead of number 2 it doesnt work. The code is like this awk…
2
votes
1 answer

How to keep original decimal numbers without rounding off

I am trying to compute the sum of the travel distance and it is computed perfectly in the text file. However, the results would be rounded off to 4 decimal places For instance, whenever I tried to add 10.1361234 and 10.1461621 meters from Distance…
Joseph
  • 35
2
votes
1 answer

can awk simultaneously read multiple input files?

I have the following code that works fine: $ awk 'FILENAME==ARGV[1] {a[FNR]=$0} FILENAME==ARGV[2] {print a[FNR],FS,$0}' tab1 tab2 (tab1 contains uppercases) The output is: 3A 3B 3C 3D 1a 1b 1c 1d 3A 3B 3C 3D 2a 2b 2c 2d 3A 3B 3C …
user587469
2
votes
1 answer

AWK: Why $* works inside bash function but not under pipe?

@terdon in this post answered the related question of mine, but I missed one more question in that post. Plz refer to the following commands: calc(){ awk "BEGIN{ print $* }" ;}; calc "((3+(2^3)) * 34^2 / 9)-75.89" The above commands work fine with…
user58029
  • 141
1
vote
1 answer

If two columns partially match, replace third with awk

File a.txt chr:1:10539:A:C 10539 C A 0.545987 0.508902 0 0.36065 + 1 chr:2:13494:A:G 13494 A G 0.330493 0.0264746 0 0.733423 + 1 chr:7:13494:A:G 13494 A G 0.330493 0.0264746 0 0.733423 + 1 File b.txt 1 4972 2 4972 3 4972 7 4970 I am looking for…
ThePooh
  • 11
1
vote
3 answers

Using awk to modify 3rd Column values

I am trying to convert the 3rd column as shown below. I can use this function to convert ***awk '{print $3}' | awk -F: '{printf("%02s:%02s:%02s:%02s:%02s:%02s\n",$1,$2,$3,$4,$5,$6)}'*** but how to get it to replace the 3rd column values. Before…
1
vote
1 answer

awk: search for pattern stored in an array

I use awk to parse a file. Have stored pattern words in an awk array. Would like to do something like this. if ( $0 ~ / arr[1] / ){ blah } I want to check if the pattern stored in the array element is found in the current line which is being…
1
vote
1 answer

avoiding losing precision with nawk option

I have a data file consists of rows with numeric data. The example of numeric data is 4053.45677771. When I perform arithmetic operation with nawk like; nawk -F, '{print $1*1000}' data > data_2 numeric values in data_2 file looks with exponential…
deepblue_86
  • 1,194
1
2 3 4