最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

awk - Remove a string containing a substring that will require a wildcard - Stack Overflow

programmeradmin3浏览0评论

First time I have had to post here to solve a problem. I am guessing I am missing something easy. Spent a bout four hours yesterday beating my head up against something I though was going to be simple. :)

I want to remove a string using sed or tr, the strings have a different number of characters, so I believe I will need to use wildcards.

myfile:

1. The cat is black df^[[K
2. The dog is large fyue^[K
3. The sky is blue
4. Grass is green ^[K

Desired output:

1. The cat is Black
2. The dog is large
3. The sky is blue
4. Grass is green

This is what I have so far:

cat myfile | sed 's/ .*\[K//g'

Output:

1.
2.
3. The sky is blue
4.

I understand what this is doing. I am basically telling it that I am looking for strings with a "space" "wildcard" "[K"

The problems lies in that I believe it is considering all the text between initial space in the line and the "[K" as my string to be removed. Is it possible to get it to only consider the space right before the occurrence of "[K"

First time I have had to post here to solve a problem. I am guessing I am missing something easy. Spent a bout four hours yesterday beating my head up against something I though was going to be simple. :)

I want to remove a string using sed or tr, the strings have a different number of characters, so I believe I will need to use wildcards.

myfile:

1. The cat is black df^[[K
2. The dog is large fyue^[K
3. The sky is blue
4. Grass is green ^[K

Desired output:

1. The cat is Black
2. The dog is large
3. The sky is blue
4. Grass is green

This is what I have so far:

cat myfile | sed 's/ .*\[K//g'

Output:

1.
2.
3. The sky is blue
4.

I understand what this is doing. I am basically telling it that I am looking for strings with a "space" "wildcard" "[K"

The problems lies in that I believe it is considering all the text between initial space in the line and the "[K" as my string to be removed. Is it possible to get it to only consider the space right before the occurrence of "[K"

Share Improve this question edited Mar 21 at 3:44 RavinderSingh13 134k14 gold badges60 silver badges98 bronze badges asked Mar 19 at 17:10 MathewMathew 312 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

Is it possible to get it to only consider the space right before the occurrence of "[K"

You may use this sed solution with a negated bracket expression:

sed 's/ [^[:blank:]]*\[K//' file

1. The cat is black
2. The dog is large
3. The sky is blue
4. Grass is green

Instead of using using .* which matches everything we are replacing that with [^[:blank:]]* which basically means match 0 or more any characters that are not space or tab characters.

In case you are ok with awk try following answer. Simple explanation would be, making field separator as [[:space:]]*[^[:blank:]]*\\[K and printing the first field as per your shown samples.

awk -F'[[:space:]]*[^[:blank:]]*\\[K' '{print $1}' Input_file
发布评论

评论列表(0)

  1. 暂无评论