Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-10-2009, 12:30 PM
|
#1
|
|
Member
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 213
Rep:
|
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?
|
|
|
|
05-10-2009, 01:41 PM
|
#2
|
|
Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,614
|
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
Last edited by H_TeXMeX_H; 05-10-2009 at 01:45 PM.
|
|
|
|
11-18-2012, 09:43 PM
|
#3
|
|
LQ Newbie
Registered: Nov 2012
Posts: 1
Rep: 
|
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
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?
|
|
|
|
|
11-19-2012, 04:43 AM
|
#4
|
|
Member
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,OpenBSD
Posts: 499
Rep: 
|
Quote:
Originally Posted by H_TeXMeX_H
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.
|
|
|
|
11-19-2012, 07:50 AM
|
#5
|
|
Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,614
|
Quote:
Originally Posted by linosaurusroot
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:55 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|