开发者

How to delete columns?

开发者 https://www.devze.com 2022-12-23 11:20 出处:网络
I have generated the following text file below: fe120b99164f151b28bf86afa6389b22 -rw-r--r-- 1 joey joey 186 2010-03-14 19:26 Descript.txt

I have generated the following text file below:

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

Basically I would like to get rid of the access, amount column, user and group columns. In other words, I want rid of columns 3,4,5. I have tried using cut to keep the columns that I do want and having " " as my delimiter however because of the file size figures, it messes up using "开发者_StackOverflow社区space" as a delimiter. Any advice would be much appreciated! Oh just to add, I would like to save the output as another text file. Many thanks!


how does your cut command look like?

$ 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

redirect to new file as needed.

Or 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


Easily done as a shell pipe.

cut --delimiter=' ' --fields=3-5 --complement

If you really need to do this in C, you could run that command in a subprocess via popen.

If you had mixed space and tab delimiters, you would need to convert one to the other as cut does not like multiple delimiters. Do this with tr or col -x.


Pipe it through awk '{print $1 $6 $7 $8 $9;}'?


Do you have to do this in C? This would be easier to do in a scripting language like perl, or even awk.

awk '{print $1 $6 $7 $8 $9}' file.txt


I smell homework, but I'll give you the benefit of the doubt.

Cut will work, but you need to merge one or more spaces into just one first:

sed "s/ \+/ /g" input_file | cut -d' ' -f-2,6-

To save the output of any command to a file, it's command > file.


I tried using awk '{print $1 $6 $7 $8 $9}' file.txt however it gives me the following output:

fe120b99164f151b28bf86afa6389b221862010-03-1419:26Descriptions.txt 41705ea936cfc653f273b5454c1cdde6302010-03-1420:29listofnumbers.txt 0e25cca3222d32fff43563465af03340282010-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:13testingfile.txt d41d8cd98f00b204e9800998ecf8427e02010-03-1500:28uniquelist.txt

All the columns have become joined into one which is not ideal for me as I will be using uniq one this at some point. Thanks for your help!


$ perl -lane'$, = " "; print @F[0,5..$#F]' input.txt

NOTE: As well as the cut version it replaces two or more spaces in the input by a single space e.g., it can broke filenames in the input.


Try to open them in excel with space as delimiter, delete it there. See if it works.

0

精彩评论

暂无评论...
验证码 换一张
取 消