LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2016, 11:57 PM   #1
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,797

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Problem sorting using '/' as a field separator


Old hand at sort? Here's one for ya...

I have a file full of paths and files obtained from a "find * -type f | sort" command that looks something like (the directories and filenames have been changed to protect the innocent):
Code:
a/a_a1.dat
a/a_b1.dat
a/a_d1.dat
a/a_d2.dat
a/a_f/a_c1.dat
a/a_f/a_d1.dat
a/a_f/a_e1.dat
a/a_g1.dat
b/b_a1.dat
b/b_b1.dat
c/c_a1.dat
.
.
.
Which gives me a sorted listing but the listing for directory "a" is interrupted by the list of files in "a/a_f/". What I'd like to do is reorder this to something like:
Code:
a/a_a1.dat
a/a_b1.dat
a/a_d1.dat
a/a_d2.dat
a/a_g1.dat
a/a_f/a_c1.dat
a/a_f/a_d1.dat
a/a_f/a_e1.dat
b/b_a1.dat
b/b_b1.dat
c/c_a1.dat
.
.
.
I tried
Code:
find * -type f | sort --field-separator=/ -k...   (as well as "='/'" )
and I still get the same ordering as the first listing.

Is there a reasonably simple way to do this?

I'm thinking of going down the road of:
Code:
find * -type d | sort | while read DN
do
    find $DN -maxdepth 1 -type f | sort
done
which I seems to do the trick though I'm hoping that I've just missed something about "sort --field-separator" and can keep this to a one-liner.

Can I?

Or is using the "sort with field separator" solution hopeless because the records don't have the same number of fields?


TIA...

--
Rick
 
Old 08-03-2016, 04:37 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Not sure ona solution off the top of my head, but what you are after is to sort all files before directories.
 
Old 08-03-2016, 05:59 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
No collating sequence I know will allow sort to put "g" before "f" (in an ascending sort). Forlorn hope for sort to do it unaided IMHO.
 
1 members found this post helpful.
Old 08-03-2016, 03:26 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
a/a_a1.dat
a/a_b1.dat
a/a_d1.dat
a/a_d2.dat
a/a_f/a_c1.dat
a/a_f/a_d1.dat
a/a_f/a_e1.dat
a/a_g1.dat
b/b_a1.dat
b/b_b1.dat
c/c_a1.dat
... this code ...
Code:
awk -F_ '{sub("_","~"NF)}1' $InFile  \
|sort  |sed 's/~./_/' >$OutFile
... produced this OutFile ...
Code:
a/a_a1.dat
a/a_b1.dat
a/a_d1.dat
a/a_d2.dat
a/a_g1.dat
a/a_f/a_c1.dat
a/a_f/a_d1.dat
a/a_f/a_e1.dat
b/b_a1.dat
b/b_b1.dat
c/c_a1.dat
Daniel B. Martin

Last edited by danielbmartin; 08-03-2016 at 03:47 PM. Reason: Improved code
 
Old 08-04-2016, 08:10 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
How about 'find . -s'?
 
Old 08-04-2016, 09:05 AM   #6
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
tree ?
 
Old 08-04-2016, 11:10 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@NevemTeve - your command gives me an 'unknown predicate' error ... is '-s' part of a different version of find?
 
Old 08-04-2016, 11:18 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I admit, I cannot test it right now. I'd say find without any sort would do what the OP wants.
 
Old 08-04-2016, 09:43 PM   #9
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,797

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by NevemTeve View Post
How about 'find . -s'?
"-s"? Not familiar with that switch on any Unix/Linux variant I've ever used. (My OpenSUSE manpages and info are coming up dry when I search for that.) And... it returns:
Code:
find: unknown predicate `-s'
What's it supposed to do?

 
Old 08-04-2016, 10:01 PM   #10
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,797

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by syg00 View Post
No collating sequence I know will allow sort to put "g" before "f" (in an ascending sort). Forlorn hope for sort to do it unaided IMHO.
Well I had been trying to convince sort to pull that off with the '/' as a delimiter. Of course, I realize that sort doesn't care about the difference between files and directories but thought there might be a way. (I obviously don't use sort enough, I guess. Just the basics.)
 
Old 08-04-2016, 10:25 PM   #11
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,797

Original Poster
Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by AnanthaP View Post
tree ?
That might work.

My goal was/is to generate a listing of directories that I can cut-n-paste from to send off to users to have them clean up or, in other cases, get back to me so I know which files can be tarred up (which files go in what archives, etc.) or, preferably, the users will have a short command they can use /themselves/ before they tackle those tasks. (Hey! A guy can dream, can't he?)

I'll have to look whether the admins at the site I'm working with even bothered to install 'tree'. On my home systems, only my ancient RedHat system has it installed -- and I'm pretty sure I went with a barebones install on that one. If it's not there, I'll be stuck on square one. I'll check tomorrow.
 
Old 08-05-2016, 07:55 AM   #12
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Why not make multiple lists, each having a specific path depth, 100% compatible with standard sort this way
 
  


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
How to print the field separator in awk? 915086731 Linux - Desktop 5 09-04-2011 09:55 AM
print FS (field separator) in awk wakatana Programming 5 11-05-2009 08:17 AM
how to keep Field Separator in AWK when using a sub statement tmcguinness Programming 4 02-09-2009 02:24 PM
perl input field separator Tinkster Programming 5 10-18-2004 04:08 PM
My field separator changes when using awk Helene Programming 3 05-01-2004 08:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:14 AM.

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
Open Source Consulting | Domain Registration