Solved

How to count the number of a particular string in a file?

Posted on 2000-04-18
2
371 Views
Last Modified: 2010-04-21
I wonder how to count the number of appearances of a particular string in a file? grep -c  can only find the number of lines that contain the string.
Another question is how to substitue a non-printable character into a return
 character "\n" using sed?  The following command seems doesn't work.
#top -d 1 >f1
#sed "1,$ s/\033/\012/g" f1
Why?   Here \033 stands for ASCII code 033 which displays as ^[ (non-printable),
\012 is "carriage return".
0
Comment
Question by:shiqi
2 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 2725830
perl -ne 'BEGIN{$/="string"} END{print $.-1,"\n"}' f1
perl -pe 's/033/\012/g' f1
0
 

Accepted Solution

by:
mdchachi earned 30 total points
ID: 2727969
non-perl answers:

1)
cat somefile | sed -n ' :LOOP
         /string/ {
                  p
                  s/string//
                  b LOOP
        } ' | grep -c string

2) not sure about sed, what about tr?

cat file | tr  '\033'  '\012'
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sort command HPUX 11 81
Linux : can't create transaction lock error 1 76
Image not there 4 56
Solaris 4.1.3 cloning and booting 13 83
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question