Linked Questions

3
votes
2answers
13k views

How to pass shell variable to an awk search pattern? [duplicate]

In my bash script I have a variable that I am trying to pass to a pattern to search for using awk. However what I expected to happen is not working. I have the following text file (text.txt): --------...
3
votes
2answers
11k views

How to insert bash variables in awk? [duplicate]

How to insert bash variables in awk (for example i need to do in in some for loop - like this: in first iteration use awk to search string by first column, next by second column and so on with using ...
0
votes
2answers
6k views

How to use a variable inside an awk statement? [duplicate]

I have a script like this #!/bin/ksh echo "Enter the matching pattern" read pattern path= /home/siva/ echo "Navigating to $path" cd $path cat filename|awk '/$pattern/ {for (i=1; i<=10; i++) {...
1
vote
1answer
3k views

make awk print the line that match a variable and the next n lines and use a variable in awk [duplicate]

With an array of elements, and an input-file I want to loop through the array and match each element with the input-file. I then want to get the line in the input-file where this element is occurring ...
-1
votes
1answer
1k views

AWK and Bash Scripting [duplicate]

How to use a value of a variable in awk? Something like this: filename = "test.txt" ls -l | awk '{ if ($9 == filename) print("File exists")}' I can't use $ in awk to access the value of that ...
1
vote
1answer
706 views

Pass Shell variable to awk [duplicate]

I realize this has been asked many times and many of the answers seam well written, but I'm missing something with my little task. Trying to pass a Shell variable (an argument in this case) to awk to ...
0
votes
2answers
1k views

using awk with variables [duplicate]

I am making a word list from all the words typed into a program that will be working with the text entered into it. I want to use a stream editor like sed or awk to search the first word of every ...
2
votes
1answer
378 views

Pass parameter to Bash function which will serve as a pattern to awk [duplicate]

I would like to know how to pass a argument to a Bash function which would consequently serve as a pattern to awk. I have read several Q/As which tend to answer the same question, however, I am ...
1
vote
1answer
417 views

how to use variable with awk [duplicate]

we have the following file cat /tmp/hive.conf "hive-exec-log4j2" : { "tag" : "TOPOLOGY_RESOLVED", "version" : 2 }, "hive-interactive-env" : { "tag" : "...
1
vote
2answers
474 views

how to awk user input [duplicate]

I'm trying to match a pattern and then print out two lines after it. I'm successfully able to do it with the following code. awk '/'202404'/{c=3}c&&c--' consolelog.log > testing.txt I'm ...
3
votes
1answer
90 views

Passing argument to AWK [duplicate]

I have the following AWK command which works well. awk -v RS="\n+?[$]{4}\n+?" '/HMDB0000008/' test.xt However what I want is to be able to call it from the command line and pass an argument ...
0
votes
1answer
164 views

Pass a variable in another variable within an awk [duplicate]

I have the below variables: grep_line_new_file='create table "informix".issue' new_file_below=`awk 'BEGIN { FS="\n[()]"; RS=";" } /$grep_line_new_file/ { print ")"$NF";"}' test` Inside test file I ...
-1
votes
1answer
53 views

pass value to awk in shell script [duplicate]

So I have my targets array which is a bunch of IP read from a file, now I am trying to cat a file that contains my IP and results from ssh command, ut because I am trying 3 different passwords on ...
1
vote
1answer
46 views

How to make variable be taken into account [duplicate]

#!/bin/bash awk 'NR!~/^(1|$q+2|$q+3)$/' deltay.txt > yota.txt q is an integer obtain from a previous process in the script, and I need to erase lines one, the q+2 an q+3, but that command just ...
0
votes
0answers
27 views

pattern matching to content of variable using awk [duplicate]

**********NB -i have read previous posts that look like duplicates but I cannot get this to work, Id really appreciate an explicit answer ********** I know this is a duplicate but I have read the ...
58
votes
3answers
92k views

How to show lines after each grep match until other specific match?

I know that by using the "-A NUM" switch I can print specific number of trailing lines after each match. I am just wondering if it's possible to print trailing lines until a specific word is found ...
2
votes
2answers
22k views

awk + set variable inside the awk

the following simple bash script print the row number ( from the file - list.txt ) function get_row { [[ $1 = 1 ]] && awk '{print $1}' list.txt [[ $1 = 2 ]] && awk '{print $2}' ...
5
votes
1answer
2k views

Using bash variable with escape character in awk to extract lines from file

I am writing a bash script (just learning bash) to extract some lines from a file based on two patterns. The first pattern is just a sentence ending in a colon. The second pattern is a * repeated N (...
1
vote
7answers
670 views

How do we use sed to replace specific line with a string variable?

Lets say for example I have a file.txt containing 3 lines: hello hello hello I have a variable called line=1 and a variable string substitute=hello1. I want to replace the string in the line ...
1
vote
1answer
3k views

use awk with input from a variable

i need to fetch n+9th line from the occurance of a specific word which is store in variable i from a file LIST.txt Below is the command Used_pages=`awk '/${i}/{nr[NR+9]}; NR in nr' LIST.txt` it ...
1
vote
1answer
800 views

awk argument $1,$2 conflict with bash input arguments

I would like to add some awk commands to a bash script. The bash script takes a few inputs, and inputs are passed by $1, $2 and etc. When I call awk, I also want to use $1 to define a position in ...
3
votes
2answers
534 views

How to use command-line argument as awk regex matching expression?

I have the following awk script: #!/bin/awk -f BEGIN { FS = ""; } value ~ "MYVALUE" # silly test { print "1 - " substr($0, 235, 12); } $235 ~ "M" { print "2 - " substr($0, 235, 12); } ...
0
votes
2answers
816 views

How to grep variable pattern which is in first column [duplicate]

I want to grep variable pattern which is in first column. $ cat test.txt abc.xyz abc.def pqr.tap pqr.abc abc.mnp mnp.abc abc.pqr abc.mob If variable pattern is abc then output should be abc....
0
votes
0answers
647 views

invalid char '@' in expression awk output

I have an awk command on ksh script, the command extract fields from a log file to then struct them in new one. ultimate=$(grep -oP 'Time:\K.*' $log2 | column -t | awk '{print $3,"/","'"$month"'","/",...
0
votes
2answers
289 views

How can I search matching pattern (stored in a variable) in a file using awk?

I have a log file in which I need to check if there are any error messages, if there are, extract the error message to another file. Like this I may have error file for the different file have ...
1
vote
3answers
96 views

Simple shell script unable to go through thousands of files; starts fine, but throws “unexpected EOF while looking for matching `”`" after some time

Shell Script in Question Let me explain what I am trying to do by e.g. so you can understand better. Let's say I have 100 .torrent files in a directory. 2 of them will download xxx.epub and yyy.epub ...
0
votes
2answers
76 views

AWK different result

I was searching for List of Open Files based on Ips amd Processes. I used below command for i in `sudo netstat -tulpna|awk '{print $5}'|grep -E "[0-9]{3}"|grep -v "^192\|10"|cut -f1 -d ":"`;do ...
1
vote
1answer
80 views

When using AWK with a variable pattern, can't get IF ELSE statement working

The AWK statement uses the variable $ip to search each line, but I need to print the variable $ip NOT FOUND in the ELSE statement for ips that are not found in the config. Can't get it to work with ...