Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matching tab with sed

Tags:

regex

bash

sed

I am new to sed, so want to know how can I match tab. I have some text like this -

    line one {
            line two {
                    filler {
                            text value
                    }
                    line four
            }
    }

I am using the expression, which matches the above text if there are no tabs.

sed -i -e "/ line two {/,/}/s/text .*\$/text new/" $file

However, there are 2 tabs before "line two" and 3 tabs before "filler" and 4 tabs before "text value".

Any help?

like image 326
user1060517 Avatar asked Oct 27 '25 23:10

user1060517


1 Answers

In GNU sed, tabs are represented as \t. So, the substitution will be successfully made with the following:

sed -i -e "/\tline two {/,/}/s/text .*\$/text new/" "$file"

The only required change was to replace / line two {/ with /\tline two {/. The command /s/text .*\$/text new/ does not require any change because it does not attempt to match the white space that precedes text. If there might be a tab that follows text, then use:

sed -i -e "/\tline two {/,/}/s/text[[:space:]].*\$/text new/" "$file"

If you are unsure of what white space character will appear, the expression [[:space:]] can be used to match any kind of white space.

like image 178
John1024 Avatar answered Oct 31 '25 02:10

John1024



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!