如何在Linux bash中提取某个字符出现在第n个和第m个之间的字符串? [英] How to extract strings between nth and mth occurence of a certain character in linux bash?

查看:27
本文介绍了如何在Linux bash中提取某个字符出现在第n个和第m个之间的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件1包含:

a:b:c:d:any words here:e:f:G

w/r此处有任何单词"可以是一个单词,两个单词,三个单词,依此类推.

w/r "any words here" can be a single word, two words, three words, and so on.

我想获取第4个:"和第5个:"之间的字符串.因此,这将是这里的任何单词".

I want to get the string between the 4rd ":" and the 5th ":". So, that will be "any words here".

我最初的想法是用空格替换:",然后使用awk打印..但是由于我要提取的字符串可以由多个单词组成,因此无法正常工作.

My initial idea was to replace ":" with space then, use awk to print.. but since the string i want to extract can be composed of multiple words, it will not accurately work.

推荐答案

cut 命令允许您基于定界符分割行,并从中提取必填字段

cut command allow you to split a line based on a delimiter, and extract required fields from it

在您的示例中,

> echo 'a:b:c:d:any words here:e:f:G' |cut -f 5 -d:

应该给你

any words here

这篇关于如何在Linux bash中提取某个字符出现在第n个和第m个之间的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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