Reputation: 361
I have a file with blank lines and I need to double space the lines in the file. Meaning I need a blank line between two lines with text in it. Can you show me an easy way to do it with awk and/or sed
Upvotes: 5
Views: 6021
Reputation: 26481
A very quick way using sed
sed '/^$/d;G'
This deletes all empty lines using the command /^$/d
and appends the content of the hold space (which is empty) to the pattern space with a newline in between (G
).
A very quick way is using awk
awk 'BEGIN{RS="";FS="\n";ORS=OFS="\n\n"}{$1=$1}1'
How does this work:
awk knowns the concept records (which is by default lines) and you can define a record by its record separator RS
. If you set the value of RS
to an empty string, it will match any multitude of empty lines as a record separator. The value ORS
is the output record separator. It states which separator should be printed between two consecutive records. This is set to two <newline> characters.
This, however, does not solve the problem when you have a multi-line record. So for this, we define fields (by default words). The field separator FS
is defined as a single <newline> character such that each field represents a line. And the output field-separator is defined as two <newline> characters. We convert FS
into OFS
by reassigning the first field to itself {$1=$1}
Finally, the statement 1
is a shorthand for {print $0}
which prints the current record followed by the output record-separator ORS
.
Upvotes: 1
Reputation: 793
With awk:
# double space a file
$ awk '1;{print ""}' file
Another way
# or like this
$ awk 'BEGIN{ORS="\n\n"};1' file
Upvotes: 0
Reputation: 49
Assuming:
1. You want to do this in just SED
2. You want to do it all in one pass
3. Some of your "blank" lines might contain tabs|spaces
4. The current spacing is non-uniform (0, 1, or more blanks after each line)
5. You want to end up with uniform double-spacing
Try this:
#!/bin/sed -f
/^[ \t]*$/d
/^..*$/a\
The first line says:
If you see a "blank line", delete it.
The second line says: If you see a non-blank line, append a line after it.
Upvotes: 4
Reputation: 49
Assuming:
1. You want to do this in just AWK
2. You want to do it all in one pass
3. Some of your "blank" lines might contain tabs|spaces
4. The current spacing is non-uniform (0, 1, or more blanks after each line)
5. You want to end up with uniform double-spacing
Try this:
awk '$0 !~ /^[ \t]*$/ { print $0, "\n" }' <file>
Breaking this up,
The part before the curly bracket says: Match only non-blank lines
The part within the curly brackets says: Print this line and append one more (a blank one)
Upvotes: 0
Reputation: 246877
Assuming you want to keep the existing blank lines intact:
awk '
prev && $0 {print ""}
{print; prev = $0}
'
The first test will only be true if both the previous line and the current line are non-empty.
Upvotes: 0
Reputation: 38462
jcomeau@intrepid:/tmp$ cat test.txt
test
tset 2
test 3
test 4
jcomeau@intrepid:/tmp$ sed 's/\(.*\)/\1\n/' test.txt
test
tset 2
test 3
test 4
Sai's sed G test.txt
works also, but I have no idea how.
Upvotes: 0