Help with nested $s and quotations in bash / awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with nested $s and quotations in bash / awk
# 1  
Old 01-14-2013
Question Help with nested $s and quotations in bash / awk

Folks - newbie bash coder here and I'd like to get your help to make the code below work. As you can see, I was trying to count the total number of lines with the 3rd value >= 15 in a file and wanted to make the threshold "15" configurable, but apparently the $THRESHOLD value was not populated correctly.

Thanks for your help!

Code:
THRESHOLD=15
REPORT_FILE=./report.txt
CMD="awk '{if ($3 >= $THRESHOLD) L++} END {print L}' $REPORT_FILE"
echo Command: $CMD
$CMD

# 2  
Old 01-14-2013
Code:
THRESHOLD=15
REPORT_FILE="report.txt"
CMD="awk -v threshold=$THRESHOLD '{if(\$3>=threshold) L++} END {print L}' $REPORT_FILE"
echo "Command: $CMD"
eval "$CMD"

# 3  
Old 01-14-2013
Code:
awk -v thr="${THRESHOLD}" '{if ($3 >= thr) L++} END {print L}' $REPORT_FILE

# 4  
Old 01-14-2013
Thank you both! That explained why my attempt below did not work!
Code:
CMD="awk '{if($3>="+$THRESHOLD+") L++} END {print L}' $REPORT_FILE"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output with quotations

I will invoke a shell script.bash -x ./00_remove_tenants_FR_BE_instanceData.sh CGD2ICON CGDEV2 50005 cgdev2@123 I need a string of "cgdev2/'cgdev2@123'@localhost:50005/cgd2icon" parameters ==> 2nd 4th 3rd ... (3 Replies)
Discussion started by: pranabpal
3 Replies

2. UNIX for Dummies Questions & Answers

Nested loop -bash

I am using the following nested loop for i in {1..3} do for y in {1..3} do if ; then echo P0${i}R${y}.fas mv P0${i}R${y}.fas P${i}R${y}.fas read -t 5 fi done done I was wondering if I can use a character such as * or ? instead of my second variable y. I tried R in... (3 Replies)
Discussion started by: Xterra
3 Replies

3. Shell Programming and Scripting

Nested double quotes won't work in my bash script?

In a bash script I have: LSCMD="find /project/media/ -mindepth 2 -maxdepth 2 -name \"files*pkg\"" ALL_PACKAGES=$( $LSCMD | sort 2>/dev/null) But I get nothing returned. It's just all blank. If I run the find command in a terminal, I get dozens of hits. I figure it's the way how I'm... (3 Replies)
Discussion started by: superbbrr
3 Replies

4. Shell Programming and Scripting

Using a Regex with double quotations

Hi All, for a script I'm writing I need to be able to put the following command in a variable: tail -30 <file> | egrep "JBoss \(MX MicroKernel\) .* Started" I thought surround it with single quotes would work. However, it doesnt escape the wildcard, and results in the wildcard being expanded... (8 Replies)
Discussion started by: dpatters
8 Replies

5. Shell Programming and Scripting

Complex bash/sed, variables and nested quotes

Ok, this one isn't for everybody, it's pretty tough and I've spent a good deal of time on it without figuring it out yet. Can anybody get this script to work: #!/bin/bash cq_fname="%let outputfile="/user/cq_"$1".csv";" sed "29s/.*/\"$cq_fname\"/" file1.sas >... (3 Replies)
Discussion started by: nocloud
3 Replies

6. Shell Programming and Scripting

nested logical expression in bash shell

Please tell me how to nest logical expressions in bash. I would like to nest logical expressions for arguments of the "test" command on bash. The following pseudo-code shows my intention. // pseudo code if (exp1 AND (exp2 OR exp3)) { Output true; } else { Output false; } ... (11 Replies)
Discussion started by: LessNux
11 Replies

7. Shell Programming and Scripting

Nested for loop in bash

Hi, How to use values in one for loop to other for loop. say "$sf_rel" variable has values "2011/W2 2011/G2" I want to use these values in inner for loop to process properly. $branch variable has G2 and 6 What is happening is outer for loop $i has 2011/W2 it is entering into inner... (3 Replies)
Discussion started by: Anjan1
3 Replies

8. UNIX for Dummies Questions & Answers

Bourne-sh (not bash) question about nested loops and sed

Here's the input: alpha, numeric or alphanumeric string ("line 1 string") numeric string ("line 2 string") numeric string ("line 3 string") numeric string ("line 4 string") ... where - each numeric string is in a pattern that can be matched with RE but - there can be any number of... (2 Replies)
Discussion started by: uiop44
2 Replies

9. Shell Programming and Scripting

Nested if question BASH

Just started learning bash ,and I am confused with sintaksis line 16: syntax error near unexpected token `else' thanks #!/bin/bash echo -n "Enter: " read num if(($(echo ${#num}) == 0 )) then echo No arguments passed.Try again elif rem=$(echo $num | tr -d ) ... (7 Replies)
Discussion started by: lio123
7 Replies

10. Shell Programming and Scripting

Bash: Nested functions and including other scripts

Hello. I have the following problem with bash code: function fl1_load_modules_and_get_list() ........... for module in $FL_MODULES_TO_PROCESS do source "${FL_MODULE_DIR}/${module}/module.sh" done ........... } function fl1_handle_install { local... (12 Replies)
Discussion started by: FractalizeR
12 Replies
Login or Register to Ask a Question