Operating System - HP-UX
1826598 Members
3513 Online
109695 Solutions
New Discussion

Re: How to replace a non-printable characters?

 
Zhang Zhao gui
Frequent Advisor

How to replace a non-printable characters?

My 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
1#sed "1,$ s/\033/\012/g" f1
Why? Here \033 stands for ASCII code 033 which displays as ^[
(non-printable), \012 is "carriage return".

Another question is 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.
3 REPLIES 3

Re: How to replace a non-printable characters?

Your top question has been answered on the other message thread. This message
is in response to your question "how to count the number of appearances of a
particular string in a file?"

Use the command 'wc'. 'wc -l' will count the number of lines it appears on;
'wc -w' will count the number of words. For more information, see the man page
for wc(1).

For example:
# grep abcd testfile |wc -l

where abcd is the string I am looking for
testfile is the filename

The results will look like the following:
# grep abcd testfile |wc -l
6

The same 'wc -l' executed on the file, will show the total lines within the
file.
# wc -l testfile
21
Zhang Zhao gui
Frequent Advisor

Re: How to replace a non-printable characters?

But if one line contains two "abcd", then your calculation is wrong, right? I
don't want to calculate
the total number of words or lines, I only want to
find out the number of occurrence of certain string.

Not to mention "top", how can I replace a non-printable character such as
\0344 with \012 if it is followed by another string "]B" ?
Sriram Nadiminti
Occasional Advisor

Re: How to replace a non-printable characters?

Use a for and case combinations.

The following gives you the idea. Syntax is to be built.


j=0
for i in `cat yourfilename`
do
case $i=yourstring) j=j+1;;

case *) NOOP ;; # you can put a favourite no operation
esac
done
echo $j

This works on most of text files