Link to home
Start Free TrialLog in
Avatar of Swaminathan K
Swaminathan KFlag for India

asked on

Extract only group and owner name in the ls -l output

Hi Team,

I want to extract the group name and owner name from the ls -l command output , below is the command which I have written , not able to get the output .

ls -l | sed -n '/^-/s/^-.* [0-9] \([[:alpha:]] [[:alpha:]]\).*/\1/p'


-rw-r--r--. 1 root  root      0 Nov 12 23:53 abc.txt
-rw-------. 1 root  root   2629 Nov 11 01:17 anaconda-ks.cfg
-rw-r--r--. 1 root  root    139 Nov 12 22:13 CALNOV
-rw-r--r--. 1 root  root      0 Nov 12 23:54 cc
-rw-r--r--. 1 root  root      0 Nov 13 00:05 $file
-rw-r--r--. 1 root  root     48 Nov 12 21:56 filename
-rw-r--r--. 1 root  root  41955 Nov 11 01:17 install.log
-rw-r--r--. 1 root  root   9154 Nov 11 01:11 install.log.syslog
-rw-r--r--. 1 root  root   2682 Nov 12 22:42 logdata
-rw-r--r--. 1 root  root   1054 Nov 12 21:58 LOGFILENAME
-rw-r--r--. 1 root  root     81 Nov 13 07:22 meals
-rw-r--r--. 1 root  root     82 Nov 13 05:20 names
-rw-r--r--. 1 root  root     79 Nov 13 07:27 newnames
-rw-r--r--. 1 root  root    139 Nov 12 22:09 novcal
-rw-r--r--. 1 root  root      0 Nov 12 23:52 test
-rw-r--r--. 1 root  root     13 Nov 10 21:25 test1
-rw-r--r--. 1 root  root      0 Nov 12 23:52 test2

i want to show only root root in the output  for all the files
Avatar of Alfredo Luis Torres Serrano
Alfredo Luis Torres Serrano
Flag of United States of America image

Try this:

find . -type d -exec ls -ld {} \; | awk '{print $3,$9}' | grep -v oasitqtc

It should give you desired output

However better code will be with egrep as states below :

find . -type d -exec ls -ld {} \; | awk '{print $3,$9}' | egrep  -v "^oasitqtc"

Try both :)
Hope this helps
Avatar of Swaminathan K

ASKER

Hi ,
I want to try it using sed command only. Can help where Iam going wrong in the regular expression.
ASKER CERTIFIED SOLUTION
Avatar of noci
noci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can you try this:

ls -l | sed -n 's/\(^-.\{9\}\)\(  *\)\([0-9]\) \([a-z][a-z]*\)\(  *\)\([a-z][a-z]*\).*/\4 \6/p'

One thing that is wrong with your pattern is that you have one space between your 2 alpha's where you sample output has 2 spaces in between.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial