Solved

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

Posted on 2000-04-18
2
360 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
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.

762 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

13 Experts available now in Live!

Get 1:1 Help Now