bash 使用 sed 命令从文本文件中提取字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11273038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:38:55  来源:igfitidea点击:

Strings extraction from text file with sed command

stringbashshellsed

提问by user1492786

I have a text file which contains some lines as the following:

我有一个文本文件,其中包含以下几行:

ASDASD2W 3ASGDD12 SDADFDFDDFDD W11 ACC=PNO23 DFSAEFEA EAEDEWRESAD ASSDRE 
AERREEW2 3122312 SDADDSADADAD W12 ACC=HH34 23SAEFEA EAEDEWRESAD ASEEWEE 
A15ECCCW 3XCXXF12 SDSGTRERRECC W43 ACC=P11 XXFSAEFEA EAEDEWRESAD ASWWWW 
ASDASD2W 3122312 SDAFFFDEEEEE SD3 ACC=PNI22 ABCEFEA EAEDEWRESAD ASWEDSSAD 
...

I have to extract the substring between the '=' character and the following blank space for each line , i.e.

我必须为每一行提取 '=' 字符和以下空格之间的子字符串,即

PNO23
HH34
P11
PNI22

I've been using the sedcommand but cannot figure out how to ignore all characters following the blank space.

我一直在使用sed命令,但无法弄清楚如何忽略空格后面的所有字符。

Any help?

有什么帮助吗?

采纳答案by Ignacio Vazquez-Abrams

Use the right tool for the job.

为工作使用正确的工具。

$ awk -F '[= ]+' '{ print  }' input.txt
PNO23
HH34
P11
PNI22

回答by Jo So

Sorry, but have to add another one because I feel the existing answers are just to complicated

对不起,但必须添加另一个,因为我觉得现有的答案太复杂了

sed 's/.*=//; s/ .*//;' inputfile

回答by potong

This might work for you:

这可能对你有用:

sed -n 's/.*=\([^ ]*\).*//p' file

or, if you prefer:

或者,如果您愿意:

sed 's/.*=\([^ ]*\).*//p;d' file

回答by Sisay Chala

A chain of grep can do the trick.

一连串的 grep 可以解决问题。

grep -o '[=][a-zA-Z0-9]*' file | grep -o '[a-zA-Z0-9]*'

回答by tripleee

Put the string you want to capture in a backreference:

将要捕获的字符串放在反向引用中:

sed 's/.*=\([^ =]*\) .*//'

or do the substitution piecemeal;

或者逐步替换;

sed -e 's/.*=//' -e 's/ .*//'

回答by Paused until further notice.

sed 's/[^=]*=\([^ ]*\) .*//' inputfile

Match all the non-equal-sign characters and an equal sign. Capture a sequence of non-space characters. Match a space and the rest of the line. Substitute the captured string.

匹配所有非等号字符和一个等号。捕获一系列非空格字符。匹配一个空格和该行的其余部分。替换捕获的字符串。