Grep particular word from a line using grep/awk commands

I Tried this cmd:

# mount | grep -E '\s/dev/shm\s' | awk  -F "(" '{print $NF}'

Expectations:

rw,nosuid,nodev,seclabel

What I get this:

rw,nosuid,nodev,seclabel)

and I also tried this one:

mount | grep -E '\s/dev/shm\s' | awk  -F "(" '{print $NF}' | cut -c -24
rw,nosuid,nodev,seclabel

I can get the exact wordm but in default there is any one word was not there it will not work so i need to remove this character ) in the end, anyone know about this? Teach me to solve this.


I am guessing that the output of your mount | grep command is like this:

$ mount | grep -E '\s/dev/shm\s'
tmpfs on /dev/shm type tmpfs (rw,nosuid,nodev,inode64)

So what you want (again, I am guessing here) is the text between the parentheses on lines containing /dev/shm. If so, try:

$ mount | grep -oP '\s/dev/shm\s.+\(\K[^)]+'
rw,nosuid,nodev,inode64

The -o option tells grep to only print the matching part of the line and the -P enables PCRE (Perl Compatible Regular Expressions) which give us \K for "ignore everything matched up to this point". So the regular expression will look for /dev/shm surrounded by whitespace, then as many characters as possible until an opening parenthesis (.+\(). Everything up to here is now ignored because of the \K and then we just match the longest stretch of non-) characters ([^)]+).


If you really need to use awk for some reason, there's no need for grep:

$ mount | awk -F'[()]' '/\s\/dev\/shm\s/{print $(NF-1)}'
rw,nosuid,nodev,inode64

findmnt is usually used to find and show details like device names, mounts, or file system types.

findmnt -n --target /dev/shm --output=options

Active mounts are found in /proc/mounts and has a fixed number of columns, which makes it easy to parse:

while read -r _ a _ b _; do
    [[ $a = /dev/shm ]] && echo $b
done < /proc/mounts