Solved How many selections with grep

Is there any way find out how many occurences were found when running grep()with a particular pattern?

I would like query each selection further and not sure how to do that.
 
🥱 Pattern occurs at most once per line:​
Bash:
grep --count 'pattern' ./sample_file
Pattern can occur multiple times per line: 🧮
Bash:
grep --only-matching 'pattern' ./sample_file | wc -l
If you want to “query” the selected lines, it sounds like the file is made up of some structured data (e. g. an HTML document), am I right? 💻 Then there are possibly specialized tools for this job.​
 
It looks like sed() would be better suited than grep() to do what I want, except I can't quite figure the correct script...

I want to be able to umount numerous mounts, eg starting with /dev/da0*

This is close, but not quite:

umount | sed -n -e '/\/dev\/da0/s/\/dev\/da0.*$/umount \/dev\/da0../p'

It returns umount /dev/da0.. instead of umount /dev/da0s1

What am I missing?
 
It looks like sed() would be better suited than grep() to do what I want, except I can't quite figure the correct script...

I want to be able to umount numerous mounts, eg starting with /dev/da0*

This is close, but not quite:

umount | sed -n -e '/\/dev\/da0/s/\/dev\/da0.*$/umount \/dev\/da0../p'

It returns umount /dev/da0.. instead of umount /dev/da0s1

What am I missing?

for such tasks I usually use a short for i in $(cmd); do loop.
you can first interactively work out the proper 'cmd' to get the output you want to hand over to e.g. (u)mount (ideally in a sh(1) shell, so you can easily transform it into a .sh script), then just copy the whole line and use it for the loop.
e.g.:
Code:
% ls /dev | grep -s ^ada0
ada0
ada0p1
ada0p2
% ls /dev | grep -s ^ada0p
ada0p1
ada0p2
% for i in $(ls /dev | grep -s ^ada0p); do
umount /dev/$i
done

and as a general rule of thumb: if you find yourself piping output of grep and sed (and sort, uniq, cut...) into one another multiple times, awk(1) might be the better tool for the job
 
mount | sed -n -e '/\/dev\/da0/s/ .*$//p'

returns:-
/dev/da0s2
/dev/da0s1

Just can't figure out how to prefix these lines with umount


mount | sed -n -e '/\/dev\/da0/s/ .*$//p' -n -e 's/^/umount /p'


returns:-

umount /dev/ada0s4a on / (ufs, NFS exported, local, soft-updates, journaled soft-updates)
umount devfs on /dev (devfs)
umount 192.168.1.5:/ on /net (nfs, noatime)
/dev/da0s2
umount /dev/da0s2
/dev/da0s1
umount /dev/da0s1
 
sko has an elegant solution for you above.

If you want to execute the umount(8) command from within your script, as opposed to just printing the command to stdout, you have to invoke a shell, e.g.
Code:
eval $(mount | sed -n -e 's;^\(/dev/da0[^ ]*\) on .*;umount \1;p')
or
mount | sed -n -e 's;^\(/dev/da0[^ ]*\) on .*;umount \1;p' | /bin/sh
 
OK, weird that you initially asked about grep(1) and match counts. ⌛ It is not necessary to unmount only file systems that are currently mounted. 🚽
Bash:
umount /dev/da0p* 2>&-   # The 2>&- closes Standard Error (so there are no diagnostic messages).
umount(8) returns an error ($? variable) though if any file system could not be unmounted (e. g. because it wasn’t mounted in the first place).​
Bash:
mount -p | cut -f 1 | grep '^/dev/da0p' | xargs umount
will at least shut up (subject to TOCTOU) about currently not mounted devices, but there still may be busy device errors.​
 
Back
Top