执行cat test.sql |sort|uniq >> test1.sql后内容混乱

sh/bash/dash/ksh/zsh等Shell脚本
回复
liulian0588
帖子: 4
注册时间: 2012-01-09 11:27

执行cat test.sql |sort|uniq >> test1.sql后内容混乱

#1

帖子 liulian0588 » 2012-01-09 11:32

test是我原先的文件,执行该shell的目的是先排序再取唯一的行,可是做完后发现空行没有了,分号也没有了。
而单独执行cat test.sql |sort >> test1.sql 和cat test.sql |uniq >> test1.sql 却都没有出现这个问题。
请问是什么原因?通过什么方法可以达到我要的目的,谢谢。
桌面.rar
附件
(18.89 KiB) 已下载 28 次
附件
桌面.rar
附件
(18.89 KiB) 已下载 26 次
alober
帖子: 144
注册时间: 2010-07-13 17:04

Re: 执行cat test.sql |sort|uniq >> test1.sql后内容混乱

#2

帖子 alober » 2012-01-09 12:13

应该是sort是按行排序的吧,把分号都当成单独一行排了。试试替换掉那个换行看看。
sed 'N;s/\n//' test.sql | sort | uniq > 1.txt
头像
link_01
帖子: 1024
注册时间: 2008-11-05 13:24

Re: 执行cat test.sql |sort|uniq >> test1.sql后内容混乱

#3

帖子 link_01 » 2012-01-09 13:48

你的文件格式有问题,用noteplus看到是“;”单独一行,能不出错吗?
笔记
-------------------------------------
http://blog.163.com/wqt_1101
头像
josephyoung
帖子: 158
注册时间: 2011-11-05 18:53
来自: 南极圈

Re: 执行cat test.sql |sort|uniq >> test1.sql后内容混乱

#4

帖子 josephyoung » 2012-01-09 21:13

你那个文件不是在ubuntu下建立,也不是在任何linux下建立的,我看到的文件名是乱码。linux文本的格式与win/dos下不一样的。还有,居然用rar压缩。。。linux下的人是不会用rar压缩任何东西的
liulian0588
帖子: 4
注册时间: 2012-01-09 11:27

Re: 执行cat test.sql |sort|uniq >> test1.sql后内容混乱

#5

帖子 liulian0588 » 2012-01-10 11:16

alober 写了:应该是sort是按行排序的吧,把分号都当成单独一行排了。试试替换掉那个换行看看。
sed 'N;s/\n//' test.sql | sort | uniq > 1.txt
确实使这个问题,但是现在执行您的这个命令,有少数的语句分号直接跟着语句了,大部分还是单独的一行,我又试过了\r,\r\n,\n\r,但是好像都不管用。
请问怎么办呢?
我这个文件在windows和linux下传输过。
另外,有没有工具可以直接看到行结尾是什么字符啊?
头像
link_01
帖子: 1024
注册时间: 2008-11-05 13:24

Re: 执行cat test.sql |sort|uniq >> test1.sql后内容混乱

#6

帖子 link_01 » 2012-01-10 11:30

head -10 test.sql |cat -v -

代码: 全选

sed -rn '/entry/N; s/\n//p' test.sql |sort  -u -o 1.sql
笔记
-------------------------------------
http://blog.163.com/wqt_1101
fnan
帖子: 919
注册时间: 2009-07-01 22:04

Re: 执行cat test.sql |sort|uniq >> test1.sql后内容混乱

#7

帖子 fnan » 2012-01-10 22:33

文件有^M字符,win下用的?
bash不如perl精妙,学不到lisp的皮毛,远不够c++强悍,不过可以用。
liulian0588
帖子: 4
注册时间: 2012-01-09 11:27

Re: 执行cat test.sql |sort|uniq >> test1.sql后内容混乱

#8

帖子 liulian0588 » 2012-01-11 11:43

link_01 写了:head -10 test.sql |cat -v -

代码: 全选

sed -rn '/entry/N; s/\n//p' test.sql |sort  -u -o 1.sql

谢谢您的回复,现在的情况是执行sed -rn '/entry/N; s/\n//p' test.sql |sort -u -o 1.sql出现了记录丢失。
如果去掉了-rn中的n或者去掉/entry/都不会出现数据丢失的情况,但是分号就有不对了。

请问应该如何调整,另外n和/entry/是什么意思?n查到的解释是读入下一笔资料/entry/没查到
liulian0588
帖子: 4
注册时间: 2012-01-09 11:27

Re: 执行cat test.sql |sort|uniq >> test1.sql后内容混乱

#9

帖子 liulian0588 » 2012-01-11 11:44

fnan 写了:文件有^M字符,win下用的?
多谢您的回复。

是的,意识到这个问题后,我已经在我这里用dos2unix转码了,现在应该可以不考虑这个问题。
头像
link_01
帖子: 1024
注册时间: 2008-11-05 13:24

Re: 执行cat test.sql |sort|uniq >> test1.sql后内容混乱

#10

帖子 link_01 » 2012-01-11 16:10

数据本身有重复的,自己去掉-u看看
要是你uniq只是想去掉空行的话,

代码: 全选

sed -nr '/entry/s/(.+)/\1;/p' |sort 
sed -nr '/entry/s/(.+)/\1;/p' test.sql|sort|uniq -d -c|sort -r
可以看到很多重复行,所以直接用-u的话,被合并了
笔记
-------------------------------------
http://blog.163.com/wqt_1101
fnan
帖子: 919
注册时间: 2009-07-01 22:04

Re: 执行cat test.sql |sort|uniq >> test1.sql后内容混乱

#11

帖子 fnan » 2012-01-11 18:28

lz目的不明确,先给一段完成的范例看看。
bash不如perl精妙,学不到lisp的皮毛,远不够c++强悍,不过可以用。
回复