How to remove leading and trailing whitespaces?

前端 未结 4 1069
遇见更好的自我
遇见更好的自我 2021-02-07 13:43

I\'m using awk \'{gsub(/^[ \\t]+|[ \\t]+$/,\"\"); print;}\' in.txt > out.txt to remove both leading and trailing whitespaces.

The problem is the output f

4条回答
  •  别那么骄傲
    2021-02-07 14:00

    Perl could be used:

    perl -lpe 's/^\s*(.*\S)\s*$/$1/' in.txt > out.txt
    

    s/foo/bar/ substitute using regular expressions
    ^ beginning of string
    \s* zero or more spaces
    (.*\S) any characters ending with a non-whitespace. Capture it into $1
    \s* zero or more spaces
    $ end of string

提交回复
热议问题