使用 grep 和 cut 定界符命令(在 bash shell 脚本 UNIX 中) - 并且有点“反转”它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17202221/
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-09 23:51:14  来源:igfitidea点击:

Using the grep and cut delimiter command (in bash shell scripting UNIX) - and kind of "reversing" it?

bashunixscriptinggrepcut

提问by DeaIss

So I have a file with the text:

所以我有一个带有文本的文件:

puddle2_1557936:/home/rogers.williams/folderz/puddle2

I want to use the grep command

我想使用 grep 命令

grep puddle2_1557936

Mixed in with the cut command (or another command if neccessary) to display just this part:

与 cut 命令(或其他命令,如果需要)混合以仅显示此部分:

/home/rogers.williams/folderz/puddle2

So far, I know that if do this

到目前为止,我知道如果这样做

 grep puddle2_1557936 | cut -d ":" -f1

then it will display

然后它会显示

puddle2_1557936

So is there anyway to kind of "inverse" the delimiter cut command?

那么无论如何要“反转”定界符剪切命令?

NOTE: The solution muststart off with grep puddle2_15579636.

注意:解决方案必须grep puddle2_15579636.

回答by Gilles Quenot

You don't need to change the delimiter to display the right part of the string with cut.

您无需更改分隔符即可使用cut.

The -fswitch of the cutcommand is the n-TH element separated by your delimiter : :, so you can just type :

命令的-f开关cut是由分隔符 : 分隔的 n-TH 元素:,因此您只需键入:

 grep puddle2_1557936 | cut -d ":" -f2


Another solutions (adapt it a bit) if you want fun :

如果您想玩得开心,另一种解决方案(稍微调整一下):

Using grep:

使用grep

grep -oP 'puddle2_1557936:\K.*' <<< 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'                                                                        
/home/rogers.williams/folderz/puddle2

or still with look aroundregex

或者仍然环顾正则表达式

grep -oP '(?<=puddle2_1557936:).*' <<< 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'                                                                    
/home/rogers.williams/folderz/puddle2

or with perl:

或使用perl

perl -lne '/puddle2_1557936:(.*)/ and print ' <<< 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'                                                      
/home/rogers.williams/folderz/puddle2

or using ruby(thanks to glenn Hymanman)

或使用ruby(感谢glenn Hymanman

ruby -F: -ane '/puddle2_1557936/ and puts $F[1]' <<< 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'
/home/rogers.williams/folderz/puddle2

or with awk:

或使用awk

awk -F'puddle2_1557936:' '{print }'  <<< 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'
/home/rogers.williams/folderz/puddle2

or with python:

或使用python

python -c 'import sys; print(sys.argv[1].split("puddle2_1557936:")[1])' 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'
/home/rogers.williams/folderz/puddle2

or using only bash:

或仅使用bash

IFS=: read _ a <<< "puddle2_1557936:/home/rogers.williams/folderz/puddle2"
echo "$a"
/home/rogers.williams/folderz/puddle2

or using jsin a shell:

或在shell 中使用js

js<<EOF
var x = 'puddle2_1557936:/home/rogers.williams/folderz/puddle2'
print(x.substr(x.indexOf(":")+1))
EOF
/home/rogers.williams/folderz/puddle2

or using phpin a shell:

或在shell 中使用php

php -r 'preg_match("/puddle2_1557936:(.*)/", $argv[1], $m); echo "$m[1]\n";' 'puddle2_1557936:/home/rogers.williams/folderz/puddle2' 
/home/rogers.williams/folderz/puddle2