Solved

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

Posted on 2000-04-18
2
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

696 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