I have script to find the invalid character in file.
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
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.
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:
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.
I have one file in which client names are there, and i need to find client name which has invalid character.
So i make this script.
while read line
do
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
done < client.txt
and my valid character are
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 finding \ character and by using fgrep also it is giving error . I dont know what are invalid character but i have list of valid character which are mentioned above.
I have one client name in which \ slash are there and some how my script is not picking that one line.
Hope this clarify you and please help me how i can correct it.
See updated post: you will need the -r option of read to stop it interpreting the backslash as an escape character on input.
To explain the two lines:
[[ "$c" = '\' || "$c" = '?' ]] && c=\\"$c"
This code excapes a couple of characters that are specially treated by the Pattern substitution feature of bash. \ escapes the next character. ? is a wildcard representing any single character.
if [[ "${v/$c/}" = "${valid[@]}" ]]
This code tests if replacing your character with nothing changes the valid characters list. If these strings are the same it means the input character is not in the valid characters list.
You should always start with the bash man entry for example Pattern subsitution:
${parameter/pattern/string}
Pattern substitution. The pattern is expanded to produce a pat-
tern just as in pathname expansion. Parameter is expanded and
the longest match of pattern against its value is replaced with
string. If pattern begins with /, all matches of pattern are
replaced with string. Normally only the first match is
replaced. If pattern begins with #, it must match at the begin-
ning of the expanded value of parameter. If pattern begins with
%, it must match at the end of the expanded value of parameter.
If string is null, matches of pattern are deleted and the / fol-
lowing pattern may be omitted. If parameter is @ or *, the sub-
stitution operation is applied to each positional parameter in
turn, and the expansion is the resultant list. If parameter is
an array variable subscripted with @ or *, the substitution
operation is applied to each member of the array in turn, and
the expansion is the resultant list.
If you are unsure how something will work try a small example from the command prompt as I did above.
I'll be around tomorrow and will check this thread for any further questions.