To print variables using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To print variables using awk
# 1  
Old 12-19-2011
To print variables using awk

Can anyone help me with how to print the variable using a awk statement.

Code:
for i in ` cat serverlist.txt ` ; do 
my command | awk '{print $1 $2 $i}' 
done

It should print like below but it is not

Code:
XXXXX YYYYY Servername 
XXXXX YYYYY Servername 
XXXXX YYYYY Servername 
XXXXX YYYYY Servername

The last column is what I am adding and printing from the variable $i.

Any help please ?

Regards,

Ravikumar R

Last edited by zxmaus; 12-19-2011 at 05:43 PM.. Reason: use code tags please
# 2  
Old 12-19-2011
Code:
for i in ` hostname ` ; do 
df -k | awk -v i=$i '{print $1, $2, i}' 
done

However the df -k part does not work that way, except in some clustered environments.
hostname will give single result, so i will always be the same....

What are you trying to do? What we see does not make clear sense.

Last edited by jim mcnamara; 12-19-2011 at 06:32 PM.. Reason: hightlight changes
# 3  
Old 12-19-2011
Thanks for the quick reply. Ok I just put the command an example.

I am using a different command where it is printing the $1 and $2 fine, I want to add a last column which should be the hostname and should read from the $i ( from the for loop)


example

Code:
for i in ` cat serverlist.txt ` ; do 
my command | awk '{print $1 $2 $i}' 
done

It should print like below but it is not

Code:
XXXXX YYYYY Servername 
XXXXX YYYYY Servername 
XXXXX YYYYY Servername 
XXXXX YYYYY Servername

The last column is what I am adding and printing from the variable $i.

Any help ?

Last edited by zxmaus; 12-19-2011 at 05:41 PM.. Reason: added tags
# 4  
Old 12-19-2011
Re-read my post above. the red part - that is your help.
This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 12-19-2011
Thanks a lot .. It works like a charm
# 6  
Old 12-20-2011
A little complement...
If the variable you use in awk contains blanks (" ") you will need to add quotes like following:

for i in ` hostname ` ; do
df -k | awk -v i="$i" '{print $1, $2, i}'
done


Of course, it's useless in this example (a hostname won't have a space in it's name)

But try:
i="` hostname ` is my host"
echo $i
df -k | awk -v i=$i '{print $1, $2, i}'
myhost.domain is my host
awk: cmd. line:1: fatal: cannot open file `my' for reading (No such file or directory)
whereas this works
i="` hostname ` is my host"
echo $i
df -k | awk -v i="$i" '{print $1, $2, i}'
This User Gave Thanks to Bernard For This Post:
# 7  
Old 12-20-2011
Thank you so much for the info. I will try this one too. Basically I am not using the df -k command , but i just put that here for example but realized later that it is not a right example.

BTW thanks for the info and I will give a try

Regards,

Ravi
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compare two variables and print the difference

Hi PRIM_SEQ=`some sql code` and output of PRIM_SEQ is like below 120 130 STB_SEQ=`some sql code` and output of STB_SEQ is like below 115 110 i need to compare this two variables output ( decimal numbers) 1) What I want to do is to compare every number in the PRIM_SEQ with... (8 Replies)
Discussion started by: amar1208
8 Replies

2. UNIX for Advanced & Expert Users

awk With multiple print variables

Hi, I have a parameter which will be having the fields which needs to be filtered or derived. But This is not working which is mentioned below. I am using the below mentioned OS uname -a SunOS udora310 5.10 Generic_150400-11 sun4v sparc sun4v param='$1$2' nawk -v para="$param" 'BEGIN... (11 Replies)
Discussion started by: Mohammed Rafi
11 Replies

3. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

4. Shell Programming and Scripting

awk: Print fields between two delimiters on separate lines and send to variables

I have email headers that look like the following. In the end I would like to accomplish sending each email address to its own variable, such as: user1@domain.com='user1@domain.com' user2@domain.com='user2@domain.com' user3@domain.com='user3@domain.com' etc... I know the sed to get rid of... (11 Replies)
Discussion started by: tay9000
11 Replies

5. Shell Programming and Scripting

PRINT TWO VARIABLES IN ONE LINE

my command nawk -F' ' '{for(i=1;i<=NF;i++){if ($i ~ /XX/) print i}}' TOM.bk The output is in two lines as below 12 30 i want the output in one line with comma delimited as below 12,30 the 23 and 79 are column numbers in one line of the file. so i want all the two columns... (1 Reply)
Discussion started by: dealerso
1 Replies

6. UNIX for Dummies Questions & Answers

Print Positional variables

Hi, I have to use the vaious kind of filters based on various fields in the input file like - count occurence of cases where "TRK-GRP" = 169 or like "ADDR-DIG" = 80080. I don;t know the positional variable for all below fields. Please help. Input File : +++ BEST 12-05-27 15:06:49 MDI 3478... (2 Replies)
Discussion started by: vanand420
2 Replies

7. Shell Programming and Scripting

print pattern between two variables awk sed

I am trying to print text between two variables in a file I have tried the following things but none seem to work: awk ' /'$a'/ {flag=1;next} /'$b'/{flag=0} flag { print }' file and also sed "/$a/,/$b/p" file But none seem to work Any Ideas? Thanks in Advance (5 Replies)
Discussion started by: forumbaba
5 Replies

8. Shell Programming and Scripting

For Loop And Print 2 Variables

I have the following loop: for I in `ldm ls |grep -v ^NAME| awk '{print $1}'` do ldm ls -o network $I | echo $I | tr -s ' ' | cut -f6 -d " " | more +9 I would like the following displayed to a file: The value of $I, followed by a newline, then the information from the rest of the... (3 Replies)
Discussion started by: hxman
3 Replies

9. Shell Programming and Scripting

How can I print variables im using in makefiles?

for example in my make file im building path from env variables and string but need to see what is did what is the best way to print the result? say I have in my Makefile : exec_prefix = $(RUN_ENV_LOCAL)/apache and I will like to print the exec_prefix value , how can it be done ? (1 Reply)
Discussion started by: umen
1 Replies

10. Shell Programming and Scripting

cannot print 3 variables on same line

Hi, I have a file containing emails about some issues. Using a bourne shell script, I need to extract the issue number from the URL's contained in the file (all URL's look like this : http://www.openoffice.org/issues/show_bug.cgi?id=1881) and then print out the issue number, status and... (2 Replies)
Discussion started by: Chris Jones
2 Replies
Login or Register to Ask a Question