茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1670299 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
F09_515.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

小企鵝開談 : [轉貼]Delete empty lines using SED

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Delete empty lines using SED
Delete empty lines using SED

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'
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:
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
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]How to remove blank lines from a file in shell?
How to remove blank lines from a file in shell?

I want to remove all empty lines from a file. Even if the line contains spaces or tabs it should also be removed.



Just grep for non-blanks:
grep '[^[:blank:]]' < file.in > file.out

[:blank:], inside character ranges ([...]), is called a POSIX character class. There are a few like [:alpha:], [:digit:]... [:blank:] matches horizontal white space (in the POSIX locale, that's space and tab, but in other locales there could be more, like all the Unicode horizontal spacing characters in UTF8 locales) while [[:space:]] matches horizontal and vertical white space characters (same as [:blank:] plus things like vertical tab, form feed...).
grep '[:blank:]'

Would return the lines that contain any of the characters, :, b, l, a, n or k. Character classes are only recognised within [...], and ^ within [...] negates the set. So [^[:blank:]] means any character but the blank ones.



Here is an awk solution:
$ awk 'NF' file

With awk, NF only set on non-blank lines. When this condition match, awk default action that is print will print the whole line.



How about :
sed -e 's/^[[:blank:]]*$//' source_file > newfile

or
sed -e '/^[[:blank:]]*$/d' source_file > newfile

i.e.
For each line, substitute:
if it starts ("^")
with spaces or tabs ("[[:blank:]]") zero or more times ("*")
and then is the end of the line ("$")
More info on ::blank:: and other special characters at http://www.zytrax.com/tech/web/regex.htm#special



You can use sed command for removing blank lines:
sed '/^$/d' in > out

This command deletes all empty lines from the file "in"



Try ex-way:
ex -s +'v/\S/d' -cwq test.txt

For multiple files (edit in-place):
ex -s +'bufdo!v/\S/d' -cxa *.txt

Note: The :bufdo command is not POSIX.
Without modifying the file (just print on the standard output):
cat test.txt | ex -s +'v/\S/d' +%p +q! /dev/stdin




Looks like I've found one not that fast, but funny at last:
| xargs -L1


原文出處:How to remove blank lines from a file in shell? - Unix & Linux Stack Exchange
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|