Awk variable replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk variable replacement
# 1  
Old 08-30-2007
Awk variable replacement

I have a function

awkvarrep() {
awk -F'|' '$1~/$1/{printf "%-10s %-30s %-15s %-30s %-15s\n", $2,$3,$4,$5,$6}' testfile
}

I'm calling it by this
VARREP=XYZ
awkvarrep $VARREP

since i'm passing $VARREP to the awkvarrep() function I want to use this with $1 but it dosen't seem to be working inside the awk in the Shell Script. Do I need to use an escape sequence for it to work pls advice or else I need to do this like 10 times!
# 2  
Old 08-30-2007
Try :

Code:
awkvarrep() {
awk -F'|' '$1~/'"$1"'/{printf "%-10s %-30s %-15s %-30s %-15s\n", $2,$3,$4,$5,$6}' testfile
}

# 3  
Old 08-30-2007
Quote:
Originally Posted by Klashxx
Try :

Code:
awkvarrep() {
awk -F'|' '$1~/'"$1"'/{printf "%-10s %-30s %-15s %-30s %-15s\n", $2,$3,$4,$5,$6}' testfile
}

Thanks !!! Way to go it works Smilie
# 4  
Old 08-30-2007
Quote:
Originally Posted by Klashxx
Try :

Code:
awkvarrep() {
awk -F'|' '$1~/'"$1"'/{printf "%-10s %-30s %-15s %-30s %-15s\n", $2,$3,$4,$5,$6}' testfile
}

Can you explain why whe need to put it in double quotes pls.
# 5  
Old 08-30-2007
Actually, the single quotes are making the difference, not the double ones:

Code:
awk -F'|' '$1~/'"$1"'/{ code... }'

All the awk code is enclosed in single quotes, when you want to "break" the awk code because you need the value held by an external shell environment variable, you simply close your code with a quote and reopen it (with another quote) when you've finished the external variable evaluation.

Putting double quotes around a shell variable is only a good scripting practice (so you avoid possible problem with null values, etc...).
# 6  
Old 08-30-2007
Quote:
Originally Posted by robotronic
Actually, the single quotes are making the difference, not the double ones:

Code:
awk -F'|' '$1~/'"$1"'/{ code... }'

All the awk code is enclosed in single quotes, when you want to "break" the awk code because you need the value held by an external shell environment variable, you simply close your code with a quote and reopen it (with another quote) when you've finished the external variable evaluation.

Putting double quotes around a shell variable is only a good scripting practice (so you avoid possible problem with null values, etc...).
Thanks its very clear explanation!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacement of variable by their content in a file

Dear all, I have a "SQL request" in a file: that request include different "host variable" and I would like to substitute the different "host variable" by their respective content before executing the request. For example: $ echo $SHELL /bin/bash $ cat dae2.txt DELETE FROM ... (11 Replies)
Discussion started by: dae
11 Replies

2. Shell Programming and Scripting

sed - replacement file path with variable - Escaping / character

Hi,, I have the line below in a file: $!VarSet |LFDSFN1| = '"E:\APC\Trials\20140705_427_Prototype Trial\Data\T4_20140705_Trial_Cycle_Data_13_T_Norm.txt" "VERSION=100 FILEEXT=\"*.txt\" FILEDESC=\"General Text\" "+""+"TITLE{SEARCH=NONE NAME=\"New Dataset\" LINE=1I want to write a script to change... (2 Replies)
Discussion started by: carlr
2 Replies

3. Shell Programming and Scripting

Sed:- Supported variable replacement after string match?

Hi All, I am trying to replace the variable in the file after the particular match string. It is being replaced if i hardcode the value and with use of "&" with sed. sed -e "s/URL./& http:\\localhost:7223/g" But when am trying to pass the variable it is failing. I tried multiple... (9 Replies)
Discussion started by: sharsour
9 Replies

4. Shell Programming and Scripting

sed replacement in file when line is in a variable

Hi, I have a file where I want to replace the 15th field separated by comma, only on specific lines matching lots of different conditions. I have managed to read the file line by line, within the loop my line is held in a variable called $line I assume this will be using sed (maybe... (5 Replies)
Discussion started by: jpt123
5 Replies

5. Shell Programming and Scripting

awk's gsub variable in replacement

I been trying to figure out how to use element of array as a replacement pattern. This works as I expected: $ echo "one two three" | awk '{ gsub(/wo/,"_BEG_&_END_",$2); print }' one t_BEG_wo_END_ three $ echo "one two three" | awk '{ tmp="foo"; gsub(/wo/,"_BEG_" tmp "_END_",$2);... (5 Replies)
Discussion started by: mirni
5 Replies

6. Shell Programming and Scripting

Awk replacement problem

Hello everyone, I have a problem with awk replacement... I need to replace "|\n" for "\n" I tried thisawk '{ sub(/\|\\n/, "\\n"); print }' but it seems like it doesn't work properly. Can anyone help with that? (4 Replies)
Discussion started by: 1tempus1
4 Replies

7. Shell Programming and Scripting

awk replacement problem

hi i am using awk for first time so i am having issue is it the correct way of using Y here is variable which i fetch using grep from file awk -v X={$Y} { if ($0 ~ /X/ ) { sub (out.*/,"out1",$0) } print 0 }, filename > temp when i look into temp file i dont see any replacement... (9 Replies)
Discussion started by: xyzstar
9 Replies

8. Shell Programming and Scripting

Awk replacement

Hi all, I need help in replacing awk with sed for the below. 1 ) cat list | awk -F" |," '/MATH/ {sub(/]*/,"",$3); print $3}' Eg: file : list Sno Subno Name 1 SUB1 ENG 2 SUB2 MATH 2) Eg:result Total No of Students: 2 Sno ID Sub ------------------ 1 ... (3 Replies)
Discussion started by: priyam
3 Replies

9. Shell Programming and Scripting

AWK String replacement

I have an xml file with following tags <NewTag>value123</xyz> <NewTag>value321</abcd> I have to replace the values in between the tags with some value ( VAL1/VAL2) but the thing the ending tag can be any thing, for this i need a awk command currently i am using this but it... (5 Replies)
Discussion started by: subin_bala
5 Replies

10. Shell Programming and Scripting

variable replacement

hi all , i want a command that interacts with the input of a user . i.e. i want it to serch a file for the first occurance of a variable and replace the value that is after the equal sign . for example : my file contains a varible that is $HOME=/home i want to search for variable $HOME... (4 Replies)
Discussion started by: ppass
4 Replies
Login or Register to Ask a Question