各种命令可谓是unix/linux系统中优美的部分,它们能够帮助我们搞定各种繁杂的任务,使我们的工作变得更轻松、更舒适、更给力。
命令之乐
文件拼接
假如我有这么一个文件,含有多行空行:
191 GW_8 [wyz] :/home/wyz/tmp>vi tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
这里仅是示例,具体应用场景,自己想吧。
问题:如何压缩空白行?
简而言之,即删除空行,你的方法呢?
好吧,先从最土的开始。。。。。。
方法一:通过vi编辑,通过stdin使用命令d逐行删除
文件内容少还好,成百上千行,效率就不行了。
方法二:借助第三方工具删除空行
比如使用Uedit,ftp方式打开后,选择空行并逐个删除。
192 GW_8 [wyz] :/home/wyz/perl/tmp>more
tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
193 GW_8 [wyz] :/home/wyz/perl/tmp>
压缩空白行
各种命令可谓是unix/linux系统中优美的部分,它们能够帮助我们搞定各种繁杂的任务,使我们的工作变得更轻松、更舒适、更给力。
这里仅开个头,能不能持续、能不能有效果,端看大家的意愿了。如果能持续,长久累积下来,就是一个非常完美的文档与参考手册,价值不菲。
今天开个头(也可能是个结尾)。
实战演练1
文件拼接
假如我有这么一个文件,含有多行空行:
191 GW_8 [wyz] :/home/wyz/tmp>vi tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
这里仅是示例,具体应用场景,自己想吧。
问题:如何压缩空白行?
简而言之,即删除空行,你的方法呢?
都有哪些方法呢?
方法1:不用忽略了more
192 GW_8 [wyz] :/home/wyz/tmp>more
tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
193 GW_8 [wyz] :/home/wyz/tmp>
more命令具有压缩空行的功能,你是否注意到了?
方法2、你使用最多的cat命令
194 GW_8 [wyz] :/home/wyz/tmp>cat -s
tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
195 GW_8 [wyz] :/home/wyz/tmp>
等同于more,压缩了多余的空白行,仅保留一个空白行。
方法3、使用tr移除空行
201 GW_8 [wyz] :/home/wyz/tmp>cat
tmp.txt | tr -s '\n'
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
202 GW_8 [wyz] :/home/wyz/tmp>
方法4、使用sed移除空行
202 GW_8 [wyz] :/home/wyz/tmp>cat
tmp.txt | sed '/^$/d'
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
203 GW_8 [wyz] :/home/wyz/tmp>
方法5、使用xargs分割空行
206 GW_8 [wyz] :/home/wyz/tmp>cat
tmp.txt | xargs -n 1
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
207 GW_8 [wyz] :/home/wyz/tmp>
Xargs中的” ”,即xargs使用空格作为默认定界符。
方法6、perl单行程序
176 GW_8 [wyz] :/home/wyz/tmp>perl -pi
-e 's/^\s+$//g' tmp.txt;cat tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
177 GW_8 [wyz] :/home/wyz/tmp>
或者
131 GW_8 [wyz] :/home/wyz/tmp>perl -pe
's/^\s+$//g' tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
方法7、uniq
204 GW_8 [wyz] :/home/wyz/tmp>uniq -u
tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
或者
205 GW_8 [wyz] :/home/wyz/tmp>uniq tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
206 GW_8 [wyz] :/home/wyz/tmp>
方法8、grep
206 GW_8 [wyz] :/home/wyz/tmp>grep -v
'^$' tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
207 GW_8 [wyz] :/home/wyz/tmp>
228 GW_8 [wyz] :/home/wyz/tmp>cat
tmp.txt | grep '^[0-9a-z]'
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
方式9、sort
219 GW_8 [wyz] :/home/wyz/tmp>sort
tmp.txt | grep -v '^$'
1,2,3,4,5,6
7,8,9,0,1,2
a,s,d,f,g,h
q,w,e,r,t,y
或
221 GW_8 [wyz] :/home/wyz/tmp>sort -u
tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
a,s,d,f,g,h
q,w,e,r,t,y
方式10
228 GW_8 [wyz] :/home/wyz/tmp>cat
tmp.txt | grep '^[0-9a-z]'
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
方式11
229 GW_8 [wyz] :/home/wyz/tmp>grep
'^[0-9a-z]' tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
230 GW_8 [wyz] :/home/wyz/tmp>
方式12
238 GW_8 [wyz] :/home/wyz/tmp>sed
'/^$/'d tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
方式13
264 GW_8 [wyz] :/home/wyz/tmp>sed -n 'G;
s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P' tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
方式14
266 GW_8 [wyz] :/home/wyz/tmp>sed
'/./\!d' tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
267 GW_8 [wyz] :/home/wyz/tmp>
方式15
267 GW_8 [wyz] :/home/wyz/tmp>sed '$\!N;
/^\(.*\)\n\1$/\!P; D' tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
268 GW_8 [wyz] :/home/wyz/tmp>
方式16
269 GW_8 [wyz] :/home/wyz/tmp>sed
'/./,/^$/\!d' tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
方式17
270 GW_8 [wyz] :/home/wyz/tmp> sed
'/^$/N;/\n$/D' tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
271 GW_8 [wyz] :/home/wyz/tmp>sed
'/^$/N;/\n$/N;//D' tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
方式18 uniq
279 GW_8 [wyz] :/home/wyz/tmp>uniq
tmp.txt
1,2,3,4,5,6
7,8,9,0,1,2
q,w,e,r,t,y
a,s,d,f,g,h
280 GW_8 [wyz] :/home/wyz/tmp>
方式19
281 GW_8 [wyz] :/home/wyz/tmp>sort
tmp.txt | grep -v '^$'
1,2,3,4,5,6
7,8,9,0,1,2
a,s,d,f,g,h
q,w,e,r,t,y
282 GW_8 [wyz] :/home/wyz/tmp>
还有其他的方法,是否想到了呢,想到了就接着补充吧。