The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Accessing variables of one shell script in another shell script rsendhilmani Shell Programming and Scripting 2 03-17-2009 01:17 AM
Accessing PL/SQL OUT variables in Korn Shell Script bright_future UNIX for Advanced & Expert Users 4 12-02-2008 12:02 PM
accessing unix variables in oracle ravi raj kumar High Level Programming 1 02-06-2008 11:05 AM
Accessing Variables from .conf file wayne1411 Shell Programming and Scripting 7 01-30-2008 04:30 PM
accessing ksh variables from awk trupti wagh Shell Programming and Scripting 1 02-26-2007 04:11 AM

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

Join Date: Jan 2008
Posts: 4
Accessing Shell Variables in awk or sed

Hello,

I wonder if it is possible to pass and use variables from shell environment into sed or awk. I am trying to achieve something similar to the following using sed or awk:
Code:
var=some_regular_expression
grep "$var" filename # Will extract lines from filename
The following code, obviously, doesn't work as I expect it to:
Code:
var=some_value
awk '/$var/ {print}' filename # or: sed -n '/$var/p' filename; This doesn't work same as grep
Is there any other way I could access sh-scoped variables inside an awk program?
  #2 (permalink)  
Old 05-05-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,420
Quote:
Originally Posted by nasersh View Post
Hello,

I wonder if it is possible to pass and use variables from shell environment into sed or awk. I am trying to achieve something similar to the following using sed or awk:
A basic example:
Code:
var=value
awk -v X=$var 'BEGIN{print X}'
  #3 (permalink)  
Old 05-05-2008
nasersh nasersh is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 4
Thanks, how about with sed?
  #4 (permalink)  
Old 05-05-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
With sed:

Code:
sed '/'"$var"'/p' filename
Regards
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 01:09 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