How to print only certain characters?

5,941

Solution 1

I think you want this command:

ls -l partition | cut -c5-7 | tr rwx cse |sed 's/-//'

You can remove the one extra command(cut -d ' ' -f 1) and replace it with your last cut command(cut -c5-7) and also add sed 's/-//' at the end to remove all -s. Now you are done. you didn't need to adding extra |.

And even better: you can also change the dash(- character with Null character(\0) as following):

ls -l partition | cut -c5-7 | tr 'rwx-' 'cse\0'

Replaced - char with null character(\0).

Solution 2

Alternative way without ls:

getfacl -c partition | sed -n '/group::/{s/.*:://;y/rwx/cse/;s/-//g;p;}'

Solution 3

Another alternative (piping two tr commands):

ls -l partition | cut -c5-7 | tr -dc rwx | tr rwx cse

Share:
5,941

Related videos on Youtube

laurentiupiciu
Author by

laurentiupiciu

Updated on September 18, 2022

Comments

  • laurentiupiciu
    laurentiupiciu almost 2 years

    I have to translate the permissions of a file this way:

    r ► c 
    w ► s 
    x ► e
    

    Then, I must extract the group of characters which are related to owner group. Last step is to print that group of characters without "-" (only letters).

    I managed to write a command till now:

    student@vm-uso ~team2 $ ls -l partition
    -rw-r--r-- 1 student student 10485760 nov 24 21:04 partition
    student@vm-uso ~team2 $ ls -l partition | cut -d ' ' -f 1| tr rwx cse | cut -c5-7
    c--
    student@vm-uso ~team2 $
    

    Forwards, I have to add another '|' in order to print only the letter (without the characters '-').

    • terdon
      terdon over 9 years
      Hi and welcome to the site. Please edit your question and 1) replace the screenshot with a copy/paste of the actual text (see here for help on formatting your post) 2) Show us the output you want. Don't describe it, give us an actual example that we can produce for you.
    • Scott - Слава Україні
      Scott - Слава Україні over 9 years
      Another way to filter out the dashes is | tr -d -, which is a little bit like tr - "\0" except it doesn't insert nul bytes, it just deletes the dashes.