Problem with awk instructions and bash variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with awk instructions and bash variable
# 1  
Old 10-07-2015
Problem with awk instructions and bash variable

Hi everyone,

I'm trying to write a small script to automatize row data treatment. However, I got some trouble with the awk command.

I want to use awk to extract a define paragraph from a text file. The first and final lines are defined externally in two variables called debut and fin.

I first tried :
Code:
awk 'FNR>=$debut && FNR<=$fin' 2d_Pt_110 >> $i.$exp

But it doesn't work... I found that the variable cannot be simply called with $ in the awk instruction. So I tried the following command instead :
Code:
awk deb="$debut" fif="$fin" 'FNR>=deb && FNR<=fif' 2d_Pt_110 >> $i.$exp

But it's not working as well. I got the following error :
Code:
awk: can't open file FNR>=deb && FNR<=fif
 source line number 1

Do you have an idea to fix my problem ?

Thank you,
Best.
# 2  
Old 10-07-2015
You were very close!

Code:
# Modern way, variables get set before any BEGIN block, etc
awk -v deb="$debut" -v fif="$fin" 'FNR>=deb && FNR<=fif' 2d_Pt_110 >> $i.$exp

Or alternately

Code:
# Old-fashioned way, variables not set before any BEGIN block but before the file
awk 'FNR>=deb && FNR<=fif' deb="$debut" fif="$fin" 2d_Pt_110 >> $i.$exp

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-07-2015
Thank you ! Smilie It's helping me a lot !
This User Gave Thanks to TeaTimeSF For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash variable expansion in awk script

Hello, I need to split a file into two of different locations by re-direction in awk. cat infle aaa 1 3 bbb 2 4 aaa 3 3 bbb 4 4 aaa 5 3 bbb 6 4 cat /storage/tmp/group_a.gtf aaa 1 3 aaa 3 3 aaa 5 3 cat /storage/tmp/group_b.gtf bbb 2 4 bbb ... (2 Replies)
Discussion started by: yifangt
2 Replies

2. Shell Programming and Scripting

Bash script - ENV Variable context problem using su

Hello I have found some piece of code to verify and then run shell script with root permission from normal user. see : http://blog.mecworks.com/articles/2006/02/23/bash-scripting-tip-running-a-script-as-root I have wrote two scripts using this tips. - one to copy file from server to local... (6 Replies)
Discussion started by: jcdole
6 Replies

3. Shell Programming and Scripting

Bash, Dynamic Variable Problem >.<

Hello, I have a problem with a bash script, I've been doing recherches but i can't make it work. It is my first time with a dynamic variable and i don't understand how to write it. #!/bin/bash USER=BARSPIN HOTKEYS_PATH="/home/$USER/Documents/bash/tibia/HOTKEYS" CFG1="A" CFG2="B"... (5 Replies)
Discussion started by: barspin
5 Replies

4. Shell Programming and Scripting

awk not escape my bash variable

I tried to parse data from switch configuration files vlan 1727 name SQ5506-15 by port tagged ethe 8/1 to 8/2 untagged ethe 1/13 ! vlan 2105 name SQ5620-7007(BR2) by port tagged ethe 8/1 to 8/2 untagged ethe 1/17 ! interface ethernet 1/13 port-name SQ5506-15.nic0 rate-limit... (2 Replies)
Discussion started by: winggundamth
2 Replies

5. Shell Programming and Scripting

populate a bash variable from an awk operation

Hi, I'm trying to populate bash script variable, data_size with the size of the largest file in my current directory data_size=$(ls -lS | grep -v "total" | head -1) | awk '{ print $5 }' I've tried adding an echo before the call to awk data_size=$(ls -l | grep -v "total" | head -1) |... (2 Replies)
Discussion started by: mark_s_g
2 Replies

6. Shell Programming and Scripting

Creating variable using awk in bash

I would like to create a variable within my bash script using awk. I'm reading in a line from an external file, then outputting to a new file in a specific format. But, it doesnt quite work as I have expected and could use some help. (A pertinent excerpt of ) the bash code is: count=1 ... (4 Replies)
Discussion started by: snoitacilppa
4 Replies

7. Shell Programming and Scripting

assign awk output to bash variable

greetings all, I am have a heck of a time trying to accomplish a very simple thing. I have an array of "shortname<spaces>id" created from a dscl output. I want to assign shortname=word1 and id=word2. I have tried shortname=$(${textArray} | awk '{print $1}') - and get 'awk : cannot open... (3 Replies)
Discussion started by: macnetdaemon
3 Replies

8. Shell Programming and Scripting

saving awk value in a bash array variable

hi all i am trying to save an awk value into an array in bash: total=`awk '{sum+=$3} END {print sum}' "$count".txt"` ((count++)) the above statement is in a while loop.. $count is to keep track of file numbers (1.txt,2.txt,3.txt,etc.) i get the following error: ./lines1:... (1 Reply)
Discussion started by: npatwardhan
1 Replies

9. UNIX for Dummies Questions & Answers

Instructions for a bash shell script

Hi, I have written a series of bash shell scripts that I will putting on a website for anyone (ie people in my specific field) to download and use. I am currently writing a readme file that explains how to run them, and it has occurred to me that it would be a good idea to indicate that they are... (1 Reply)
Discussion started by: msb65
1 Replies

10. Shell Programming and Scripting

Urgent!! Bash - problem using CD with variable

I am extracting directory path from file which is stored in a variable. while read inputline do script_name=`echo $inputline | cut -d' ' -f2` cd $script_name done < path_scripts.txt where as path_scripts contains input_process.bash ~/jobs/process/input/bin The script output says... (1 Reply)
Discussion started by: jacksam007
1 Replies
Login or Register to Ask a Question