Find invalid character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find invalid character
# 1  
Old 10-29-2014
Find invalid character

HI Team,

I have script to find the invalid character in file.

Code:
f=’pallvi\mahajan’
                 n=0
 while (( $n <= ${#f} ));
 do
 c="${f:$n:1}"
 echo '$c'
 if [[ "$c" = *[^[:space:]*] ]];
 then 
 grep -sq $c valid.txt
 if  [ $? -eq  1 ];
 then
 echo "$f" >> f.txt
 break
 fi
 fi
 n=$((n+1))
 done

My valid.txt file is
Code:
a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E  F G H I J K L M N O P Q R S T U V W X Y Z ( ) { }  \t 1 2 3 4 5 6 7 8 9 0 - / :  ? + \n . , '

But my script is not searching \ character which is string f='pallvi\mahajan'
Please let me know how i can do this.

I have run this script in debugging mode and it is running but this variable is not putting in file f.txt
while running script in debussgibg mode, i checked grep -sq is giving error when searching \
exit status is 2 in that time.
# 2  
Old 10-29-2014
Your script is wrong in many ways.

Code:
f=’pallvi\mahajan’

This attempts to run the command pallvi\mahajan and stores its output, if any, in f. Since no such command exists, you get nothing. I believe you meant:

Code:
f='pallvi\mahajan'

Also, variables don't expand in single quotes. echo '$c' will print the literal text $c. I believe you meant echo "$c"

Also, some characters have a special meaning to grep. \ for example. To make grep take all characters you give it literally, tell it -F.

Also, you don't need to use $? to fit grep into an expression. Just use if ! grep ... then ...

Also, if you know your shell has [[ string == *str* ]], you could use it instead of grep. Or you could replace valid.txt and most of this program with one case, really.
# 3  
Old 10-29-2014
The #1 mistake is the grep.
1. quote "$c"
2. a single \ is an RE error, must be \\
Because one can prepend a \ to a normal character, you can do
Code:
grep -sq \\"$c" valid.txt

\\ is necessary for the shell; it will pass a single \ to grep.
You better use a non-RE grep
Code:
fgrep -sq "$c" valid.txt

# 4  
Old 10-29-2014
thanks but this did not work for another character getting backrefrence error
Code:
grep -sq \9 valid.txt

while running script in debug mode, i found this error and also this are passing another character also backslash
# 5  
Old 10-29-2014
Ah, even \9 is a special meaning in an RE. Not to speak of special characters.
Then take my 2nd suggestion, a non-RE grep!
# 6  
Old 10-29-2014
This also not work fgrep

can you suggest me something

---------- Post updated at 03:49 PM ---------- Previous update was at 03:37 PM ----------

while runnig in script in debug mode, i find this using fgrep.
Code:
  echo '$c'
 $c
 + [[ \ = *[^[:space:]*] ]]
 + grep -sq '\' valid.txt
 last character is \
 + '[' 2 -eq 1 ']'
 + n=7
 + ((  7 <= 14  ))
 + c=m
 + echo '$c'

# 7  
Old 10-29-2014
Moderator's Comments:
Mod Comment Posting "Does not work" without explanation does not help you or anyone. If a command does not work for you, please show the exact circumstances you used it, and the exact error or malfunction you received. Do not paraphrase errors, or post the text as links, images, or attachments if you can avoid it: Paste the exact message, in code tags, like [code] text [/code] or by selecting the text and using the Image button.

Thank you.

The UNIX and Linux Forums
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching invalid character in list of client name

Hi Friend, I have a client name list and client name has some invalid character due to which some issue raised and list of client are15k. I want to make script who find invalid character name. can you please help me how i can make script, i means i need logic. Valid character are :- ... (5 Replies)
Discussion started by: pallvi_mahajan
5 Replies

2. AIX

Bison -pap_expr_yy invalid character:% unexpected "identifier" while running make for Apache2.4.3 64

The Follwing packages are installed on my AIX 6.1 box gcc-4.7.2-1 gcc-c++-4.7.2-1 gcc-cpp-4.7.2-1 gcc-gfortran-4.7.2-1 libgcc-4.7.2-1 libgomp-4.7.2-1 libstdc++-4.7.2-1 libstdc++-devel-4.7.2-1 gmp-5.0.5-1 libmpc-1.0.1-2 libmpc-devel-1.0.1-2 libmpcdec-1.2.6-1 libmpcdec-devel-1.2.6-1... (0 Replies)
Discussion started by: Ashish Gupta
0 Replies

3. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

4. Shell Programming and Scripting

How to find invalid URL in a text file using shell script?

How to find and remove invalid URLs in a text file using shell script? (1 Reply)
Discussion started by: vel4ever
1 Replies

5. Shell Programming and Scripting

Find and replace a character

Hi Team, i have 1st cloumn of data containing, LAMSBA01-BA-COFF-YTD LAMSBA01-BA-COFF-ITD LAMSBA01-BA-AGGR-IND . LAMSBA01-BA-CURR-COFF-BAL i need to replace the "-" to "_" (underscore) using AWK . please help me on this. Thanks, Baski (4 Replies)
Discussion started by: baskivs
4 Replies

6. Solaris

An invalid XML character (Unicode: 0x1a)

While uploading an exl file to my application in Solaris 10 the upload failed with error Error! Parsing Error: /SPLM/TC83/tcdata83/model/model_dbextract.xml Line:65576 Column:73 An invalid XML character (Unicode: 0x1a) was found in the value of attribute "unitOfMeasureSymbol" and element is ... (12 Replies)
Discussion started by: karghum
12 Replies

7. UNIX for Dummies Questions & Answers

to find alphanumeric character

Hi All, I am having a data file which consists of lakhs of records in the below foramt: "1223323","4341341","discription aadfad" "3123123","5463456","discription aadfad" "2343324","6565767","discription asdfads" "A3423423","7957456","discription aadfad" "343453B","7957456","discription... (1 Reply)
Discussion started by: abhi_123
1 Replies

8. Linux

Invalid Character

Hi, I am using a Perl script to generate a report file in Linux server. When my input data contains an invalid character which looks like hyphen after that my program is printing junk values in the report. Why that symbol is causing issue and is there a way to tell the server that this is a valid... (1 Reply)
Discussion started by: lawrance_ps
1 Replies

9. Shell Programming and Scripting

Find ^M Character

Hi Experts, Is there a way to find whether a file contains ^M character in it?. It's quite obvious to vi, but can this be accomplished using an automated way to find recursively in a directory. probably i guess the best way would be to 1) Read the file and check if the end of line is ^M... (8 Replies)
Discussion started by: ganga.dharan
8 Replies

10. Shell Programming and Scripting

writing shell script to find line of invalid characters

Hi, I have to write s script to check an input file for invalid characters. In this script I have to find the exact line of the invalid character. If the input file contain 2 invalid character sat line 10 and 17, the script will show the value 10 and 17. Any help is appreciated. (3 Replies)
Discussion started by: beginner82
3 Replies
Login or Register to Ask a Question