LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-30-2008, 02:23 PM   #1
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Rep: Reputation: 15
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
 
Old 06-30-2008, 02:48 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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"
                        }
                }'
 
Old 06-30-2008, 03:11 PM   #3
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Original Poster
Rep: Reputation: 15
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
 
Old 06-30-2008, 04:12 PM   #4
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Original Poster
Rep: Reputation: 15
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
 
Old 06-30-2008, 05:12 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by indiancosmonaut View Post
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" ......
 
Old 06-30-2008, 05:19 PM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by indiancosmonaut View Post
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!
 
Old 06-30-2008, 08:57 PM   #7
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Original Poster
Rep: Reputation: 15
Thanks colucix!
It was really a good learning.

Kind regards,

indiancosmonaut
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
awk does not seem to recognize character classes new_2_unix Linux - Newbie 6 10-15-2007 06:36 AM
awk error 'Invalid collation character: ' Mash Programming 1 04-15-2007 02:44 AM
Insert character by using sed/awk manish_meet_in Linux - General 3 04-05-2007 01:19 PM
awk escape character for colon in string quadmore Programming 2 02-27-2007 05:56 PM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 06:07 PM


All times are GMT -5. The time now is 11:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration