
Vivek Gite is the founder of nixCraft, the oldest running blog about Linux and open source. Do read the following manual page using the man command or help command: These are some quick examples for sed commands when you wish to delete or remove all leading blank spaces/tabs ( whitespace ) from each input line on your Linux, macOS, FreeBSD or Unix machine. $ cat input.txt | sed 's/^*// s/*$//' > output.txt Summing up Here is my demo.txt file and I am removing all leading white spaces:Įxec 3 ' | awk -F '' '" done exec 0 output.txtīetter remove all leading and trailing whitespace from end of each line:
// : Replace (delete) all matched pattern. ^* : Search pattern ( ^ – start of the line * match one or more blank spaces including tab). s/ : Substitute command ~ replacement for pattern (^*) on each addressed line. $ echo " This is a test" | sed -e 's/^*//' To remove all whitespace (including tabs) from left to first word, enter: Following echo statements printed lots of whitespace from left side: The sed is very handy tool for editing and deleting unwanted stuff. $ cat /etc/nf | sed '/^$/d' > /tmp/output.file sed tip: Remove / Delete All Leading Blank Spaces / Tabs ( whitespace ) From Each Line Let us try to remove all empty lines from a text file: $ grep "Linux" file.txt | sed 's/^*//' To remove all blank lines, enter: For example, find all lines contain word “Linux” and then run sed over it: It is possible to use the grep command or egrep command too. $ sed -e '/^PEN/,/^PENCIL/p' my_input.txt > my_output.txt
You can skip the cat and use it as follows: $ cat input.txt sed -e '/^PEN/,/^PENCIL/p' Let us use the combination of the cat command and sed command:
To Print the lines between each pair of words pen and pencil, inclusive, enter: