Accessing Shell Variables in awk or sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Accessing Shell Variables in awk or sed
# 1  
Old 05-05-2008
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  
Old 05-05-2008
Quote:
Originally Posted by nasersh
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  
Old 05-05-2008
Thanks, how about with sed?
# 4  
Old 05-05-2008
With sed:

Code:
sed '/'"$var"'/p' filename

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Accessing variable from awk program in shell

Hi, I want to access a variable outside the awk program. My program is as below:- I can not access the exact value of k (See the last line of the program). #!/usr/bin/sh j=10 k=1 #k is declared outside awk awk ' BEGIN { i=1; j1="'"$j"'" printf("\n ## Value of j1 is %d ##", j1); ... (2 Replies)
Discussion started by: shouvik.mitra
2 Replies

2. Shell Programming and Scripting

Accessing variables of one shell script in another shell script

Hi All, I have a shell script called sample1.sh where I have 2 variables. Now I have another shell script called sample2.sh. I want the variables in sample1.sh to be available to sample2.sh. For example. In sample1.sh I am finding the sum of 2 numbers namely a and b. Now I want to access... (2 Replies)
Discussion started by: rsendhilmani
2 Replies

3. UNIX for Advanced & Expert Users

Accessing PL/SQL OUT variables in Korn Shell Script

Hello All, I was just wondering if there is any direct way to access PL/SQL OUT variables from Korn Shell Script. I could already figure out how to return a single value back from PL/SQL to Shell Script (using bind variable). But, what if we want to return multiple values? One option I... (4 Replies)
Discussion started by: bright_future
4 Replies

4. Shell Programming and Scripting

Accessing shell arrays in awk

I am trying to pass a shell array I created to match on values amongst other things. I have a sample: #! /bin/ksh i=0 for id in `cat id.lst` do IDs=$id (( i=i+1 )) done cat sqloutput.txt | awk -F, -v ids=$ID '{ # Do stuff }' But it looks as if I cannot do this.... (1 Reply)
Discussion started by: Zombywoof
1 Replies

5. Shell Programming and Scripting

problem accessing Multiple Variables from C Program to a Shell script

program name--test #!/bin/bash output1=`/home/user/a.c` output2=`/home/user/a.c` k=`$output1 + 1` m=`$output2 + 1` echo $k echo $m --------------------------------------------------------------------------- prgram name--a.c #include<stdio.h> int main() (1 Reply)
Discussion started by: sameworld1980
1 Replies

6. Shell Programming and Scripting

Accessing awk array from shell

Hi, i want awk to read a file and place it's content into two arrays. When trying to read these arrays with a "for a in ${source_path} "-Loop it gives the right result. But when trying to access directly (/bin/echo ${source_path}) it doesn't work. I read "all "the awk threads in this forum and... (6 Replies)
Discussion started by: bateman23
6 Replies

7. Shell Programming and Scripting

Problem in accessing variables outside shell

Hi, I have a shell script wherein i am doing some file operations and storing the data in some variables. I am exporting these variables as i need to use them outside shell. Then within the shell i am launching GDB session hoping that i will be able to access the exported variables in the GDB... (2 Replies)
Discussion started by: jsantosh
2 Replies

8. Shell Programming and Scripting

Accessing variables of one shell script in another shell script

I have a variable $exe in a shell script file a.sh which I need to access in another shell script file b.sh. How can I do that? :rolleyes: Thanks!! (2 Replies)
Discussion started by: looza
2 Replies

9. Shell Programming and Scripting

Could someone give me an example of awk accessing array defined in Korn Shell?

As per title and much apprecieated! (2 Replies)
Discussion started by: biglau
2 Replies

10. Shell Programming and Scripting

accessing ksh variables from awk

hi everybody! i am running this ksh script for replacing a set of strings by another set of new ones. i am getting both these from a file. also, the strings that i want to replace, are sub-strings(can occur more than once in each chunk) in a big chunk of data that i have bulk-copied(bcp utility)... (1 Reply)
Discussion started by: trupti wagh
1 Replies
Login or Register to Ask a Question