Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem in using Sed to remove leading and trailing spaces

Tags:

regex

sed

I am using the following code to remove both leading and tailing spaces from all lines of a file A.txt

sed 's/^[ \t]*//;s/[ \t]*$//' ./A.txt > ./B.txt

The problem occurs on the lines where there is a t in the beginning or at the end. So say for example, the original line that starts with the string "timezone" becomes "imezone"

Can you please tell me what is happening here? and also if there is a known solution to the problem.

Thanks in advance.

like image 511
user57299 Avatar asked Nov 18 '25 21:11

user57299


1 Answers

Some older versions of sed don't understand C-style escape characters such as \t and treat that as two characters, '\' and 't'. You can avoid the problem by using a literal tab character -- if you're typing this directly into the shell, type Ctrl+V Tab.

like image 90
Chris Dodd Avatar answered Nov 20 '25 13:11

Chris Dodd



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!