The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need Replacement for sed dbsurf Shell Programming and Scripting 0 01-25-2008 06:47 PM
Regarding Replacement rajx UNIX for Dummies Questions & Answers 2 05-22-2007 10:19 AM
variable replacement ppass Shell Programming and Scripting 4 06-01-2005 11:18 AM
Replacement using sed handak9 UNIX for Dummies Questions & Answers 5 07-13-2004 12:28 PM
cpu replacement. help IMPTRUE UNIX for Dummies Questions & Answers 3 07-25-2003 09:40 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-30-2007
iAm4Free iAm4Free is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 18
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 (permalink)  
Old 08-30-2007
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
Try :


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

  #3 (permalink)  
Old 08-30-2007
iAm4Free iAm4Free is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 18
Quote:
Originally Posted by Klashxx View Post
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
  #4 (permalink)  
Old 08-30-2007
iAm4Free iAm4Free is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 18
Quote:
Originally Posted by Klashxx View Post
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 (permalink)  
Old 08-30-2007
robotronic's Avatar
robotronic robotronic is offline Forum Advisor  
Can I play with madness?
  
 

Join Date: Apr 2002
Location: Italy
Posts: 370
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 (permalink)  
Old 08-30-2007
iAm4Free iAm4Free is offline
Registered User
  
 

Join Date: Oct 2005
Posts: 18
Quote:
Originally Posted by robotronic View Post
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!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0