Control Characters

Can somebody please help me with the query. ?

I want a part of program of which should look for control characters in the flat file , when it finds it, displaying message that Control Characters found..!

Please help me

Try this Insert ^M by using ^v and ^M

 
perl -0ne 'print "Control Chars found in $ARGV" if /^M/g' txt.txt
1 Like

Use the [:cntrl:] character class with any tool that supports regular expressions.

Regards,
Alister

3 Likes

Thanks Alister.. I am actually beginner in scripting.:confused:. so could you please help me writing the block which shill i do the required work ?:slight_smile:

Show your attempts first, and where you're stuck. Read the man pages for man grep (POSIX), man awk (POSIX), ... Otherwise we'd have to assume this is some kind of homework, and any help cheating is prohibited.

I tried that part but its not working..

The line terminator for a normal unix text file is ctrl/J (linefeed).
Are these text files or something else?
Can you give an example?

Hello Sir..

Wel i have a file called mireport.txt

which has values like this ( below is one row records)

"4208"|"INCIDENT"|"BaNCS Accurate file issues in LCT"|"RESOLVED"|"2011-06-10-12.19.08.563000"|2|2|2|"174021"|"2011-05-25-12.17.54.533000"|"174021"|"2011-05-25-12.17.54.000000"|||"BANCS"|||"2011-05-25-12.47.54.533000"|"2011-05-25-20.17.54.533000"|"2011-06-09-14.18.44.324000"|"2011-06-10-12.19.08.563000"|"BGL"|"NEST"|"2011-06-22-14.42.28.179000"|"253563"|"1025"||1||"174021"|"2011-05-25-12.17.54.533000"|"APP_SERVICES"|"BANCS"

I will be loading this to my data base.. before loading , my manager asked me2 check if it has control characters or not.. if it has i should not take this file for loading , displaying error msg saying it has control character..!!

I have written other part of code, but i want a part of code in unix, which will check if the file ( i have other files too) has control character or not.. based upon that display msg..

Please help me with the same.

If your grep supports extended regular expressions you can do something like this:

if grep -qE '[[:cntrl:]]' your_file
then
    echo Cannot process: File contains control characters!
    exit 1
fi

What Chubler_XL said is right.

Additionally you can redirect the output to logfile with timestamp.

Is this a standard unix text file with each line terminated with a linefeed character? If so, linefeeds will match the Regular Expression [[:ctrl:]] .

Hi methyl,

My Os version is SunOS 5.9.

However, the script is not working:
Error:

grep: illegal option --  q
 grep: illegal option --  E
 Usage: grep -hblcnsviw  pattern file . . .
 

Any suggestion?

Try /usr/xpg4/bin/grep instead of grep

Jean-Pierre.

Hi Jean,

I tried the above code in UAT server...

OS Version:

ksh:0$ uname -a
SunOS 5.9 Generic_122300-48 sun4u sparc SUNW,Sun-Fire-V440

My code is:

#! /bin/ksh
 
if /usr/xpg4/bin/grep -qE '[[:cntrl:]]' control_char_file.dat
then
    echo 'Cannot process: File contains control characters!'
    exit 1
else
    echo 'No control characters found!!!'
fi

The input file is:

F|852871
F|852872^M
- Saps.

I'm getting error like:

ksh:0$ ksh test_control_char.ksh
test_control_char.ksh[11]: usr/xpg4/bin/grep:  not found
No control characters found!!!

Permission of both files: -rwxr--r--

Please help.

Working fine now... :slight_smile:

---------- Post updated 06-30-11 at 05:33 AM ---------- Previous update was 06-29-11 at 10:20 AM ----------

Hi @Alister,

  1. Can we use

in perl script?

  1. We have a dos2unix validation in our perl script which will get the following command from DB using SELECT query:
dos2unix -437 -q <some_file_nm>

-437 is for Key Board format message.
What will '-q' do?