LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   How do I cut fields with repeated delimiters? (https://www.linuxquestions.org/questions/linux-software-2/how-do-i-cut-fields-with-repeated-delimiters-725014/)

Mountain 05-10-2009 12:30 PM

How do I cut fields with repeated delimiters?
 
Given a file listing like this:

Code:

$ ls -laR
.:
total 24
drwxr-xr-x  3 auser admin  4096 2009-05-10 13:19 .
drwxr-xr-x 20 auser admin 12288 2009-05-10 13:19 ..
-rw-r--r--  1 auser admin  998 2008-04-07 13:37 file1.txt
drwxr-xr-x  2 auser admin  4096 2009-05-10 13:21 directory1

./directory1:
total 24
drwxr-xr-x 2 auser admin 4096 2009-05-10 13:21 .
drwxr-xr-x 3 auser admin 4096 2009-05-10 13:19 ..
-rwxr-xr-- 1 auser admin 2176 2008-04-06 13:30 2file.txt
-rw-r--r-- 1 auser admin  249 2008-04-06 23:23 experiment.txt
-rw-r--r-- 1 auser admin  334 2008-04-07 11:29 somefile.txt
-rw-r--r-- 1 auser admin 1604 2008-04-02 13:37 text3.out


I want a list that looks like this:
Code:

2008-04-02 13:37 text3.out
2008-04-06 13:30 2file.txt
2008-04-06 23:23 experiment.txt
2008-04-07 11:29 somefile.txt
2008-04-07 13:37 file1.txt

What I'm trying to do is list all files in all subdirectories recursively and sort by date. (I might also pipe the output through tail to list e.g., only the 100 newest files.)

Here is what I tried:
Code:

$ ls -ogARtr | grep '^-' | cut -d ' ' -f 4-
998 2008-04-07 13:37 file1.txt
2008-04-02 13:37 text3.out
2008-04-06 13:30 2file.txt
249 2008-04-06 23:23 experiment.txt
334 2008-04-07 11:29 somefile.txt

Apparently the extra space before the smaller files makes cut not work as expected. That's my main problem. The other problem is I will apparently have to send it through sort to get an overall (not the ls per-subdirectory) sorting.

How can I achieve the desired result?

H_TeXMeX_H 05-10-2009 01:41 PM

I dunno, but here's what I'd do:

Code:

bash-3.1$ ls -l | awk '{ printf("%s\t%s\t%s\n",$6,$7,$8)}' | sort -k1
               
2008-11-24        22:29        var
2009-04-12        13:54        note
2009-04-13        17:08        opt
2009-05-03        16:40        chrom.odt
2009-05-03        18:35        bin
2009-05-03        18:35        lib
2009-05-05        22:14        subjects
2009-05-10        16:14        tmp
2009-05-10        18:16        bench

The '-k1' is optional as the default sorts by first column anyway.

Of course this would also work:

Code:

bash-3.1$ ls -l | sort -k6
total 120
drwxr-xr-x 15 kafox users  4096 2008-11-24 22:29 var
-rw-r--r--  1 kafox users    82 2009-04-12 13:54 note
drwxr-xr-x 12 kafox users  4096 2009-04-13 17:08 opt
-rw-r--r--  1 kafox users  9894 2009-05-03 16:40 chrom.odt
drwxr-xr-x  2 kafox users  4096 2009-05-03 18:35 bin
drwxr-xr-x 17 kafox users  4096 2009-05-03 18:35 lib
-rw-r--r--  1 kafox users  466 2009-05-05 22:14 subjects
drwxr-xr-x 48 kafox users 40960 2009-05-10 16:14 tmp
-rw-r--r--  1 kafox users  474 2009-05-10 18:16 bench


nad2000 11-18-2012 09:43 PM

you should remove repeats:

Code:

$ ls -ogARtr | grep '^-' | sed "s/  */ /g" | cut -d ' ' -f 4-
that will do the trick.

Quote:

Originally Posted by Mountain (Post 3536249)
Given a file listing like this:

Code:

$ ls -laR
.:
total 24
drwxr-xr-x  3 auser admin  4096 2009-05-10 13:19 .
drwxr-xr-x 20 auser admin 12288 2009-05-10 13:19 ..
-rw-r--r--  1 auser admin  998 2008-04-07 13:37 file1.txt
drwxr-xr-x  2 auser admin  4096 2009-05-10 13:21 directory1

./directory1:
total 24
drwxr-xr-x 2 auser admin 4096 2009-05-10 13:21 .
drwxr-xr-x 3 auser admin 4096 2009-05-10 13:19 ..
-rwxr-xr-- 1 auser admin 2176 2008-04-06 13:30 2file.txt
-rw-r--r-- 1 auser admin  249 2008-04-06 23:23 experiment.txt
-rw-r--r-- 1 auser admin  334 2008-04-07 11:29 somefile.txt
-rw-r--r-- 1 auser admin 1604 2008-04-02 13:37 text3.out


I want a list that looks like this:
Code:

2008-04-02 13:37 text3.out
2008-04-06 13:30 2file.txt
2008-04-06 23:23 experiment.txt
2008-04-07 11:29 somefile.txt
2008-04-07 13:37 file1.txt

What I'm trying to do is list all files in all subdirectories recursively and sort by date. (I might also pipe the output through tail to list e.g., only the 100 newest files.)

Here is what I tried:
Code:

$ ls -ogARtr | grep '^-' | cut -d ' ' -f 4-
998 2008-04-07 13:37 file1.txt
2008-04-02 13:37 text3.out
2008-04-06 13:30 2file.txt
249 2008-04-06 23:23 experiment.txt
334 2008-04-07 11:29 somefile.txt

Apparently the extra space before the smaller files makes cut not work as expected. That's my main problem. The other problem is I will apparently have to send it through sort to get an overall (not the ls per-subdirectory) sorting.

How can I achieve the desired result?


linosaurusroot 11-19-2012 04:43 AM

Quote:

Originally Posted by H_TeXMeX_H (Post 3536297)
Code:

bash-3.1$ ls -l | awk '{ printf("%s\t%s\t%s\n",$6,$7,$8)}' | sort -k1

I would do awk fields $(NF-2),$(NF-1),$NF rather than fixed numbers 6,7,8.

H_TeXMeX_H 11-19-2012 07:50 AM

Quote:

Originally Posted by linosaurusroot (Post 4832458)
I would do awk fields $(NF-2),$(NF-1),$NF rather than fixed numbers 6,7,8.

Why ?

P.S. This thread is old, I'm pretty sure the OP will not answer.


All times are GMT -5. The time now is 07:57 PM.