Visit Our UNIX and Linux User Community

setting a variable, using SSH and awk?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting setting a variable, using SSH and awk?
# 1  
Old 10-27-2009
setting a variable, using SSH and awk?

hi there

I am trying to get a value from a remote machine into a local variable. To get this value i want to use awk but im having trouble getting it to run, am i escaping in the right places here and using the right quotes (i must have tried a million combinations Smilie)

# VAR=`ssh server1 "dmidecode|grep \"Manufacturer:\"|head -1|sed s/^.*Manufacturer:.//|awk {'print \$1'}"`
Sun Microsystems

As you can see, it returns "Sun Microsystems" where, I expected it to return "Sun". It is ignoring my AWK, or at least ignoring the $1 within it

If i run it straight, without putting it into a variable, it works fine (i.e by removing the VAR=` from the front and the ` from the end)

like this

# ssh server1 "dmidecode|grep \"Manufacturer:\"|head -1|sed s/^.*Manufacturer:.//|awk {'print \$1'}"

Could anybody help me out with this?

# 2  
Old 10-27-2009
VAR=`ssh server1 "dmidecode|grep \"Manufacturer:\"|head -1|sed s/^.*Manufacturer:.//|awk '{print \$1}' "`

But you should be able to do it all with ONLY one awk - no need for grep/head/sed/etc...
What's the sample output of 'dmidecode'?
# 3  
Old 10-27-2009
Try to simplify the command first, see if that helps.

VAR=$(ssh server1 "dmidecode | awk '/Manufacturer:/ { print \$2; exit }'")

# 4  
Old 10-27-2009
Hi thanks for the reply, i certianly looks neater than my code Smilie.

---------- Post updated at 09:52 PM ---------- Previous update was at 09:33 PM ----------

I wonder if i could ask a question about the simplification you have done above (which by the way is fab). I have a similar piece of code that uses sed, head, grep and culminates in an awk if statement, is there also a way of simplifying this or would i need to pipe it to another awk statement ?

I am really interested in how you are doing this ? i didn't realise it could be trimmed down this much

this is my current code (which i need to variabize as per your solution above)

VAR=`ssh server1 "/usr/platform/\`uname -i\`/sbin/prtdiag|head -1|sed s/.\*Sun\" \"//|nawk '{ if (\$3 == \"M
2\") { print \$2\$3; } else { print \$2; } }'"`

can this go into one awk command? surely not
# 5  
Old 10-27-2009
Yes it can.

Like before. head -1 = awk exit after first line and the sed like the awk pattern:

(untested - but generally correct!...)
VAR=$(ssh server1 "/usr/platform/\$(uname -i)/sbin/prtdiag | nawk '/.*Sun/ { if (\$3 == \"M2\") print \$2\$3 else print \$2; exit }'")

The "if" part could be simplified a little further, perhaps.
# 6  
Old 10-28-2009
thats perfect, thankyou .... I had to put a few curlies around the if statement to get it working but thank you very much for your help Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #986
Difficulty: Medium
The X Window System is installed by default in FreeBSD.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - Removing extra character when setting variable

I have a data file d0 that looks like this: $cat d0 server1 running -n-cv- 8G 3.1% 1435d 15h server2 running -n---- 8G 39% 660d 22h server3 running -n--v- 8G 2.5% 1173d 6h server4 running -n---- 8G 1.1% 1048d 20h... (2 Replies)
Discussion started by: jake0391S
2 Replies

2. Shell Programming and Scripting

setting a shell script variable in awk

The following is part of a larger shell script grep -v "Col1" my_test.log | grep -v "-" | awk '$5 == "Y" {print $1}' instead of printing, can I set set $1 to a variable that the rest of the shell script can read? if $5 == Y, I want to call another shell script and pass $1 as a... (2 Replies)
Discussion started by: guessingo
2 Replies

3. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

4. Shell Programming and Scripting

awk: Eliminating white space while setting variable

Hi, I have a large flat file from host without delimiter. I'm transforming this file to a csv file using statements like # Row 03: Customer / field position 3059 +20 WOFABNAM=substr( $0, 3059, 20 ); and deleting the trailing whitespaces before and after with that sub( /^ +/, "",... (4 Replies)
Discussion started by: Celald
4 Replies

5. Shell Programming and Scripting

use awk to ssh from variable in flat file

flat file looks like ooss-pfgg-1234,vol_name_1, mail-list decoded = hostname,volum_name,mail_list each line has diff info am trying to ssh into each fist field, check vol usage for second field, and if greater than 90% send mail to mail-list got the second and third part working, ... (1 Reply)
Discussion started by: riegersteve
1 Replies

6. UNIX for Dummies Questions & Answers

setting environment variable in awk

Dear all, I have a data sample... Dose: Summed ROI: Bladder ************************** Bin Dose Volume 001 0.700 100.000 002 0.715 99.998 168 3.142 0.368 169 3.157 0.338 170 3.171 0.292 Dose: Summed ROI:... (2 Replies)
Discussion started by: tintin72
2 Replies

7. Solaris

Setting up SSH - first time

Good day to you all. I have a server (running on SunOS 5.8) that i always got into via telnet. I have eventually decided to block telnet access to it and instead look toward using SSH. The problem is, whne i establish the SSH connection via PuTTy, i get the "Log in as" prompt, but upon... (5 Replies)
Discussion started by: de049
5 Replies

8. Shell Programming and Scripting

awk using ssh variable?

I have a file named Atoms that has a list of atoms listed vertically, like: O C Na etc. There is a variable number of them. I want to count their occurences in another file. I want to do this by saving each atom as a variable, preferabbly in an associative array, then counting how... (3 Replies)
Discussion started by: RisingSun
3 Replies

9. AIX

ssh setting

I would like to implement the secure shell environment in order for me to close all telnet and ftp ports. 1) Anyone can assist to give me a steps of what to do so that I can implement the ssh on my AIX5.3 server. 2) Currently I have installed putty in my PC to replace telnet directly into the... (11 Replies)
Discussion started by: kwliew999
11 Replies

10. UNIX for Dummies Questions & Answers

Setting SSH port

How can I switch the port which SSH listens on? (1 Reply)
Discussion started by: Spetnik
1 Replies

Featured Tech Videos