Skip to content

How to Trim Both Ends of Lines

  • by
In this post, I plan to remove the leading and trailing white spaces or tab characters from the file. Let's see the original file.
[root@localhost ~]$ cat temp.txt
  Database Administration.  Web Development.
IT Architecture Design.   Project Management.

In the above file, the leading character of the second line is a tab. We can use sed to remove them.
[root@localhost ~]$ cat temp.txt | sed 's/^[ t]*|[ t]*$//g'
Database Administration.  Web Development.
IT Architecture Design.   Project Management.

You can see the white spaces or tabs at the leading and trailing of lines are removed, but other inline white spaces or tabs remain unchanged.

Leave a Reply

Your email address will not be published.