awksed

Remove a string containing a substring that will require a wildcard


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"


Solution

  • 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.