Using Shell variable within awk program


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using Shell variable within awk program
# 1  
Old 07-15-2010
Java Using Shell variable within awk program

I have a fixed width text file data.txt delimited by pipe. My requirement is to replace the second column values by *. Problem is that when tried access the shell variable in the awk program, value of shell variable is not printed instead only the name of the variable is printed. Please help to rectify the issue. Due to formatting issues, I have attached the text file along with message showing the data in text file and awk code that I have used. Please help me on this issue.Thanks

Last edited by VijayakumarS; 07-15-2010 at 11:31 AM.. Reason: Not displayed properly. Formatting done for readability
# 2  
Old 07-15-2010
I cannot even begin to fix the format of this post for you.
Please try using the [ code ] [ /code ] (no spaces) tags to assist you in formatting.
# 3  
Old 07-15-2010
Quote:
Code:
shell_var=`awk 'BEGIN{while (a++<9) s=s "*"; print s }'` 
awk '{ x=substr($0,1,9); z=substr($0,18); print x "$shell_var" z }' data.txt

Code:
[house@leonov] cat data.file
Column 1|Column 2|Column 3
Column 1|Column 2|Column 3
Column 1|Column 2|Column 3
Column 1|Column 2|Column 3
Column 1|Column 2|Column 3
[house@leonov] cat data.file | awk -F "|" '{print $1"|*********|"$3}'
Column 1|*********|Column 3
Column 1|*********|Column 3
Column 1|*********|Column 3
Column 1|*********|Column 3
Column 1|*********|Column 3

# 4  
Old 07-15-2010
you should change
Code:
awk '{ x=substr($0,1,9); z=substr($0,18); print x "$shell_var" z }' data.txt

to
Code:
awk '{ x=substr($0,1,9); z=substr($0,18); print x "'$shell_var'" z }' data.txt

so ,you can use shell variable in awk

Last edited by Franklin52; 07-15-2010 at 02:34 PM.. Reason: Please use code tags, thank you
# 5  
Old 07-15-2010
Probably you can also use the assignment option -v of awk for using the shell variable in the awk programming.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Awk: How to get an awk variable out to the shell, using system() ?

I am reasonably capable with awk and its quirks, but not with shell weirdness. This has to be Bourne Shell for portability reasons. I have an awk program that is working just fine; it handles multiple input streams and produces several reports, based on the request (-v Variables). In addition... (3 Replies)
Discussion started by: DerekAsirvadem
3 Replies

3. UNIX for Dummies Questions & Answers

is there a way to assing variable a value that is output of a program in awk script?

Hi there is there a way to assing variable a value that is output of a program in awk script. For e.g., I did temp=(`grep "" $5 | cut -f8 -d' '`) but it does not work. Any advice??? Thanks in advance!!! :) (3 Replies)
Discussion started by: FUTURE_EINSTEIN
3 Replies

4. Shell Programming and Scripting

Can a shell variable be called in a cobol program

Hi All, I have a file which sets all the variables on unix , based on the hostname. Currently these variables are hardcoded in the cobol programs.I was wondering if unix variables can be used in Cobol programs ? Example : I have a variable $SHTEMP which is set based on the following : Prod... (2 Replies)
Discussion started by: nua7
2 Replies

5. Shell Programming and Scripting

Shell variable to c++ program as input

I have an Shell script which has few global variables eg : range=100; echo "$range" I want to use the same variable in my C++ program for example int main() { cout << range << "\n"; } i tried using this int main(int argc, char *argv) { cout << range << "\n"; } but... (5 Replies)
Discussion started by: shashi792
5 Replies

6. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

7. 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

8. Shell Programming and Scripting

AWK Shell Program to Split Large Files

Hi, I need some help creating a tidy shell program with awk or other language that will split large length files efficiently. Here is an example dump: <A001_MAIL.DAT> 0001 Ronald McDonald 01 H81 0002 Elmo St. Elmo 02 H82 0003 Cookie Monster 01 H81 0004 Oscar ... (16 Replies)
Discussion started by: mkastin
16 Replies

9. Shell Programming and Scripting

AWK program with array variable

Hi, I made a small awk program just to test array variables. I couldn't find anything wrong with it. But it doesn't give out valid numbers.( just 0.00 ) Do you see any problem that I didn't see? Thanks in advance! Here is the program: ################################## BEGIN { FS =... (4 Replies)
Discussion started by: whatisthis
4 Replies

10. Shell Programming and Scripting

Passing shell variables to awk program..

Hello, Can we pass shell variables like $PATH etc. to a awk program part for example, awk ' { fieldValue=$PATH .... }' file (1 Reply)
Discussion started by: Vishnu
1 Replies
Login or Register to Ask a Question