在 Bash/sed 中分隔字符 [英] Separating characters in Bash/sed

查看:26
本文介绍了在 Bash/sed 中分隔字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的文件:

09/03/2018 t38940 "https:/dsdsadasdsa.dsadsa.dsadsa
09/03/2018 x38940 "https:/dsadas.dsad.dsa
09/03/2018 d38940 "https:/dasdsa.dsadas.dsadsa
09/03/2018 (38940 "https:/dsadas.dsadasd.dsa
09/03/2015 )38940 "https:/dsds.dasdas.d

我想得到结果:

09/03/2018 38940 "https:/dsdsadasdsa.dsadsa.dsadsa
09/03/2018 38940 "https:/dsadas.dsad.dsa
09/03/2018 38940 "https:/dasdsa.dsadas.dsadsa
09/03/2018 38940 "https:/dsadas.dsadasd.dsa
09/03/2015 38940 "https:/dsds.dasdas.d

我想删除第 2 列开头的数字和字符(始终只有一个字符).

I want to remove numbers and characters attached to the 2nd column on the beginning (it is always only one character).

我如何编写 sed 命令来获得它?

How can I write a sed command to get that?

推荐答案

我假设您的列始终由一个空格分隔.

I'm assuming that your columns are always separated by a single space.

捕获第一列\([^ ]* \)(零个或多个非空格字符,后跟一个空格),并忽略下一个字符.(不要将其包含在替换中):

Capture the first column \([^ ]* \) (zero or more non-space characters, followed by a space), and ignore the next character . (don't include it in the replacement):

sed 's/\([^ ]* \)./\1/' file

更一般地,要对第 N 列执行此操作,然后捕获 N-1 次重复,例如:

More generally, to do this for the Nth column, then capture N-1 repetitions, e.g.:

sed 's/\(\([^ ]* \)\{2\}\)./\1/' file

将删除第 3 列的第一个字符.

would remove the first character of the 3rd column.

使用 -E 来使用 (){} 不带反斜杠:

Use -E to use () and {} without backslashes:

sed -E 's/(([^ ]* ){2})./\1/' file

这篇关于在 Bash/sed 中分隔字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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