从一列中提取域,同时保留其他列

MKo*_*ona 11 regex grep

我有一个包含三列的文件,如下所示:

0       1612291061      http://www.staropolska.pl/
0       1612450417      http://m.kerygma.pl/
6831926761338023936     1612171787      http://www.kerygma.pl/hermeneutyka-biblijna/377-ksiegi-starego-testamentu-mini-streszczenie
6867871457052077056     1612534199      http://www.kerygma.pl/katechizm-kkk/kkk-iv-modlitwa/538-kkk-2558-2565
Run Code Online (Sandbox Code Playgroud)

我想从第三列中提取域,同时保留前两列,所以我想要一个看起来像这样的文件:

0       1612291061      http://www.staropolska.pl
0       1612450417      http://m.kerygma.pl
6831926761338023936     1612171787      http://www.kerygma.pl
6867871457052077056     1612534199      http://www.kerygma.pl
Run Code Online (Sandbox Code Playgroud)

到目前为止,我能够使用 grep 提取域:

cat file.txt | grep -Eo '(http|https)://[^/"]+'
Run Code Online (Sandbox Code Playgroud)

但这只给了我第三列中的域:

http://www.staropolska.pl
http://m.kerygma.pl
http://www.kerygma.pl
http://www.kerygma.pl
Run Code Online (Sandbox Code Playgroud)

不打印前两个。

Chr*_*itz 7

另一种选择是cut/用作分隔符:

$ cat file.txt | cut -d '/' -f 1-3
0       1612291061      http://www.staropolska.pl
0       1612450417      http://m.kerygma.pl
6831926761338023936     1612171787      http://www.kerygma.pl
6867871457052077056     1612534199      http://www.kerygma.pl
Run Code Online (Sandbox Code Playgroud)


anu*_*ava 6

您只需要允许grep正则表达式匹配之前的任何内容https?://

grep -Eo '.*[[:blank:]]https?://[^/"]+' file

0       1612291061      http://www.staropolska.pl
0       1612450417      http://m.kerygma.pl
6831926761338023936     1612171787      http://www.kerygma.pl
6867871457052077056     1612534199      http://www.kerygma.pl
Run Code Online (Sandbox Code Playgroud)

正则表达式解释:

  • .*: 匹配 0 个或多个任意字符
  • [[:blank:]]: 匹配一个空格或制表符
  • https?: 匹配httpshttp
  • ://: 比赛 ://
  • [^/"]+: 匹配任何不是 a/和 a 的字符的 1+"

或者,你也可以试试这个sed

sed -E 's~([[:blank:]]https?://[^/"]+).*~\1~' file
Run Code Online (Sandbox Code Playgroud)


Rav*_*h13 6

使用 中显示的样本awk,请尝试以下操作。

awk 'match($0,/.*http[s]?:\/\/[^/]*/){print substr($0,RSTART,RLENGTH)}' Input_file
Run Code Online (Sandbox Code Playgroud)

说明:为以上添加详细说明。

awk '                                ##Starting awk program from here.
match($0,/.*http[s]?:\/\/[^/]*/){    ##Using match function to match regex from starting to till http/https:// till next / here.
  print substr($0,RSTART,RLENGTH)    ##Printing sub string of matched regex here.
}
' Input_file                         ##Mentioning Input_file name here.
Run Code Online (Sandbox Code Playgroud)