Solved

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

Posted on 2000-04-18
2
362 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

914 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now