LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Character \ in awk (http://www.linuxquestions.org/questions/programming-9/character-%5C-in-awk-652619/)

indiancosmonaut 06-30-2008 01:23 PM

Character \ in awk
 
Hi,

I am trying to find if the following set of characters \ ' " * #
are present in the a string...

Code:

x=`grep \* test.123`

echo "$x"

echo | awk -v list="$x" -v R="\\\\ \# * \" \'" ' {
                        split(R,ARRAY," ");
                        for (i in ARRAY)
                        {
                                print ":"ARRAY[i]":";
                                if (index(list,ARRAY[i]) > 0)
                                        print ARRAY[i],"is present"
                        }
                }'

But it does not point to character \ and lets it pass.

OUTPUT

Code:

$ ./test.sh
a b c * 1 2 3 \ ABC ' " #
:#:
# is present
:*:
* is present
:":
" is present
:':
' is present
:\:
$

Could anyone explain the reason for this.

Kind regards,

indiancosmonaut

colucix 06-30-2008 01:48 PM

When you pass the list = $x, it should have a double \\ otherwise it is interpreted by awk as the escape character. Practically the same as you did when passing R. If the value $x is extracted from a file using grep, you should substitute every instance of \ with \\, for example using sed:
Code:

echo | awk -v list="$(echo $x|sed s/\\\\/\\\\\\\\/g)" -v R="\\\\ \# * \" \'" ' {
                        split(R,ARRAY," ");
                        for (i in ARRAY)
                        {
                                print ":"ARRAY[i]":";
                                if (index(list,ARRAY[i]) > 0)
                                        print ARRAY[i],"is present"
                        }
                }'


indiancosmonaut 06-30-2008 02:11 PM

Hi colusix,

Got your point.
But now its not picking up *

Code:

$ ./test1.sh
a b c * 1 2 3 \ ABC ' " #
:#:
# is present
:*:
:":
" is present
:':
' is present
:\:
\ is present
$

I think because * is replaced by all the files and directores present in the pwd. I am trying to find a way out.
With you experience, could you advice on a solution.

Kind regards,

indiancosmonaut

indiancosmonaut 06-30-2008 03:12 PM

An observation
 
Another observation:
When I grep \ from a string, it displays well.
But when I try to assign the output to a variable it displays

last character is \

in the next line. And when I try to echo the variable, its empty.

What could be the reason behind this? Thanks in advance for your help.

Code:

$ cat test.123
a b c * 1 2 3 \ ABC ' " #
$ grep \\\\ test.123
a b c * 1 2 3 \ ABC ' " #
$ back_slash=`grep \\\\ test.123`
last character is \
$ echo $back_slash

$

Kind regards,

indiancosmonaut

colucix 06-30-2008 04:12 PM

Quote:

Originally Posted by indiancosmonaut (Post 3199535)
I think because * is replaced by all the files and directores present in the pwd. I am trying to find a way out.

Yes, sorry. It is the echo command which expands * not enclosed in double quotes. You can solve this issue modifying the line as
Code:

echo | awk -v list="$(echo "$x"|sed s/\\\\/\\\\\\\\/g)" ...
using double quotes in echo "$x". Or in your specific script you can apply the sed substitution together with the grep command:
Code:

x=`grep \* test.123 | sed 's/\\\\/\\\\\\\\/g'`

echo "$x"

echo | awk -v list="$x" ......


colucix 06-30-2008 04:19 PM

Quote:

Originally Posted by indiancosmonaut (Post 3199570)
But when I try to assign the output to a variable it displays

last character is \

in the next line. And when I try to echo the variable, its empty.

This is because of expansion in back ticks used for command substitution. One of the \ is interpreted as continuation character. You can prevent this using the $( ) syntax for command substitution:
Code:

$ cat test.123
a b c * 1 2 3 \ ABC ' " #
$ back_slash=$(grep \\\\ test.123)
$ echo "$back_slash"
a b c * 1 2 3 \ ABC ' " #

It is not easy to explain all these nuances, but testing and testing again is the way for learning! :)

indiancosmonaut 06-30-2008 07:57 PM

Thanks colucix!
It was really a good learning.

Kind regards,

indiancosmonaut


All times are GMT -5. The time now is 02:07 PM.