Unix 如何删除列?
我已生成以下文本文件:Unix 如何删除列?,unix,shell,text,Unix,Shell,Text,我已生成以下文本文件: fe120b99164f151b28bf86afa6389b22 -rw-r--r-- 1 joey joey 186 2010-03-14 19:26 Descript.txt 41705ea936cfc653f273b5454c1cdde6 -rw-r--r-- 1 joey joey 30 2010-03-14 20:29 listof.txt 0e25cca3222d32fff43563465af03340 -rw-r--r-- 1 joey joey 28
fe120b99164f151b28bf86afa6389b22 -rw-r--r-- 1 joey joey 186 2010-03-14 19:26 Descript.txt
41705ea936cfc653f273b5454c1cdde6 -rw-r--r-- 1 joey joey 30 2010-03-14 20:29 listof.txt
0e25cca3222d32fff43563465af03340 -rw-r--r-- 1 joey joey 28 2010-03-14 23:35 sedexample.txt
d41d8cd98f00b204e9800998ecf8427e -rw-r--r-- 1 joey joey 0 2010-02-16 15:11 test1.txt
d41d8cd98f00b204e9800998ecf8427e -rw-r--r-- 1 joey joey 0 2010-02-16 15:11 test2.txt
d41d8cd98f00b204e9800998ecf8427e -rw-r--r-- 1 joey joey 0 2010-02-16 15:11 test3.txt
d41d8cd98f00b204e9800998ecf8427e -rw-r--r-- 1 joey joey 0 2010-02-16 15:11 test4.txt
d41d8cd98f00b204e9800998ecf8427e -rw-r--r-- 1 joey joey 0 2010-02-16 15:11 test5.txt
d41d8cd98f00b204e9800998ecf8427e -rw-r--r-- 1 joey joey 0 2010-02-16 15:11 test6.txt
f5c7f1856249d0526be10df5bd5b895a -rw-r--r-- 1 joey joey 26 2010-03-13 14:13 testingfile.txt
d41d8cd98f00b204e9800998ecf8427e -rw-r--r-- 1 joey joey 0 2010-03-15 00:28 uniquelist.txt
基本上我想去掉access、amount列、user列和group列。换句话说,我想去掉第3、4、5列。我曾尝试使用cut来保留我想要的列,并将“”作为分隔符,但是由于文件大小的原因,使用“space”作为分隔符会出现问题。任何建议都将不胜感激!哦,只是补充一下,我想把输出保存为另一个文本文件。非常感谢 你必须用C语言来做吗?这在perl或awk等脚本语言中更容易实现
awk '{print $1 $6 $7 $8 $9}' file.txt
尝试在excel中以空格作为分隔符打开它们,然后将其删除。看看它是否有效。作为壳管很容易完成
cut --delimiter=' ' --fields=3-5 --complement
如果确实需要在C中执行此操作,可以通过popen
在子流程中运行该命令
如果混合使用空格和制表符分隔符,则需要将一个分隔符转换为另一个分隔符,因为
cut
不喜欢多个分隔符。用tr
或col-x通过awk'{print$1$6$7$8$9;}'
?我闻到了家庭作业的味道,但我会让你放心
剪切将起作用,但首先需要将一个或多个空间合并为一个:
sed "s/ \+/ /g" input_file | cut -d' ' -f-2,6-
要将任何命令的输出保存到一个文件,它是command>file
您的cut
命令是什么样子的
$ cut -d" " -f1,6- file
fe120b99164f151b28bf86afa6389b22 186 2010-03-14 19:26 Descript.txt
41705ea936cfc653f273b5454c1cdde6 30 2010-03-14 20:29 listof.txt
0e25cca3222d32fff43563465af03340 28 2010-03-14 23:35 sedexample.txt
d41d8cd98f00b204e9800998ecf8427e 0 2010-02-16 15:11 test1.txt
d41d8cd98f00b204e9800998ecf8427e 0 2010-02-16 15:11 test2.txt
d41d8cd98f00b204e9800998ecf8427e 0 2010-02-16 15:11 test3.txt
d41d8cd98f00b204e9800998ecf8427e 0 2010-02-16 15:11 test4.txt
d41d8cd98f00b204e9800998ecf8427e 0 2010-02-16 15:11 test5.txt
d41d8cd98f00b204e9800998ecf8427e 0 2010-02-16 15:11 test6.txt
f5c7f1856249d0526be10df5bd5b895a 26 2010-03-13 14:13 testingfile.txt
d41d8cd98f00b204e9800998ecf8427e 0 2010-03-15 00:28 uniquelist.txt
根据需要重定向到新文件
还是awk
$ awk '{$2=$3=$4=$5="";gsub(/ +/," ")}1' file
fe120b99164f151b28bf86afa6389b22 186 2010-03-14 19:26 Descript.txt
41705ea936cfc653f273b5454c1cdde6 30 2010-03-14 20:29 listof.txt
0e25cca3222d32fff43563465af03340 28 2010-03-14 23:35 sedexample.txt
d41d8cd98f00b204e9800998ecf8427e 0 2010-02-16 15:11 test1.txt
d41d8cd98f00b204e9800998ecf8427e 0 2010-02-16 15:11 test2.txt
d41d8cd98f00b204e9800998ecf8427e 0 2010-02-16 15:11 test3.txt
d41d8cd98f00b204e9800998ecf8427e 0 2010-02-16 15:11 test4.txt
d41d8cd98f00b204e9800998ecf8427e 0 2010-02-16 15:11 test5.txt
d41d8cd98f00b204e9800998ecf8427e 0 2010-02-16 15:11 test6.txt
f5c7f1856249d0526be10df5bd5b895a 26 2010-03-13 14:13 testingfile.txt
d41d8cd98f00b204e9800998ecf8427e 0 2010-03-15 00:28 uniquelist.txt
我尝试使用awk“{print$1$6$7$8$9}”file.txt,但它提供了以下输出:
FE120B99164F151B28BF86AF6389B221862010-03-1419:26Descriptions.txt
41705EA936CFC653F273B5454C1CDE6302010-03-1420:29号码列表.txt
0E25CCA3222D32FF43563465AF03340282010-03-1423:35sedexample.txt
d41d8cd98f00b204e9800998ecf8427e02010-02-1615:11test1.txt
d41d8cd98f00b204e9800998ecf8427e02010-02-1615:11test2.txt
d41d8cd98f00b204e9800998ecf8427e02010-02-1615:11test3.txt
d41d8cd98f00b204e9800998ecf8427e02010-02-1615:11test4.txt
d41d8cd98f00b204e9800998ecf8427e02010-02-1615:11test5.txt
d41d8cd98f00b204e9800998ecf8427e02010-02-1615:11test6.txt
f5c7f1856249d0526be10df5bd5b895a262010-03-1314:13测试文件.txt
d41d8cd98f00b204e9800998ecf8427e02010-03-1500:28uniquelist.txt
所有的列都连接成一个对我来说并不理想的列,因为我将在某个时候使用uniq one。谢谢你的帮助
$ perl -lane'$, = " "; print @F[0,5..$#F]' input.txt
注意:除了用单个空格替换输入中的两个或多个空格外,例如,它还可以打断输入中的文件名。我最初的cut命令是这样的:more indexfile.txt | cut-d“”-f1,6,7,8,9>良好指数。txt@Joey杰:如果不清楚为什么最初的剪切有时会删除时间和/或文件名:大小(?)列中的额外填充空间在剪切内部创建了额外的空字段。这在某些情况下导致字段枚举太短。如果您使用-f1,6,7,8,9,10,11
,它会起作用,但显然-f1,6-
更简洁灵活。如果要剪切超出第一个空格填充列的字段,则需要不同的解决方案。可能cut-c
如果所有列(包括要剪切的列)都有固定的宽度。这个答案压缩了所有空格,它只是一个大而长的“单词”