Bash CRLF - new lines at the end of document

时间:2016-03-04 18:11:20

标签: bash shell sed newline

I have looked at many questions at StackOverflow and uncle Google, but somehow I still can't crack it.

I have a CSV file that is automatically exported by SSRS. Unfortunately the export plugins are old and they put two line breaks and carriage returns at the end of the file:

00000c0: 6b7c 3230 2d46 6562 2d31 360d 0a0d 0a k|20-Feb-16....

I tried many sed replacements however it seems to only remove one line.

For example the simplistic

sed -i '/^\s*$/d'

Also tried to replace \s with [[:space:]] (also works but on one line only)

After which the last line of the hex dump looks like below:

00000c0: 6b7c 3230 2d46 6562 2d31 360d 0a k|20-Feb-16..

I've tried things like:

sed -i 's/\x0D\X0A//g' <file> however this wouldn't replace both 0d0a at the end

Any help would be appreciated

2 个答案:

答案 0 :(得分:1)

The following command should work for you:

sed 's/\x0d//;/^$/d'

I'm removing all carriage return characters and delete empty lines.

Try it, like this:

echo -e "foo\x0a\x0d\x0a" | sed 's/\x0d//;/^$/d' | xxd
00000000: 666f 6f0a                                foo.

答案 1 :(得分:1)

“忘记最后两行。”

# gnu!
head -n -2 foo.csv > foo.csv.new

“哦,ed(或ex / vi / vim),杀掉最后两行。”

ed foo.csv << EOF
$
-1,$d
w foo.csv.new
q
EOF

# ex/vi/vim: change this to vi -c "the whole trunk".

“我喜欢sed。我喜欢sed。” (我没有)

sed -i -e '$d' foo.csv; sed -i -e '$d' foo.csv

“我应该杀了吗?”

[[ $(tail -n 2 foo.csv) == $'\r\n\r\n' ]]

“Vim,你能亲自测试吗?”

# I don't write vimscript.

“PERL?”

# I don't write Perl.