views:

220

answers:

9

Hi all,

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 "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!

+2  A: 

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
Seth
A: 

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

hallie
+2  A: 

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.

Lachlan Roche
+3  A: 

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

Mike DeSimone
A: 

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.

Max E.
A: 

Thx guys, I'll give it a try! I was being stupid when I tagged it as being C. Its really just Unix and scripting. My bad!

Joey jie
This is not a forum; don't respond by "posting an answer" unless the answer turned out to be none of those given. Please respond by using the "add comment" links under the answer(s) you are responding to.
Mike DeSimone
+2  A: 

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
ghostdog74
My original cut command was like this:more indexfile.txt | cut -d " " -f 1,6,7,8,9 >goodindex.txt
Joey jie
Many thanks for your help! Your solution was perfect!
Joey jie
@Joey jie: In case it is not clear why your initial *cut* sometimes dropped the time and/or filename: The extra padding spaces in the size(?) column created extra, empty fields internally in *cut*. This caused your field enumeration to be too short in some cases. If you used `-f1,6,7,8,9,10,11` it would have worked, but obviously `-f1,6-` is more succinct and flexible. You would need a different solution if you wanted to cut out fields that were beyond the first space-padded column. Maybe `cut -c` if all the columns (before and including the columns you want to cut) have fixed widths.
Chris Johnsen
A: 

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!

Joey jie
A: 
$ 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.

J.F. Sebastian