English 中文(简体)
利用指挥系统提取格局
原标题:Extracting a pattern using sed command
  • 时间:2011-11-21 07:59:54
  •  标签:
  • sed

我有这样的经历。

 "112344 1234234 guest 25 % allocation used"

我愿在座标中选取25

在“112344 1234234位客座拨款25%”这一特定项目中,我只有兴趣“25%”。 第一个领域也可能是空洞的,例如:

使用的“25%”拨款

but " % allocation used" is fixed string. So the regex should be based on this fixed pattern.

任何人都可以帮助我。

最佳回答

引言

 cut -d% -f1 | awk  { print $(NF) } 
问题回答

从根本上说,你们想要的是第四点。 为此,我使用<代码>awk:

awk  { print $(NF-3) } 

如果绝对必须列入<条码><>。

sed -e  s/^/ /  -e  s/(^|.* )(.*) % allocation used/1/ 

如果你确信在百分比之前的数字是2位数,那么你可以使用以下数字:

[jaypal~/Temp]$ cat text5
112344 1234234 guest 25 % allocation used
25 % allocation used
2344 guest 15 % allocation used

[jaypal~/Temp]$ sed  s/.*(.. %).*/1/  text5
25 %
25 %
15 %




相关问题
how to tell sed "dot match new line"

I can t figure how to tell sed dot match new line: echo -e "one two three" | sed s/one.*two/one/m I expect to get: one three instead I get original: one two three

sed: using search pattern in output

I d like to use the search pattern in the output part of a sed command. For instance, given the following sed command: sed /HOSTNAME=/cHOSTNAME=fred /etc/sysconfig/network I d like to replace the ...

Text manipulation and removal

I have text files generated by one of my tools with structure shown below. 1 line text (space) multiple lines text (space) multiple lines text nr 2 ----------------------------------------------------...

Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

how to use sed, awk, or gawk to print only what is matched?

I see lots of examples and man pages on how to do things like search-and-replace using sed, awk, or gawk. But in my case, I have a regular expression that I want to run against a text file to extract ...

Bulk Insert Code Before </body> Tag in 100 Files

I d like to insert <?php include_once( google_analytics.php ); ?> before the closing body tag of about 100 php files. Unfortunately the person who made the site didn t make a header or ...

Extract float from text line with sed?

I am issuing a sed replace on a line, trying to extract a particular floating-point value, but all that appears to be matched is the right-side of the decimal Text Line: 63.544: [GC 63.544: [DefNew: ...

热门标签