在终端中使用 RegEx 从字符串中提取字符串 [英] Extract string from string using RegEx in the Terminal

查看:25
本文介绍了在终端中使用 RegEx 从字符串中提取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 stringfirst url, second url, 第三个 url 并且想只提取 url 词后的 >second 在 OS X 终端中(仅第一次出现).我该怎么做?

I have a string like first url, second url, third url and would like to extract only the url after the word second in the OS X Terminal (only the first occurrence). How can I do it?

我最喜欢的编辑器中,我使用了正则表达式 /second (url)/ 并使用 $1 来提取它,我只是不知道如何在终端中进行操作.

In my favorite editor I used the regex /second (url)/ and used $1 to extract it, I just don't know how to do it in the Terminal.

请记住,url 是一个实际的 url,我将使用以下表达式之一来匹配它:正则匹配 URL

Keep in mind that url is an actual url, I'll be using one of these expressions to match it: Regex to match URL

推荐答案

echo 'first url, second url, third url' | sed 's/.*second//'

我误解了.更好:

echo 'first url, second url, third url' | sed 's/.*second ([^ ]*).*/1/'

或:

echo 'first url, second url, third url' | perl -nle 'm/second ([^ ]*)/; print $1'

这篇关于在终端中使用 RegEx 从字符串中提取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆