Visit Our UNIX and Linux User Community


Variables in an awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Variables in an awk command
# 1  
Old 08-10-2013
Variables in an awk command

I am scripting a process where usernames are added to the end of a specific line in a file.

using this:

Code:
awk 'NR==1{$NF=$NF" TEXT"}1' myfile

works, and then I would use
Code:
> tempfile && mv tempfile myfile

HOWEVER. Being that this is to be scripted, I am trying to do something like:

Code:
awk: NR=="${chosenline}"{$NF=$NF" ${enteredtext}"}1

when I run through the script like this, my variables are populated, and in the awk command, however the NF's are gone. How can I get the NF value to work as it does without variables, but work when there are variables?

Any and all help would be greatly appreciated

Last edited by magicjoe; 08-10-2013 at 04:30 PM..
# 2  
Old 08-10-2013
Define awk variables and use them in your program:
Code:
awk -v L="$chosenline" -v T="$enteredtext" 'NR==L{$NF=$NF T}1' myfile

# 3  
Old 08-10-2013
Quote:
Originally Posted by Yoda
Define awk variables and use them in your program:
Code:
awk -v L="$chosenline" -v T="$enteredtext" 'NR==L{$NF=$NF T}1' myfile


The issue is not the variables. $NF is not there. I am debugging by using echo to see the generated command. It comes out like this:

Code:
awk 'NR==1{ = TEXT}1' myfile

# 4  
Old 08-10-2013
It seems $NF in your awk program got disappeared due to shell variable expansion before you program executed.

You might have to escape the dollar sign \$ to avoid that. Or post your code so that we can help.
This User Gave Thanks to Yoda For This Post:
# 5  
Old 08-11-2013
Thank you Yoda, you truly are a Jedi Master! the escapes seem to help some, but I am still "so close, yet so far away". I am posting my code here, I have documented where the issue is, and it is set up for debugging as it is. I can echo the command and the results are what I want, but it still is not quite perfect.
Code:
#!/bin/bash
#
#
#
#
#
#Adds username to .group file with prompts
#
########################################

# Lets the user know that they will have a backup of the .group file

clear

echo "==================================================="
echo "Welcome to the Report Access Tool.  As a precaution"
echo "a copy of the customer .group file will be copied  "
echo "to your home directory before the command completes"
echo "PRESS ENTER TO CONTINUE .......................  "
echo "==================================================="

read


clear

# prompt for username

echo "================================================="
echo "Enter the username that needs to access reports?"
echo "================================================="

read EmonUser

clear

# get customer

echo "================================================="
echo "  Which customer's reports are they accessing?"
echo "================================================="

read customer




# here is where we are sending a backup of the file, before doing our work, just in case
# commented out for testing
cp $customer.group ~/$customer.group.bak


# initiate a loop sequence that will allow the user to be added to multiple groups

while [ "$loop" != "n" ]
do

clear

echo "================================================="
echo "  Which report does this user need access to?"
echo "================================================="

# this line uses awk to display the group names of all lines in the file
awk -F':' '{ print NR " " $1 }' $customer.group


read reportline




# THIS IS THE COMMAND THAT HAS THE ISSUE. SET AS VARIABLE ONLY FOR DEBUGGING
keycommand="awk 'NR=="${reportline}"{\$NF=\$NF\" "${EmonUser}"\"}1' $customer.group"

echo $keycommand


# uncomment this read line to debug the above  command with echo
read
# ABOVE THIS LINE IS ONLY USED AT THIS TIME FOR DEBUGGING the results of echo $keycommand at this point are what needs to run
# once that piece works, I will append the > AddToGroup.tmp && mv AddToGroup.tmp $customer.group


clear

echo "====================================================="
echo "Do you want to add this user to another report? (Y/N)"
echo "====================================================="

read doitagain

# nested loop will repeat first loop until they answer no to adding to another group.


while [ "doitagain" != "n" ]
do

# case statment will exit this loop if they want to add to another report
# or will exit this loop, and then close the first loop, ending the script for no
# if the input is not Y or N, second loop starts again, forcing a valid answer
case $doitagain
in
[nN]) doitagain="n" ; loop="n" ; clear; break;;
[yY]) doitagain="y" ; break;;
   *) clear; echo "====================================================="; echo "Invalid action.  Add to another report?  Y or N"; echo "====================================================="; read doitagain;;
esac

done

# cleans up the temp file created
#rm AddToGroup.tmp

done

# 6  
Old 08-11-2013
Why do you store the shell command in a variable first?
It makes things complicated.
Directly run it, and becomes
Code:
awk 'NR=='$reportline' {$NF=$NF" '$EmonUser'"} 1' $customer.group

or the better readable
Code:
awk 'NR==L {$NF=$NF" "U} 1' L=$reportline U=$EmonUser $customer.group

# 7  
Old 08-12-2013
storing in the variable makes the echo command, which is used to debug. currently, the command does not work. for some reason, it does not keep all variables, and the NF, even when using escapes. if you copy the script, and create a TEST.group file, and try to run it you will see. If I run the command from the shell directly, it works. when I run the same awk command in the script, it does not function properly. storing it to a variable, and then echo that, allows me to see what is actually being run. however, what comes in the echo is correct, and manipulates the file how I want it to. yet when I run the script without the debugging, it does not manipulate the file as desired.

Previous Thread | Next Thread
Test Your Knowledge in Computers #156
Difficulty: Easy
The first two-network TCP/IP communications test was performed between Stanford and University College London, in 1975.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

2. Shell Programming and Scripting

awk - Why can't value of awk variables be passed to external functions ?

I wrote a very simple script to understand how to call user-defined functions from within awk after reading this post. function my_func_local { echo "In func $1" } export -f my_func_local echo $1 | awk -F"/" '{for (k=1;k<=NF;k++) { if ($k == "a" ) { system("my_local_func $k") } else{... (19 Replies)
Discussion started by: sreyan32
19 Replies

3. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

4. UNIX for Dummies Questions & Answers

Passing Shell Variables to an awk command

Hello, I have two files File1 & File2. File1 76 135 136 200 250 345 .... File2 1 24 1 35 1 36 1 72 .... I want to get all the values form File2 corresponding to the range in File 1 and feed it to a program. Is the code below right? Can I pass shell variables to awk in this... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

5. UNIX Desktop Questions & Answers

Variables within awk

Hi, I'm having trouble getting awk to read a variable with spaces in it. Input: vendorName="Bob's Steakhouse" awk -F":" '$2 ~ /'$vendorName'/ {print $1}' Purchases.dat Error: awk: $2 ~ /Bob's awk: ^ unterminated regexp The awk command isn't recognizing the entire string. It... (2 Replies)
Discussion started by: Cablephish
2 Replies

6. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

7. Shell Programming and Scripting

Using AWK variables.

Hi all, I am new to the forum and Shell Script programming. The problem is: I need to do a script to search all system processes and show me hierarchical way the number of bytes occupied by each of the regions of the memory map of each process. Today I got to show me the number of regions in... (3 Replies)
Discussion started by: cougar_rea
3 Replies

8. UNIX for Dummies Questions & Answers

Using variables in AWK

Hi, I'm pretty new to AWK and was wondering if someone could let me know how to execute varibles within an AWK statement. An example is below: NO=6 end=25 awk = 'NR == $NO, NR == $end' file1 > file2 I'm currently attempting to use this within a script but awk seems to read $NO and... (2 Replies)
Discussion started by: chris01010
2 Replies

9. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

10. Shell Programming and Scripting

Awk and Variables

Hi, I have some files in metrica (assume a pre-defined format) which i need to process and have some values in a .csv file. The script that does this, part of which is: procMetricaOMData() { host=$1 fileIN=$2 fileOUT=$3 $CATCMD $fileIN | \ awk -f... (4 Replies)
Discussion started by: deepak4you
4 Replies

Featured Tech Videos