Archive for November 16th, 2007
16
Delete empty lines using sed command under Linux / UNIX
No comments · Posted by nguyen in Linux Docs
Q. I need to delete all empty lines but could not figure out sed command for the same? How do I delete all empty lines with sed?
A. sed is a stream editor and perfect for these kind of work.
You need to use d command under sed which is act as the delete function.
Sed Delete Empty Line Syntax
sed ‘/^$/d’
echo $VAR | sed ‘/^$/d’
So to delete all empty lines from a file called /tmp/data.txt, enter:
$ sed ‘/^$/d’ /tmp/data.txt
To store output to another file use redirection operator:
$ sed ‘/^$/d’ /tmp/data.txt > /tmp/output.txt
Deleting a line that matches a pattern
You can also match a word or a pattern to delete. For example
$ cat data.txt
Output:
This is a test
Linux rulez
Windows sucks
Redhat is good server disro
To delete all lines that contain a ‘Windows’ word, enter:
$ sed ‘/Windows/d’ /tmp/data.txt
Reff: http://www.cyberciti.biz/faq/using-sed-to-delete-empty-lines/
Q. How do I use sed for selective deletion of certain lines? I have text as follows in file:
Line 1
Line 2
WORD1
Line3
Line 4
WORD2
Line5
I would like to delete all lines between WORD1 and WORD2 to produce final output:
Line 1
Line 2
Line5
A. For selective deletion of certain lines sed is the best tool. To print all of file EXCEPT section between WORD1 and WORD2 (2 regular expressions), use
$ sed ‘/WORD1/,/WORD2/d’ input.txt > output.txt
Shell script to remove Javascript code
Here is my small script that reads all *.html files and removes javascript (script download link).
#!/bin/bash
# ALL HTML FILES
FILES=“*.htmlâ€
# for loop read each file
for f in $FILES
do
INF=“$fâ€
OUTF=“$f.out.tmpâ€
# replace javascript
sed ‘/
No tags
No tags
