對這文章發表回應
發表限制: 非會員 可以發表
發表者: 冷日 發表時間: 2016/8/25 12:10:32
Delete empty lines using SED
I am trying to delete empty lines using sed
but i have no luck with it. for example I have this lines:
and i want it to be like:
what should be the code for this?
you may have space/tabs in your "empty" line try this, see if it helps:
Where \s matches any whitespace character.
should be fine, are you expecting to modify the file in place? If so you should use the -i flag.
Maybe those lines are not empty, so if that's the case, look at this question Remove empty lines from txtfiles, remove spaces from start and end of line I believe that's what you're trying to achieve.
I believe this is the easiest and fastest one:
If you need to ignore all white-space lines as well then try this:
Example:
outputs
I am missing the awk solution:
Which would return:
How does this work? Since NF stands for "number of fields", those lines being empty have 0 fiedls, so that awk evaluates 0 to False and no line is printed; however, if there is at least one field, the evaluation is True and makes awk perform its default action: print the current line.
With help from the accepted answer here and the accepted answer above, I have used:
This covers all the bases and works perfectly for my needs. Kudos to the original posters @Kent and @kev
You can say:
You can do something like that using "grep", too:
原文出處:linux - Delete empty lines using SED - Stack Overflow
I am trying to delete empty lines using sed
sed '/^$/d'
but i have no luck with it. for example I have this lines:
xxxxxx
yyyyyy
zzzzzz
and i want it to be like:
xxxxxx
yyyyyy
zzzzzz
what should be the code for this?
you may have space/tabs in your "empty" line try this, see if it helps:
sed '/^\s*$/d'
Where \s matches any whitespace character.
sed '/^$/d'
Maybe those lines are not empty, so if that's the case, look at this question Remove empty lines from txtfiles, remove spaces from start and end of line I believe that's what you're trying to achieve.
I believe this is the easiest and fastest one:
cat file.txt | grep .
If you need to ignore all white-space lines as well then try this:
cat file.txt | grep '\S'
Example:
s="\
\
a\
b\
\
Below is TAB:\
\
Below is space:\
\
c\
\
"; echo "$s" | grep . | wc -l; echo "$s" | grep '\S' | wc -l
outputs
7
5
I am missing the awk solution:
awk 'NF' file
Which would return:
xxxxxx
yyyyyy
zzzzzz
How does this work? Since NF stands for "number of fields", those lines being empty have 0 fiedls, so that awk evaluates 0 to False and no line is printed; however, if there is at least one field, the evaluation is True and makes awk perform its default action: print the current line.
With help from the accepted answer here and the accepted answer above, I have used:
$ sed 's/^ *//; s/ *$//; /^$/d; /^\s*$/d' file.txt > output.txt
`s/^ *//` => left trim
`s/ *$//` => right trim
`/^$/d` => remove empty line
`/^\s*$/d` => delete lines which may contain white space
This covers all the bases and works perfectly for my needs. Kudos to the original posters @Kent and @kev
You can say:
sed -n '/ / p' filename #there is a space between '//'
You can do something like that using "grep", too:
egrep -v "^$" file.txt
原文出處:linux - Delete empty lines using SED - Stack Overflow