Score:0

Grep particular word from a line using grep/awk commands

sd flag
lee

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.

FedKad avatar
cn flag
Did you try: `mount | grep -E '\s/dev/shm\s' | awk -F "[()]" '{print $2}'` ?
sd flag
lee
thanks for the reply! It works fine
Score:2
cn flag

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
sd flag
lee
Thanks a lot! It works fine.
Score:2
cn flag

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
sd flag
lee
Thanks for the reply! It works fine
terdon avatar
cn flag
@lee, If one of the answers here solved your issue, please take a moment and [accept it](//askubuntu.com/help/someone-answers) by clicking on the checkmark on the left. That is the best way to express your thanks on the Stack Exchange sites.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.