Linux - 从第二个选项卡获取文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11132763/
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
Linux - get text from second tab
提问by bandit
Suppose that we have file like this:
假设我们有这样的文件:
sometext11 sometext12 sometext13 sometext21 sometext22 sometext23
sometext11 sometext12 sometext13 sometext21 sometext22 sometext23
Texts are separated by tabs and we know sometext from column 1 but want to get text from column 2. I know I can get line by:
文本由制表符分隔,我们知道第 1 列中的一些文本,但想要从第 2 列中获取文本。我知道我可以通过以下方式获取行:
grep 'sometext11' file.txt
How to get text from second column? Maybe some tool with option -t [column nr]?
如何从第二列获取文本?也许一些带有选项 -t [column nr] 的工具?
采纳答案by Fredrik Pihl
awk:
awk:
awk '{print }' file.txt
cut:
切:
cut -f2 file.txt
bash:
重击:
while read -a A; do echo ${A[1]}; done < file.txt
perl:
珀尔:
perl -lane 'print $F[1]' file.txt
If you know the string you are grepping for, you can use grep
:
如果您知道要搜索的字符串,则可以使用grep
:
grep -o 'sometext12' file.txt
回答by Gryphius
awk '{print }' < yourfile
回答by Paused until further notice.
You don't need grep
:
你不需要grep
:
awk '/sometext11/ {print }' file.txt
or you can do it all in grep
if yours supports Perl Compatible Regular Expressions (PCRE), such as GNU or OS X grep
:
或者,grep
如果您的支持 Perl 兼容正则表达式 (PCRE),例如 GNU 或 OS X ,则您可以全部完成grep
:
grep -Po '(?<=sometext11\t).*?(?=\t.*)' file.txt