How to run grep inside awk?
Try following
awk '{print $1}' input.txt | xargs -n 1 -I pattern grep -rn pattern dir
using grep as if condition inside awk
You can use awk's system
function:
cat /var/log/somelogfile | awk '{ if (system("grep -Fxq " $1 " textfile")) print "useful command " $1; }'
See the docs.
Using grep and awk together
Here is an example. Create the following files and run
awk -f c.awk B.txt A*.txt
c.awk
FNR==NR {
s[$3]
next
}
$3 in s {
print FILENAME, $0
}
A1.txt
1 2 3
1 2 6
1 2 5
A2.txt
1 2 3
1 2 6
1 2 5
B.txt
1 2 3
1 2 5
2 1 8
The output should be:
A1.txt 1 2 3
A1.txt 1 2 5
A2.txt 1 2 3
A2.txt 1 2 5
grep a file using results from awk as parameter?
Use the following one-line awk
approach:
awk -F: 'NR == FNR{ if($1=="bob") p=$2; next}(p == $1){print $2}' userfile.txt passfile.txt
Here we have processing of two files.
How it works:
$ awk 'NR == FNR { # some actions; next} # other condition {# other
actions}' file1.txt file2.txtWhen processing more than one file, awk reads each file sequentially,
one after another, in the order they are specified on the command
line. The special variable NR stores the total number of input records
read so far, regardless of how many files have been read. The value of
NR starts at 1 and always increases until the program terminates.
Another variable, FNR, stores the number of records read from the
current file being processed. The value of FNR starts at 1, increases
until the end of the current file is reached, then is set again to 1
as soon as the first line of the next file is read, and so on. So, the
condition NR == FNR is only true while awk is reading the first file.
Thus, in the program above, the actions indicated by # some actions
are executed when awk is reading the first file; the actions indicated
by # other actions are executed when awk is reading the second file,
if the condition in # other condition is met. The next at the end of
the first action block is needed to prevent the condition in # other
condition from being evaluated, and the actions in # other actions
from being executed, while awk is reading the first file.
using grep as if condition inside awk
You can use awk's system
function:
cat /var/log/somelogfile | awk '{ if (system("grep -Fxq " $1 " textfile")) print "useful command " $1; }'
See the docs.
Running find, grep and awk script
If you have GNU awk
you can do everything in one line:
$ awk 'c{c--;a[$1]=$2} /Data starts now/{c=15} \
ENDFILE{m=1;c=0;for(i in a){m*=a[i]}print FILENAME": Multiplication is "m}' test*.log
test1.log: Multiplication is 9792
test2.log: Multiplication is 49929
test3.log: Multiplication is 190638
Explanation
awk '
c{ # if c is non-zero (c is 0 when script is 1st ran)
c-- # decrement c
a[$1]=$2 # Create a hash-map with $1 as the key and $2 as the value
}
/Data starts now/{c=15} # When regex matches, set c to 15
ENDFILE{ # True when the last record of a file has been read (gawk only)
m=1 # Set m to one so multiplication doesnt return 0
c=0 # Set c to 0 in case file has less than 15 lines after match
for(i in a){ # For each key/value pair...
m*=a[i] # Multiply them together and store result in m
}
print FILENAME": Multiplication is "m}
' test*.log
How to use awk and grep combination
You can use the following :
awk '$2 == "G01" {$10="value"}1' file.txt
To preserve whitespaces you can use the solution from this post :
awk '$2 == "G01" {
data=1
n=split($0,a," ",b)
a[10]="value"
line=b[0]
for (i=1;i<=n; i++){
line=(line a[i] b[i])
}
print line
}{
if (data!=1){
print;
}
else {
data=0;
}
}' file.txt
Related Topics
Why Does The Call Latency on Clock_Gettime(Clock_Realtime, ..) Vary So Much
How to Determine Thread Local Storage Model Used by a Library on Linux
Bash: Return a String from Bash Function
Detect If Stdout Is Redirected to a Pipe (Not to a File, Character Device, Terminal, or Socket)
What Is The Maximum Allowed Limit on The Length of a Process Name
How to Get Screen Dpi (Linux,Mac) Programatically
Cross-Platform Build Under Windows Targeting Linux Using Cmake
Configure "-Prefix" Option for Cross Compiling
Gdb Complains No Source Available
Getting Function Arguments Using Kprobes
What Is Dma Mapping and Dma Engine in Context of Linux Kernel
Inappropriate Ioctl for Device When Trying to Ssh
Check for Iommu Support on Linux
Bash Will Not Auto-Complete (Tab) with Files