awk with passing variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk with passing variable
# 1  
Old 08-15-2016
awk with passing variable

I have file called in in.txt contains with the below lines I want to display the lines between the value which I would be passing.
Code:
one
two
three
four
five
ten
six
seven
eight

Expected output if I have passed one and ten
Code:
two
three
four
five

This one works for me when I pass value directly in awk.
Code:
cat in.txt | awk '/one/{flag=1;next}/ten/{flag=0}flag' 

With the expected below results

two
three
four
five

But when I use -v options it does not working for me, as I would like to pass the starting and ending point from the script.
Code:
start="one"
end="ten"
cat in.txt | awk -v v_start=$start -v v_end=$end '/v_start/{flag=1;next}/v_end/{flag=0}flag'
Did not work for me

Can some pls tell me what I am doing wrong
# 2  
Old 08-15-2016
You need to use regex strings instead of regex constants. Try:
Code:
awk -v v_start="$start" -v v_end="$end" '$0~v_start{flag=1;next}$~v_end{flag=0}flag'

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 08-15-2016
Quote:
Originally Posted by Scrutinizer
You need to use regex strings instead of regex constants. Try:
Code:
awk -v v_start="$start" -v v_end="$end" '$0~v_start{flag=1;next}$~v_end{flag=0}flag'

Hi Scrutinizer,
I think you dropped a character when copying your code here. Didn't you want $0 on both RE matches:
Code:
awk -v v_start="$start" -v v_end="$end" '$0~v_start{flag=1;next}$0~v_end{flag=0}flag'


Last edited by Don Cragun; 08-15-2016 at 09:18 PM.. Reason: Highlight the changed section in both pieces of code.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 4  
Old 08-15-2016
Thank you Scrutinizer and Don Cragun. It works great
# 5  
Old 08-16-2016
Just noticed that I am not printing the delimiter which is start and end on my results.

From the sample file,

Sample file:
Code:
one
two
three
four
five
ten
six
seven
eight

If I am passing "one" and "ten" the expected result would be
Code:
start="one"
end="ten"
awk -v v_start="$start" -v v_end="$end" '$0~v_start{flag=1;next}$0~v_end{flag=0}flag'

Expected output would be:
Code:
one
two
three
four
five
ten

Can anyone pls help me with this code?
# 6  
Old 08-16-2016
Quote:
Originally Posted by mychbears
Just noticed that I am not printing the delimiter which is start and end on my results.

From the sample file,

Sample file:
Code:
one
two
three
four
five
ten
six
seven
eight

If I am passing "one" and "ten" the expected result would be
Code:
start="one"
end="ten"
awk -v v_start="$start" -v v_end="$end" '$0~v_start{flag=1;next}$0~v_end{flag=0}flag'

Expected output would be:
Code:
one
two
three
four
five
ten

Can anyone pls help me with this code?
Hello mychbears,

Could you please try following and let me know if this helps you.
Code:
awk -v v_start="$start" -v v_end="$end" '$0~v_start{flag=1;}$0~v_end{print;flag=0}flag'  Input_file
OR
awk -v v_start="$start" -v v_end="$end" '$0~v_start{flag=1;print;next}$0~v_end{print;flag=0}flag'  Input_file
OR
awk -v v_start="$start" -v v_end="$end" '$0~v_start{flag=1;};flag;$0~v_end{flag=0}'  Input_file

Output will be as follows in above all 3 solutions.
Code:
one
two
three
four
five
ten

Thanks,
R. Singh
# 7  
Old 08-17-2016
Try also
Code:
awk -vST=$start -vEN=$end '$0~ST, $0~EN' file
one
two
three
four
five
ten

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Passing Shell Variable to awk

Hello All, May i please why my shell variable is not getting passed into awk script. #!/bin/bash -vx i="1EB07C50" /bin/awk -v ID="$i" '/ID/ {match($0,/ID/);print substr($0,RSTART,RLENGTH)}' /var/log/ScriptLogs/keys.13556.txt Thank you. (1 Reply)
Discussion started by: Ariean
1 Replies

2. Shell Programming and Scripting

awk programming -Passing variable to awk for loop

Hi All, I am new to AWK programming. I have the following for loop in my awk program. cat printhtml.awk: BEGIN -------- <some code here> END{ ----------<some code here> for(N=0; N<H; N++) { for(M=5; M<D; M++) print "\t" D ""; } ----- } ... (2 Replies)
Discussion started by: ctrld
2 Replies

3. Shell Programming and Scripting

Passing external variable to awk

Hi, I am trying to write a bash script in which I need to pass a external variable to the awk program. I tired using -v but it not accepting the value. Here is my sample code. #!/usr/bin/bash ###################################################################################### ####... (5 Replies)
Discussion started by: jpkumar10
5 Replies

4. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

Passing backslash character to awk variable

Hi All. I have a file that contains some special characters and I'm trying to use AWK to search for lines between <pattern1> and <pattern2>. As an example: I need the lines between the line containing ' select_id="x_0 ' and the line containing the next instance of ' from '. This is a file... (5 Replies)
Discussion started by: Mudshark
5 Replies

6. Shell Programming and Scripting

Passing multiple variable to awk

Hi , can I pass more then one variable to awk using -v option? (4 Replies)
Discussion started by: Anupam_Halder
4 Replies

7. Shell Programming and Scripting

awk alias passing a value to a variable

I am trying to turn this into an alias with no luck. I would then like to put the alias into my bashrc file. I know awk is very picky about quotes. I have tried every version of quotes, single quotes, double quotes, and backslashes that I can think of. VAR=$(xrandr | awk '$2=="connected"{s=$1}... (3 Replies)
Discussion started by: cokedude
3 Replies

8. Shell Programming and Scripting

Passing variable to awk

Hi, I'm new with this stuff, but I hope you can help me. This is what I'm trying to do: for id in $var; do awk '{if ($1 == $id) print $2}' merg_data.dat > neigh.tmp done I need that for every "id", awk search the first column of the file merg_data.dat which contains "id" and... (3 Replies)
Discussion started by: matteo86
3 Replies

9. UNIX for Dummies Questions & Answers

Passing a Shell Variable to awk

Hello, I have a file with 4 columns. An arbitrary example is shown below: a Tp 10 xyz b Tq 8 abc c Tp 99 pqr d Tp 44 rst e Tr 98 efg Based on the values in col 2 and col 3, I will execute another program. I have been running this:... (5 Replies)
Discussion started by: Gussifinknottle
5 Replies

10. UNIX for Advanced & Expert Users

Passing a variable into an awk script

Hello all, I'm trying to run a script of this format - for i in $(cat <file>); do grep $i <file1>|awk '{print $i, $1, $2}' It's not working - does anyone know how this can be done? Khoom (5 Replies)
Discussion started by: Khoomfire
5 Replies
Login or Register to Ask a Question