Saturday 15 October 2016

Using sed to match regex



I don't know much about sed, nor regex. I want to replace every line that contains only tabs by the string '0'. There are also lines in my file that contain only '\n'.



Basically I want to use the regular expression ^\h+$ and replace the matches with 0.




I tried:



sed -i 's/^\h+$/0/' file.txt


But it doesn't work


Answer



You can use:



sed -i.bak -E 's/^[[:blank:]]+$/0/' file



POSIX character class [[:blank:]] matches a space or tab which is same as \h in PCRE.



-i.bak is to keep original file in file.bak, in case you want to restore.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...