New problem with awk using bash


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers New problem with awk using bash
# 1  
Old 03-14-2015
New problem with awk using bash

Hi!

I have a new problem with awk, this time I think is because I'm using it in bash and I don't know how to put the valor of the variable in awk. Here is the code:

Code:
#!/bin/bash

for i in 1 2 3 4 5
    do     
        a=$i
        b=$[$i+2]
        awk '$1>=a&&$1<=b {print $1,$2,$3}'>asdf test  
        newname="asdf_"$i
        touch $newname
        mv asdf /home/carol/test_bash/$newname
        touch asdf
    done

I think is not taking a and b as values, but if I use $a and $b in awk it doesn't work.

I hope you can help me Smilie
# 2  
Old 03-14-2015
You should pass using -v option of awk as below
Code:
x=100
awk -v fromShellX=$x 'BEGIN{print fromShellX}' /dev/null

in your case modifiy as

Code:
awk -v a=$a,b=$b '$1>=a&&$1<=b {print $1,$2,$3}'>asdf test

This User Gave Thanks to kumaran_5555 For This Post:
# 3  
Old 03-14-2015
The awk statement is still not correct. When passing two variables you need two -v options:
Code:
awk -v a="$a" -v b="$b" '....


Last edited by Scrutinizer; 03-14-2015 at 11:14 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-14-2015
That returns and empty file Smilie

---------- Post updated at 09:11 AM ---------- Previous update was at 09:10 AM ----------

Yes ! I needed two -v statements, thank you both! SmilieSmilieSmilieSmilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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... (2 Replies)
Discussion started by: TeaTimeSF
2 Replies

2. Shell Programming and Scripting

Bash/awk script problem

Hi, I have 100 files containing different values in single column, I want to split those files in two separate files (file2 and file3) based on average value of first column of each file, for those files I am working on the following script #bin/bash for memb in $(seq 1 100) do awk... (4 Replies)
Discussion started by: dsp80
4 Replies

3. Shell Programming and Scripting

bash problem

hello i am new with linux hello unix forum. i have big problem i want to install the samp server for every user folder for ex.. samp for my frined name fred fredsamp then i want create more samp to my firend lee i want folder name will be leesamp i want to know how make the script put the... (0 Replies)
Discussion started by: mage200
0 Replies

4. Shell Programming and Scripting

Bash or awk script to solve this problem

Hi everybody! I have written some awk scripts that return me some results I need to process. At the moment I use openOffice to process them, but I am trying to find a more efficient solution using possibly a bash or awk script. I have two files, file1 is in the format: time position ... (3 Replies)
Discussion started by: Alice236
3 Replies

5. Shell Programming and Scripting

Problem with awk awk: program limit exceeded: sprintf buffer size=1020

Hi I have many problems with a script. I have a script that formats a text file but always prints the same error when i try to execute it The code is that: { if (NF==17){ print $0 }else{ fields=NF; all=$0; while... (2 Replies)
Discussion started by: fate
2 Replies

6. UNIX for Advanced & Expert Users

AWK sub function curious problem under bash

I need to detect the number of pages in a print job when it is available so I can warn users when they try to print a report much larger than they expected. Sometimes they are trying to print 1000 page reports when they thought they were getting a 10 page report. Under linux I am scanning the... (5 Replies)
Discussion started by: Max Rebo
5 Replies

7. Shell Programming and Scripting

bash problem

I have a ksh script with the following code and working fine under ksh. IFS=$IFS IFS=: while read a b c do test "$a" = "$oraserver" && { orahome=$b; break; } echo $orahome done < /var/opt/oracle/oratab2 IFS=$_IFS ... (13 Replies)
Discussion started by: talashil
13 Replies

8. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

9. Shell Programming and Scripting

problem about '..' in bash

There is an interview question about UNIX bash: Some shells like bash try to make '..' always work propery, namely, from home directory, cd ../$USER will place you in your home directory. Does bash always get this behavior correct? Anyway can provide any example that bash doesnt work well? ... (4 Replies)
Discussion started by: usfish
4 Replies

10. Shell Programming and Scripting

Bash problem

Hello there, I'm a beginner in bash programining and I have a problem with the interpretetion of the code: sed -e "s/\(*\):.*/\1/" in this for loop: for process in $(sed -e "s/\(*\):.*/\1/" /etc/passwd) thx for any help edgehead (3 Replies)
Discussion started by: edgehead
3 Replies
Login or Register to Ask a Question