LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   awk AND syntax (https://www.linuxquestions.org/questions/programming-9/awk-and-syntax-809935/)

webhope 05-25-2010 01:50 AM

awk AND syntax
 
I have a condition like this //{}
And I would like to extend the condition to the and condition. Is this syntax correct?
// && // {} ... or // // {} ?
No other ideas...

grail 05-25-2010 02:08 AM

I have found this to be a good reference: www.gnu.org/manual/gawk/html_node/index.html

webhope 05-25-2010 02:17 AM

I watched "Conditional Exp" and "If statement" section but no help there. "5.11 Boolean Expressions" does not fit .

webhope 05-25-2010 02:29 AM

What is wrong on this syntax?

Code:

if ($0 !~ /^[[:space:]]*title/ && 
$0 !~ /^[[:space:]]*hide/ &&
$0 !~ /^[[:space:]]*unhide/)  { print $0; }


grail 05-25-2010 02:31 AM

Except the semicolon after $0 I see no issue

webhope 05-25-2010 02:35 AM

Code:

[root@localhost]# echo "Hi world" | awk 'if ($0 ~ /^[[:space:]]*title/ &&  $0 ~ /^[[:space:]]*hide/ && $0 ~ /^[[:space:]]*unhide/)  { print $0 }'
awk: if ($0 ~ /^[[:space:]]*title/ &&  $0 ~ /^[[:space:]]*hide/ && $0 ~ /^[[:space:]]*unhide/)  { print $0 }
awk: ^ syntax error


webhope 05-25-2010 03:01 AM

Never mind (Fuck it).

I have this code:
Code:

block=' title sata XP (no mapping)
 unhide (hd0,2)
 hide (hd0,1)
rootnoverify (hd0,20)
chainloader +1
makeactive
savedefault'
            uuid=($(echo "$block" | awk '/.*UUID=/{ x=gensub(/.*UUID=([-0-9A-F]+)[ )].*/,"\\1","g");  print x}')) # Druhá varianta:  echo $source  |  awk 'BEGIN{RS="UUID="}/-/{gsub(/ .*/,"");print}'

            hd=($(echo "$block" | awk 'BEGIN{FS="\n"; RS="hd"} !/^[[:space:]]*title/ && !/^[[:space:]]*hide/ && !/^[[:space:]]*unhide/ { x=gensub(/([[:digit:]],[[:digit:]]+)).*/,"\\1)","g");print $0="(hd"x}'));
echo $hd

Returns:
(hd0,2)

However I wanted to say: "Exclude line beginning on ^title or ^unhide or ^hide and then do {}"

So it should return (hd0,20) not line #2.

grail 05-25-2010 04:31 AM

"If" statement in awk is to appear between braces {}, but if you choose to review the page I showed you, what you are looking for can be solved
without the "if()" function just include the rest: example
Code:

$0 ~ /^[[:space:]]*title/ &&  $0 ~ /^[[:space:]]*hide/{}

webhope 05-25-2010 04:39 AM

Quote:

Originally Posted by grail (Post 3980380)
what you are looking for can be solved
without the "if()" function

This is what I try from tomorrow. I try it again, but no success.

Code:

block=' title sata XP1 (bez premapování)
 unhide (hd0,2)
 hide (hd0,1)
rootnoverify (hd0,20)
chainloader +1
makeactive
savedefault'
hd=($(echo "$block" | awk 'BEGIN{FS="\n"; RS="hd"} $0 !~ /^[[:space:]]*title/ && $0 !~ /^[[:space:]]*hide/ && $0 !~ /^[[:space:]]*unhide/ { x=gensub(/([[:digit:]],[[:digit:]]+)).*/,"\\1)","g");print $0="(hd"x}')); # my job (array working)
echo $hd

Why this still returns (hd0,2) instead (hd0,20)?

grail 05-25-2010 04:58 AM

Once again the issue is you do not break anything down to see its effect. Try running the following and it should be obvious why you get your result:
Code:

block=' title sata XP1 (bez premapování)
 unhide (hd0,2)
 hide (hd0,1)
rootnoverify (hd0,20)
chainloader +1
makeactive
savedefault'

echo "$block" | awk 'BEGIN{FS="\n"; RS="hd"}1'


webhope 05-25-2010 05:09 AM

Sorry I don't understand you what you want I to do. I don't know what you want to say.

Edit:
You think I incorrectly set separators? But there is not problem in them. I tested that the condition //{} decides what line will be processed to gensub.

MTK358 05-25-2010 05:52 AM

It's

Code:

(a > b && c == d) { print }
NOT:

Code:

if(a > b && c == d) { print }

grail 05-25-2010 05:55 AM

Have you run the above??
The output I get is:

Code:

title sata XP1 (bez premapování)
 unhide (    #end of first record
0,2)
 hide (      #end of second record
0,1)
rootnoverify (    #end of third record
0,20)
chainloader +1
makeactive
savedefault    #end of fourth record

Based on this output and without any of the testing or gensub changes you cannot hope to get the desired results.

I will choose to look at the second record alone:
Code:

0,2)
 hide (      #end of second record

Now let us look at your tests one at a time based on this data being equal to $0:

1. $0 !~ /^[[:space:]]*title/ - does this record start with any number of spaces followed by the word "title" - answer - no - therefore true

2. $0 !~ /^[[:space:]]*hide/ - does this record start with any number of spaces followed by the word "hide" - answer - no - therefore true

3. $0 !~ /^[[:space:]]*unhide/ - does this record start with any number of spaces followed by the word "unhide" - answer - no - therefore true

4. Process the following braces as all these statements are true

Hence I do not believe you have tested this anywhere near as much as you should have.

webhope 05-25-2010 06:38 AM

I have run your code. But I hadn't the idea. I didn't realize that the output could change. Well, next time I have to check it. Thanx

webhope 05-25-2010 07:38 AM

...........


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