What's wrong with my sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What's wrong with my sed?
# 1  
Old 05-02-2012
What's wrong with my sed?

Code:
lyang0@lyang0-OptiPlex-755:~$ cat xx
Settings for eth1:
	Supported ports: [ TP ]
	Supported link modes:   10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Full 
	Supports auto-negotiation: Yes
	Advertised link modes:  10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Full 
	Advertised pause frame use: No
	Advertised auto-negotiation: Yes
	Speed: 1000Mb/s
	Duplex: Full
	Port: Twisted Pair
	PHYAD: 1
	Transceiver: internal
	Auto-negotiation: on
	MDI-X: off
	Supports Wake-on: pumbg
	Wake-on: g
	Current message level: 0x00000001 (1)
			       drv
	Link detected: yes

Code:
lyang0@lyang0-OptiPlex-755:~$ cat xx |grep "Speed" |sed 's/.* \([0-9]*\)Mb.*/\1/g'
1000

but I don't want to use the second pipe, so I use below cmd, but I got un-expected ansower, anyone correct me?
Code:
lyang0@lyang0-OptiPlex-755:~$ cat xx |sed '/Speed/s/.* \([0-9]*\)Mb.*/\1/g'
Settings for eth1:
	Supported ports: [ TP ]
	Supported link modes:   10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Full 
	Supports auto-negotiation: Yes
	Advertised link modes:  10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Full 
	Advertised pause frame use: No
	Advertised auto-negotiation: Yes
1000
	Duplex: Full
	Port: Twisted Pair
	PHYAD: 1
	Transceiver: internal
	Auto-negotiation: on
	MDI-X: off
	Supports Wake-on: pumbg
	Wake-on: g
	Current message level: 0x00000001 (1)
			       drv
	Link detected: yes

# 2  
Old 05-02-2012
Hi, try:

Code:
sed -n '/Speed/s/.* \([0-9]*\)Mb.*/\1/gp' xx

alternatively:
Code:
awk '/Speed/{print $2+0}' xx

# 3  
Old 05-02-2012
try with awk

Code:
 
$ nawk '/Speed/ {sub("Mb/s","",$2);print $2}' test.txt
1000

# 4  
Old 05-02-2012
Quote:
Originally Posted by Scrutinizer
Hi, try:

Code:
sed -n '/Speed/s/.* \([0-9]*\)Mb.*/\1/gp' xx

alternatively:
Code:
awk '/Speed/{print $2+0}' xx

Greate it works, can you explain the $2+0, I don't understand it, or I google something I don't know
# 5  
Old 05-02-2012
Hi, by adding the number 0 to the string contained in $2, it is forced into numeric context, so "Mb/s" gets dropped and 1000 remains...
This would be an alternative way of doing the same..
Code:
awk '/Speed/{print int($2)}' xx

# 6  
Old 05-02-2012
Quote:
Originally Posted by Scrutinizer
Hi, by adding the number 0 to the string contained in $2, it is forced into numeric context, so "Mb/s" gets dropped and 1000 remains...
This would be an alternative way of doing the same..
Code:
awk '/Speed/{print int($2)}' xx

Thanks for the explain, I never see this usage before in the book, My book is bad
# 7  
Old 05-02-2012
Maybe it is not so much bad, but it chooses to show examples that are perhaps a bit easier to understand. The same could have been achieved with the sub function like itkamaraj showed, which may be the most accessible method. There are just many ways of doing it, it could for example also be done with the field separator
Code:
awk -F': |Mb/s' '/Speed/{print $2}' infile

or
Code:
awk -F'Speed: |Mb/s' 'NF==3{print $2}' infile

or with split
Code:
awk '/Speed/{split($2,T,"Mb/s"); print T[1]}' infile


Last edited by Scrutinizer; 05-02-2012 at 07:37 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Same sed code prints(p) correct, but writtes(w) wrong output

Dear all, I am using sed as an alternative to grep in order to get a specific line from each of multiple files located in the same directory. I am using sed because it prints the lines in the correct order (unlike grep). When I write sed code that prints out the output I get it correct, but... (1 Reply)
Discussion started by: JaNaJaNa
1 Replies

2. Post Here to Contact Site Administrators and Moderators

Whether anything went wrong ?

Hi could see all the post with this old color scheme ( legacy (dark) vBulletin color scheme (unsupported)) except this post, please look at the screenshot . Looks like url parsing also some problem is there getting "Page not found" error. -- Akshay --edit-- screenshot is there in album... (18 Replies)
Discussion started by: Akshay Hegde
18 Replies

3. UNIX for Dummies Questions & Answers

What is wrong with: echo $PATH | sed s/:/\\n/g

Hello all! I am on Mac (10.8.4) and my shell tcsh (man says version: Astron 6.17.00). Just to precise my tcsh: echo $LC_CTYPE UTF-8 I want to replace all ':' with a new line, to get all paths on one line. I don't find a way to make my shell accept the "\n" My start was: echo... (17 Replies)
Discussion started by: marek
17 Replies

4. Shell Programming and Scripting

Why result is wrong here ? whether break statement is wrong ?

Hi ! all I am just trying to check range in my datafile pls tell me why its resulting wrong admin@IEEE:~/Desktop$ cat test.txt 0 28.4 5 28.4 10 28.4 15 28.5 20 28.5 25 28.6 30 28.6 35 28.7 40 28.7 45 28.7 50 28.8 55 28.8 60 28.8 65 28.1... (2 Replies)
Discussion started by: Akshay Hegde
2 Replies

5. Shell Programming and Scripting

What's wrong my sed?

$ cat file1 gcc-configure-sdk, gcc-configure-cross: Dont recomput libgcc: Remove unpackage unwind.h $ cat file2 fc94926 gcc-configure-sdk, gcc-configure-cross: Dont recomput f7ec6ea libgcc: Remove unpackage unwind.h f800988 balbababababa $ cat ./xx.sh #!/bin/bash while read... (10 Replies)
Discussion started by: yanglei_fage
10 Replies

6. Shell Programming and Scripting

SED to delete last word on line...what's wrong?

I have a line that gets pulled from a database that has a variable number of fields, fields can also be of a variable size. Each field has a variable number of spaces between them so there is no 'defined' delimiter. The LastData block is always a single word. What I want to do is delete the... (2 Replies)
Discussion started by: Bashingaway
2 Replies

7. UNIX for Dummies Questions & Answers

What is wrong in here ???

]#PATH=/usr/bin:/etc:/bin:/boot/grub:/boot/grup/bin: /boot/solaris/bin:/sbin:/usr/openwin/bin:/usr/5bin://usr/X11/bin:/usr/apache/bin:/usr/apache2/bin:/usr/appserver/bin:... (9 Replies)
Discussion started by: microbot
9 Replies

8. Shell Programming and Scripting

What am I doing wrong?

Trying to run a script as a different user (sudo userx inside the script) but after I call the script it just sits in memory until I <ctrl D>. If I do whoami before the <ctrl D> the system returns the userid that was invoked in the script. Is there any way to get around this behavior? TIA,... (3 Replies)
Discussion started by: gwfay
3 Replies

9. Shell Programming and Scripting

What's wrong with this sed command? delete & append

I want to write a sed command that does the following work: file: <a>asdfasdf<\s> <line>hello</line> <b>adf<\c> <b>tttttttt<\c> output: name=hello sed -e 's/^*//' -n -e '/<line>/s/<*>//gp;' -e 's/^/name="/g' file but I can not append "=" after getting the line with... (5 Replies)
Discussion started by: minifish
5 Replies

10. UNIX for Dummies Questions & Answers

am i wrong?

knew nothing about unix - linux untill a week ago when i started out to learn the basics. I think i'm aproaching unix wrong, i'm trying to compare unix with dos, searching similar commands. Did some great things for dumbass newbie like writing scripts to mount cdrom and floppy, that's the... (4 Replies)
Discussion started by: termiEEE
4 Replies
Login or Register to Ask a Question