删除空行

时间:2015-05-12 21:45:20

标签: awk sed grep tr blank-line

我有一个csv文件,其中每隔一行都是空白的。我尝试了一切,没有删除线条。应该更容易的是数字44出现在每个有效行中。我尝试过的事情:

grep -ir 44 file.csv
sed '/^$/d' <file.csv
cat -A file.csv
sed 's/^ *//; s/ *$//; /^$/d' <file.csv
egrep -v "^$" file.csv
awk 'NF' file.csv
grep '\S' file.csv
sed 's/^ *//; s/ *$//; /^$/d; /^\s*$/d' <file.csv
cat file.csv | tr -s \n

决定我想象空白行,但是导入Google表格并且它们仍然存在!开始质疑我的理智!有人可以帮忙吗?

5 个答案:

答案 0 :(得分:1)

使用-i选项将原始文件替换为已编辑的文件。

sed -i '/^[ \t]*$/d' file.csv

或者输出到另一个文件并重命名,这正是-i所做的。

sed '/^[[:blank:]]*$/d' file.csv > file.csv.out && mv file.csv.out file.csv

答案 1 :(得分:1)

instruments -v -w YOUR_DEVICE_ID -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.xrplugin/Contents/Resources/Automation.tracetemplate /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/Applications/Preferences.app -e UIASCRIPT /Users/mytests/Documents/xCode/Shell-Script/YourTestScript-App.js

-n means skip printing
-i inplace (overwrite same file)
- /44/p print lines where '44' exists

without '44' present

sed -n -i '/44/p' file

\s is matching whitespace, ^startofline, $endofline, d delete line

答案 2 :(得分:1)

Given:

select n.*
from dbo.RegionNodes as n
where n.regionId=-1
    and not exists
    (
        select 1
        from dbo.RegionNodes as n2
        where n2.nodeId=n.nodeId
            and n2.regionId<>n.regionId
    )

You can remove blank lines with Perl:

$ cat bl.txt
Line 1 (next line has a tab)

Line 2 (next has several space)

Line 3

awk:

$ perl -lne 'print unless /^\s*$/' bl.txt
Line 1 (next line has a tab)
Line 2 (next has several space)
Line 3

sed + tr:

$ awk 'NF>0' bl.txt
Line 1 (next line has a tab)
Line 2 (next has several space)
Line 3

Just sed:

$ cat bl.txt | tr '\t' ' ' | sed '/^ *$/d'
Line 1 (next line has a tab)
Line 2 (next has several space)
Line 3

答案 3 :(得分:1)

Aside from the fact that your commands do not show that you capture their output in a new file to be used in place of the original, there's nothing wrong with them, EXCEPT that:

None

should be:

cat file.csv | tr -s \n

Otherwise, the shell eats the cat file.csv | tr -s '\n' # more efficient alternative: tr -s '\n' < file.csv and all that \ sees is tr.

Note, however, that the above only eliminates only truly empty lines, whereas some of your other commands also eliminate blank lines (empty or all-whitespace).

Also, the n (for case-insensitive matching) in -i is pointless, and while using grep -ir 44 file.csv (for recursive searches) will not change the fact that only -r is searched, it will prepend the filename followed by file.csv to each matching line.


If you have indeed captured the output in a new file and that file truly still has blank lines, the : (cat -A on BSD-like platforms) you already mention in your question should show you if any unusual characters are present in the file, in the form of cat -et sequences, such as ^<char> for ^M chars.

答案 4 :(得分:0)

如果您喜欢awk,请执行以下操作:

awk '/44/' file

它只会打印包含44

的行