Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove a string containing a substring that will require a wildcard

Tags:

sed

awk

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"

like image 791
Mathew Avatar asked May 30 '26 03:05

Mathew


2 Answers

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.

like image 148
anubhava Avatar answered Jun 02 '26 02:06

anubhava


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
like image 26
RavinderSingh13 Avatar answered Jun 02 '26 02:06

RavinderSingh13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!