Solved Doubt about awk output

December 11, 2014 at 06:26:07
Specs: Solaris 10 10/08, T5240
Gentlemen,

I have the following file in the remote server A:
# cat input.txt
a b c d e f g h i j l m n o

Doubt: How can I print just the column 11 ? (the result must b, in this case, m)

I have tried the following without success:

# ssh A "cat input.txt | awk '{ print $11 }'"
1

or

# ssh A "cat input.txt | awk '{test=11} { print $test }'"
a b c d e f g h i j l m n o p q r s t u v


Does anyone know the solution ?



See More: Doubt about awk output

Report •


✔ Best Answer
December 15, 2014 at 08:48:26
I just realized that the $ is a special character in ssh, therefore it has to be escaped. Notice the \$11 below:

ssh nails@putz150 "cat /tmp/input.txt | awk '{sum+=\$11} END {print sum}'"

My ssh experience is limited but I'll bet other characters like ~ . would also cause a problem and have to be escaped.

My suggestion to you is to still place your awk script in a script file.



#1
December 11, 2014 at 10:08:51
I'd use some variation of
cut -f 11 input.txt

man cut

How To Ask Questions The Smart Way


Report •

#2
December 11, 2014 at 11:01:33
Your awk program is correct; you are interpreting your data incorrectly. The letter "m" in your data is column 12 - not column 11.

BTW, if you choose to use the cut command in Solaris 10, you must provide a delimiter:

cut -d " " -f 11 input.txt


Report •

#3
December 12, 2014 at 08:58:20
Hi Guys, thanks for the answer.

But i need to use awk because i need to sum the column 11 of all lines.

# cat input.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
55 56 57 58 59 101 102 103 104 110 110 111 112 113 115

The command should be like this:
cat input.txt | awk -F"|" '{sum+=$11} END {print sum}'

But, always i have tried running it remotely with ssh, the output is wrong.

Example:
# ssh A "cat /tmp/input.txt"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
55 56 57 58 59 101 102 103 104 110 110 111 112 113 115

# ssh A "cat /tmp/input.txt | awk -F' ' '{sum+=$11} END {print sum}'"
3

:(


Report •

Related Solutions

#4
December 12, 2014 at 09:48:06
First, I execute your awk script:

cat /tmp/input.txt | awk -F' ' '{sum+=$11} END {print sum}'

and it returns 121.

second, it looks like your problem is with the ssh implementation. I am assuming the "A" in the ssh command:

ssh A "cat /tmp/input.txt"

is the user-remote server string in the form of:

user@remote-host

Try debuggng your ssh connection with

ssh -v user@remote-host

Maybe you can get an idea what your connection issue is.


Report •

#5
December 12, 2014 at 09:59:19
Hi Nails,

"A" is a remote server I used just as example:

Bellow a real environment:

# ssh spjag01ium8 "cat /tmp/a"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
55 56 57 58 59 101 102 103 104 110 110 111 112 113 115

=== 2014Dec08 15:49:21 root@smcta35 /
# ssh spjag01ium8 "cat /tmp/a | awk -F' ' '{sum+=$11} END {print sum}'"
3


There is not connection issue:
# ssh -v spjag01ium8 "cat /tmp/a | awk -F' ' '{sum+=$11} END {print sum}'"
Sun_SSH_1.1.2, SSH protocols 1.5/2.0, OpenSSL 0x0090704f
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Rhosts Authentication disabled, originating port will not be trusted.
debug1: ssh_connect: needpriv 0
debug1: Connecting to spjag01ium8 [10.108.64.99] port 22.
debug1: Connection established.
debug1: identity file /.ssh/identity type -1
debug1: identity file /.ssh/id_rsa type 1
debug1: identity file /.ssh/id_dsa type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_4.3
debug1: match: OpenSSH_4.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-Sun_SSH_1.1.2
debug1: use_engine is 'yes'
debug1: pkcs11 engine initialized, now setting it as default for RSA, DSA, and symmetric ciphers
debug1: pkcs11 engine initialization complete
debug1: Failed to acquire GSS-API credentials for any mechanisms (No credentials were supplied, or the credentials were unavailable or inaccessible
Unknown code 0
)
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: Peer sent proposed langtags, ctos:
debug1: Peer sent proposed langtags, stoc:
debug1: We proposed langtags, ctos: i-default
debug1: We proposed langtags, stoc: i-default
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: dh_gen_key: priv key bits set: 121/256
debug1: bits set: 987/2048
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'spjag01ium8' is known and matches the RSA host key.
debug1: Found key in /.ssh/known_hosts:841
debug1: bits set: 1070/2048
debug1: ssh_rsa_verify: signature correct
debug1: newkeys: mode 1
debug1: set_newkeys: setting new keys for 'out' mode
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: newkeys: mode 0
debug1: set_newkeys: setting new keys for 'in' mode
debug1: SSH2_MSG_NEWKEYS received
debug1: done: ssh_kex2.
debug1: send SSH2_MSG_SERVICE_REQUEST
debug1: got SSH2_MSG_SERVICE_ACCEPT
debug1: Authentications that can continue: publickey,gssapi-with-mic,password
debug1: Next authentication method: gssapi-with-mic
debug1: Failed to acquire GSS-API credentials for any mechanisms (No credentials were supplied, or the credentials were unavailable or inaccessible
Unknown code 0
)
debug1: Next authentication method: publickey
debug1: Trying private key: /.ssh/identity
debug1: Trying public key: /.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 149 lastkey 708d0 hint 1
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey)
debug1: channel 0: new [client-session]
debug1: send channel open 0
debug1: Entering interactive session.
debug1: ssh_session2_setup: id 0
debug1: channel request 0: env
debug1: channel request 0: env
debug1: channel request 0: env
debug1: channel request 0: env
debug1: channel request 0: env
debug1: channel request 0: env
debug1: Sending command: cat /tmp/a | awk -F' ' '{sum+=1} END {print sum}'
debug1: channel request 0: exec
debug1: channel 0: open confirm rwindow 0 rmax 32768
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug1: channel 0: rcvd eof
debug1: channel 0: output open -> drain
debug1: channel 0: rcvd close
debug1: channel 0: close_read
debug1: channel 0: input open -> closed
3
debug1: channel 0: obuf empty
debug1: channel 0: close_write
debug1: channel 0: output drain -> closed
debug1: channel 0: almost dead
debug1: channel 0: gc: notify user
debug1: channel 0: gc: user detached
debug1: channel 0: send close
debug1: channel 0: is dead
debug1: channel 0: garbage collecting
debug1: channel_free: channel 0: client-session, nchannels 1
debug1: Transferred: stdin 0, stdout 0, stderr 0 bytes in 0.1 seconds
debug1: Bytes per second: stdin 0.0, stdout 0.0, stderr 0.0
debug1: Exit status 0


Report •

#6
December 12, 2014 at 10:21:53
I set up a similar ssh test in my Solaris 10 environment and I have the same problem you do. I'll look into it further when I have some time.

Report •

#7
December 12, 2014 at 10:59:33
Ok Nails ... I will be glad to you !! :)

Report •

#8
December 14, 2014 at 20:45:00
I still don't know why ssh does not work as you wrote it.

However, if you place the awk script in a shell script it works. Let me explain: On the destination machine (putz150 on my machine), create this script called b.ss:

#!/bin/ksh

# b.ss:
# No error checking. script needs data file name
cat $1 | awk '{sum+=$11} END {print sum}'

On the source machine, call the script like this:

ssh nails@putz150 "/tmp/b.ss /tmp/input.txt"

If I figure out anything else, I'll let you know.



Report •

#9
December 15, 2014 at 08:48:26
✔ Best Answer
I just realized that the $ is a special character in ssh, therefore it has to be escaped. Notice the \$11 below:

ssh nails@putz150 "cat /tmp/input.txt | awk '{sum+=\$11} END {print sum}'"

My ssh experience is limited but I'll bet other characters like ~ . would also cause a problem and have to be escaped.

My suggestion to you is to still place your awk script in a script file.


Report •

#10
December 16, 2014 at 09:00:22
you are the guy nails !!! Thanks !!!

Report •


Ask Question