LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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



Reply
 
Search this Thread
Old 05-10-2009, 01:30 PM   #1
Mountain
Member
 
Registered: Nov 2007
Location: A place with no mountains
Distribution: Kubuntu, sidux, openSUSE
Posts: 214

Rep: Reputation: 33
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?
 
Old 05-10-2009, 02:41 PM   #2
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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 02:45 PM.
 
Old 11-18-2012, 10:43 PM   #3
nad2000
LQ Newbie
 
Registered: Nov 2012
Posts: 1

Rep: Reputation: Disabled
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 View Post
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?
 
Old 11-19-2012, 05:43 AM   #4
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 869
Blog Entries: 2

Rep: Reputation: 216Reputation: 216Reputation: 216
Quote:
Originally Posted by H_TeXMeX_H View Post
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.
 
Old 11-19-2012, 08:50 AM   #5
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Quote:
Originally Posted by linosaurusroot View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple (?) awk, two delimiters int0x80 Programming 3 02-25-2009 09:53 AM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 08:16 PM
Using Delimiters to get the end of the string champak Programming 3 12-16-2007 07:15 AM
Delimiters in control file ancys Programming 1 08-11-2006 12:40 PM
cut fields in a file christina_rules Linux - Newbie 12 07-15-2006 11:00 AM


All times are GMT -5. The time now is 11:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration