LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 02-25-2012, 08:46 AM   #16
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389

Ok, the problem here is with finding out the file/directory stuff. That's why I used find in the first place -- because it's printf feature has the '%y' option. Since it seems to be GNU only extension, we'll have to find a different way of solving this. We can use the first letter of the file permissions entry in the output of ls -l. So we'll have d for directories and - for files. Let's start with AWK:

Code:
ls -l|awk '{ print $9"|0|0|"$9 substr($1,0,1) }'
This assumes that the filename is the column $9 in the output of ls. (On some systems, it may be $8, though). It will also break if the filenames contain spaces because the names will be broken into multiple columns. We can do slightly better if we remove all columns from the first one up to $8 and print the entire line:

Code:
ls -l|awk '{ x=substr($1,0,1); for(i=1;i<9;i++){$i=""}; match ($0,"^ *"); $0=substr($0,RLENGTH+1); print $0"|0|0|"$0x}'
this can hardly be considered a neat one-liner, though. A slightly shorter alternative would be to print all columns from $9 on in a loop:

Code:
ls -l|awk -F' +' '{ x=$9; for(i=10;i<=NF;i++){x=x" "$i}; print x"|0|0|"x substr($0,0,1)}'
This will, however, break, if the filenames contain multiple consequent spaces, because we assumed that columns are separated by single spaces, which is not neccesarily the case.
Alternatively, here is a somewhat ugly, but shorter sed version:

Code:
ls -l | sed -e 's/^\(.\)\(\S* *\)\{8\}\(.*\)/\3|0|0|\3\1/'

Last edited by millgates; 02-25-2012 at 08:48 AM.
 
  


Reply



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
[SOLVED] Add filename to file output sarenace Programming 4 02-06-2012 04:34 PM
[SOLVED] Sed – how do I save output to file with filename from content of another file? misarab Programming 4 02-28-2011 02:12 PM
sed add a character to the end of each line keenboy Linux - General 2 08-05-2010 12:36 PM
How to make alterations to a file using sed , saving changes and retaining filename ? uncle-c Linux - General 2 07-02-2009 10:46 AM
sed: print section of file from string to end of file samyboy Linux - Newbie 4 02-26-2008 07:23 AM

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

All times are GMT -5. The time now is 03:51 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