HI experts
I updated my question here to
eg:
$cat file
ABC: abc ABC FDFD
USB: usb usb_name usb_disk usbdriver USB
PA: PA pa paa
I want to how many usb exactly matched(not match usb_name or usbdriver) in the line cotains"USB:"
thanks in advance
HI experts
I updated my question here to
eg:
$cat file
ABC: abc ABC FDFD
USB: usb usb_name usb_disk usbdriver USB
PA: PA pa paa
I want to how many usb exactly matched(not match usb_name or usbdriver) in the line cotains"USB:"
thanks in advance
Here is the solution,
sed -r "s/\busb\b/Match/g" file
'\b' indicates the word boundary .
sed 's/^usb */match /g' a
This will match if the "usb" is in the beginning of a line.
consider the following line.
usb usb_name usb_disk usbdriver USB usb
The last "usb" is not getting matched.
[/COLOR]
can we get the match num eg:how many usb matched exactly
grep -c "pattern" file_name.
This will give how many times the given pattern is matched in the given file.
eg:
$cat file
ABC: abc ABC FDFD
USB: usb usb_name usb_disk usbdriver USB
PA: PA pa paa
I want to how many usb exactly matched(not match usb_name or usbdriver) in the line cotains"USB:"
sed -n '/USB:/p' file | grep -c "usb"
no no no
$cat file
ABC: abc ABC FDFD
USB: usb usb_name usb_disk usbdriver USB
PA: PA pa paa
I want to how many usb exactly matched(not match usb_name or usbdriver) in the line cotains"USB:"
See the following grep command. -w option will match the word.
grep -w "usb" input_file
no that's no the answer, I want to know how many matched
-c option it will give the number of count.
grep -wc "usb" usb
Refer the following Like for more grep options
http://unixhelp.ed.ac.uk/CGI/man-cgi?grep
no no no
$ cat xxx
b usb usb usb_name usb_disk usbdriver USB usb_du
$ grep -wc "usb" xxx
1
it should be 2
$ sed -r "s/\busb\b/Match/g" xxx
b Match Match usb_name usb_disk usbdriver USB usb_du
I want to know how many Match usb exactly
Is it ok?
sed 's/ /\n/g' usb | grep -cw "usb"
No, it will give the number of lines which match.
Ten occurrences on a single line will count as 1.
Try this,
cat input_file | tr ' ' '\n' | grep -wc "usb"
Try:
perl -lane 'print scalar grep {/\busb\b/} @F' file
$cat file
ABC: abc ABC FDFD
USB: usb usb_name usb_disk usbdriver USB
PA: PA pa paa
I want to how many usb exactly matched(not match usb_name or usbdriver) in the line cotains"USB:"
Ok..If you just need the total number of occurances in a file and not line by line,
Try:
perl -lane '$s+=scalar grep {/\busb\b/} @F; END { print $s; }' file
Example:
/home/usr1 >cat file
ABC: abc ABC FDFD
USB: usb usb_name usb_disk usbdriver USB
PA: PA pa paa
ifrff ds usb usb_stick and whatelsewithusb
usb at first line
at the end there is usb
usbstick at line
at the end testusb
/home/usr1>perl -lane '$s+=scalar grep {/\busb\b/} @F; END { print $s; }' file
4
---------- Post updated at 14:18 ---------- Previous update was at 14:05 ----------
Ok..I misunderstood. If you need the count only on lines containing USB
perl -lane '$s+=scalar grep {/\busb\b/} @F if /USB/; END { print $s; }' file
UUOC.
tr ' ' '\n' < input_file | grep -wc "usb"