简体   繁体   English

使用grep处理文本文件

[英]Manipulating a text file with grep

Ok, so I have a text file formatted like this, it has several rows going down. 好的,所以我有一个这样的文本文件格式,它的行数下降了。

EXAMPLE1:EXAMPLE2:EXAMPLE3
EXAMPLE1:EXAMPLE2:EXAMPLE3
EXAMPLE1:EXAMPLE2:EXAMPLE3

I want to use grep to change it to look like this 我想使用grep将其更改为如下所示

EXAMPLE2:EXAMPLE3

Basically I want to take out the first part before the : 基本上,我想取出第一部分:

If someone could please tell me how to do this it would be greatly appreciated. 如果有人可以告诉我该怎么做,将不胜感激。

You can use the cut utility to filter out columns given a delimiter. 您可以使用cut实用程序过滤出给定分隔符的列。

In this example, the delimiter is : and you want every column starting with the second: 在此示例中,定界符为:并且您希望每一列都从第二列开始:

$ cut -d: -f2- < input.txt

You can use -o flag to select the part of the input which matches a regex in grep . 您可以使用-o标志来选择与grep中的正则表达式匹配的输入部分。

Example

$ grep -o "[^:]*:[^:]*$" input
EXAMPLE2:EXAMPLE3
EXAMPLE2:EXAMPLE3
EXAMPLE2:EXAMPLE3

Another way to do this. 另一种方法。 Hope this helps. 希望这可以帮助。

awk -F":" '{printf "%s:%s\n" ,$1,$2}' file.txt

Output 输出量

EXAMPLE1:EXAMPLE2
EXAMPLE1:EXAMPLE2
EXAMPLE1:EXAMPLE2

Here it is: 这里是:

cut -d':' -f2,3 filename

Output: 输出:

EXAMPLE2:EXAMPLE3
EXAMPLE2:EXAMPLE3
EXAMPLE2:EXAMPLE3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM