LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-26-2014, 05:28 AM   #1
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Rep: Reputation: Disabled
awk or sed to print particular word


Hi All,
I need to print below bold words.
Code:
# cat /tmp/ping 
PING google.com (74.125.68.113) from 192.168.1.2 eth0: 56(84) bytes of data.
64 bytes from 74.125.68.113: icmp_seq=1 ttl=46 time=63.9 ms
64 bytes from 74.125.68.113: icmp_seq=2 ttl=46 time=64.1 ms
64 bytes from 74.125.68.113: icmp_seq=3 ttl=46 time=63.1 ms

--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 63.127/63.735/64.159/0.528 ms
I don't want to go with specific field like '{ print $7 }' because the "time=" word sometime in the 7th field and sometime in the 8th field. So expecting your kind help to print always time= word.

Last edited by cli; 09-26-2014 at 05:36 AM.
 
Old 09-26-2014, 05:55 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
Code:
egrep -o 'time=[0-9]+.[0-9]' t.t
time=63.9
time=64.1
time=63.1
 
2 members found this post helpful.
Old 09-26-2014, 06:11 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,275
Blog Entries: 12

Rep: Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862Reputation: 1862
While not awk or sed, using a bash script, you can do the following to split it into an array. Then you can loop based on the number of indices in the array and then test each element to determine if it contains the "time=" term.
Code:
#!/bin/bash

string='64 bytes from 74.125.68.113: icmp_seq=1 ttl=46 time=63.9 ms'

IFS=' ' read -a array <<< "$string"

echo "Input string = $string"
echo "Index Count  = ${#array[@]}"
echo "Element 0    = ${array[0]}"
echo "Element 1    = ${array[1]}"
echo "Element 2    = ${array[2]}"
echo "Element 3    = ${array[3]}"
echo "Element 4    = ${array[4]}"
echo "Element 5    = ${array[5]}"
echo "Element 6    = ${array[6]}"
echo "Element 7    = ${array[7]}"
Sample output:
Code:
Input string = 64 bytes from 74.125.68.113: icmp_seq=1 ttl=46 time=63.9 ms
Index Count  = 8
Element 0    = 64
Element 1    = bytes
Element 2    = from
Element 3    = 74.125.68.113:
Element 4    = icmp_seq=1
Element 5    = ttl=46
Element 6    = time=63.9
Element 7    = ms
The way this is written, it specifically has to be bash. At some long ago point I learned this and know that I always have to do #!/bin/bash.

In fact a quick test today where I converted that to #!/bin/sh I got errors, it doesn't like the redirection when taking in $string into the array.
 
1 members found this post helpful.
Old 09-26-2014, 06:16 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,350

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
sed 's/.*time/time/;s/ ms.*//'
 
1 members found this post helpful.
Old 09-26-2014, 06:41 AM   #5
mandyapenguin
Member
 
Registered: Nov 2011
Location: India
Distribution: RedHat, Cent OS, Fedora, Debian, Ubuntu
Posts: 106

Rep: Reputation: Disabled
If "time=" is always in 2nd field from the end then you may try
# grep "time=" /tmp/ping | awk '{print $(NF-1)}'
time=44.5
time=44.8
time=43.3
 
1 members found this post helpful.
Old 09-26-2014, 06:43 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,350

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
yes, that can be used, but without grep (if you want to filter):
awk '/time=/ {print $(NF-1)}'
 
1 members found this post helpful.
Old 09-26-2014, 10:59 AM   #7
cli
Member
 
Registered: Apr 2013
Distribution: RedHat, Cent, Ubuntu
Posts: 80

Original Poster
Rep: Reputation: Disabled
Hi Thanks to all.
Both worked fine.
Code:
grep "time=" /tmp/ping | awk '{print $(NF-1)}'
awk '/time=/ {print $(NF-1)}' /tmp/ping
 
Old 09-27-2014, 11:19 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,350

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
yes, but the first one needs much more resources
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question regarding awk or sed to find and replace a word in Solaris for Scripting karthik1asr Linux - Newbie 3 03-29-2013 11:40 AM
[SOLVED] awk/grep/sed/print......etc dotran Linux - Newbie 2 08-08-2012 04:41 PM
[SOLVED] sed/awk to print every before line that pattern match niharikaananth Linux - Newbie 10 02-22-2012 10:47 PM
awk print word on each line on 3 column mad_penguin Programming 9 09-22-2011 05:59 AM
I need to parse a word: awk or sed? mehesque Programming 5 07-27-2004 04:23 PM


All times are GMT -5. The time now is 05:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration