LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 05-25-2010, 01:50 AM   #1
webhope
Member
 
Registered: Apr 2010
Posts: 184

Rep: Reputation: 30
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...
 
Old 05-25-2010, 02:08 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,424

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
I have found this to be a good reference: www.gnu.org/manual/gawk/html_node/index.html
 
Old 05-25-2010, 02:17 AM   #3
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
I watched "Conditional Exp" and "If statement" section but no help there. "5.11 Boolean Expressions" does not fit .
 
Old 05-25-2010, 02:29 AM   #4
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
What is wrong on this syntax?

Code:
if ($0 !~ /^[[:space:]]*title/ &&  
$0 !~ /^[[:space:]]*hide/ && 
$0 !~ /^[[:space:]]*unhide/)  { print $0; }
 
Old 05-25-2010, 02:31 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,424

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
Except the semicolon after $0 I see no issue
 
Old 05-25-2010, 02:35 AM   #6
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
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

Last edited by webhope; 05-25-2010 at 02:39 AM.
 
Old 05-25-2010, 03:01 AM   #7
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
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.

Last edited by webhope; 05-25-2010 at 04:41 AM.
 
Old 05-25-2010, 04:31 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,424

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
"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/{}
 
Old 05-25-2010, 04:39 AM   #9
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by grail View Post
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)?
 
Old 05-25-2010, 04:58 AM   #10
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,424

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
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'
 
Old 05-25-2010, 05:09 AM   #11
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
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.

Last edited by webhope; 05-25-2010 at 05:16 AM.
 
Old 05-25-2010, 05:52 AM   #12
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
It's

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

Code:
if(a > b && c == d) { print }
 
Old 05-25-2010, 05:55 AM   #13
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,424

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
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.
 
1 members found this post helpful.
Old 05-25-2010, 06:38 AM   #14
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
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
 
Old 05-25-2010, 07:38 AM   #15
webhope
Member
 
Registered: Apr 2010
Posts: 184

Original Poster
Rep: Reputation: 30
...........

Last edited by webhope; 05-25-2010 at 08:40 AM.
 
  


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
while loop in awk function - syntax problem skuz_ball Programming 2 03-10-2010 02:59 AM
[SOLVED] AWK Syntax MTK358 Programming 9 03-02-2010 11:31 AM
awk syntax wakatana Programming 10 10-20-2009 01:51 PM
awk syntax to print particular record of xyz file? johnpaulodonnell Linux - Newbie 4 06-14-2007 07:47 AM


All times are GMT -5. The time now is 10:15 AM.

Main Menu
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