Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

advanced awk

Shell Programming and Scripting


Tags
nawk

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-31-2010
wakatana wakatana is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 23 May 2013, 2:35 PM EDT
Posts: 116
Thanks: 1
Thanked 0 Times in 0 Posts
advanced awk

Hi all

Input

Code:
group1	user1
	user2
	user3
group2	user4
	user5
	user1
group3	user6
	user7
	user8

Desired output

Code:
group1
group2

So far I've tried, but did not work

Code:
BEGIN
{
  i=0;                                  # declaring variable
}

{
  if (NF==2){currgrp=$1;}               # check whether awk actually processing 2 column record, if yes save the group name
  else
     {
      if ($1~/uname/)                   # else if awk is processing 1 column record and is found user name, save the group name to array and increment i
       {
         netgroups[i]=currgrp;
         i++;
       }
     }
}

END                                     # at the end of processing whole file print the array (all saved groups)
{
  for(j=0;j<i;j++)
  {
    print netgroups[i];
  }
}

skript was called

Code:
awk -v uname="user1" -f awkscript.awk group_file

what is wrong with script above ?
Sponsored Links
    #2  
Old Unix and Linux 03-31-2010
Scott's Unix or Linux Image
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 22 April 2015, 12:29 PM EDT
Location: Zürich
Posts: 8,534
Thanks: 336
Thanked 1,065 Times in 928 Posts
It was a bit complicated, I think for the task?


Code:
$ cat Test1
awk '
 $2 { G = $1 }
 $NF == U && ! _[G]++ { print G }
' U="$1" file1

$ cat file1
group1	user1
	user2
	user3
group2	user4
	user5
	user1
group3	user6
	user7
	user8

$ ./Test1 user1
group1
group2

Sponsored Links
    #3  
Old Unix and Linux 03-31-2010
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 April 2015, 3:05 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 624 Times in 582 Posts
This one should work:

Code:
awk -v uname="user1" 'END {
  while (++j <= i) print netgroups[j]
  }
NF == 2 { currgrp = $1 }
$0 ~ uname { netgroups[++i] = currgrp }
' group_file

Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.

---------- Post updated at 07:47 PM ---------- Previous update was at 07:39 PM ----------

Quote:
Originally Posted by scottn View Post
It was a bit complicated, I think for the task?
[...]
Sure, no END block is needed Unix or Linux Image



Code:
awk  'NF == 2 { g = $1 }
$NF ~ u && $0 = g x
' u="user1" group_file

One of the previous solutions will fail if the username is 0 Unix or Linux Image

... and my code assumes unique group names.

Last edited by radoulov; 03-31-2010 at 01:52 PM..
    #4  
Old Unix and Linux 03-31-2010
wakatana wakatana is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 23 May 2013, 2:35 PM EDT
Posts: 116
Thanks: 1
Thanked 0 Times in 0 Posts
Thank you guys,
To radulov: your solution is so similar to mine, don't you know why mine is not working ? Seems more similar to me Unix or Linux Image
Sponsored Links
    #5  
Old Unix and Linux 03-31-2010
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 974 Times in 789 Posts

Code:
$ u=user1
$ sed -n '/^group/h; /'"$u"'/{g;s/[[:blank:]].*$//;p;}' file
group1
group2

I would recommend against using this type of solution unless you can guarantee that the user name will not contain any sed regular expression special characters ("/", ".", "*", "\" .. come immediately to mind).

You have been warned Unix or Linux Image

Alister

Last edited by alister; 03-31-2010 at 03:21 PM..
Sponsored Links
    #6  
Old Unix and Linux 03-31-2010
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 April 2015, 3:05 PM EDT
Location: Варна, България / Milano, Italia
Posts: 5,690
Thanks: 184
Thanked 624 Times in 582 Posts
Quote:
Originally Posted by wakatana View Post
Thank you guys,
To radulov: your solution is so similar to mine, don't you know why mine is not working ? Seems more similar to me Unix or Linux Image
Comments inline.


Code:
BEGIN {                          # do not put BEGIN on a line by itself   
  i = 0                          # no need to initialize the counter,
                                 # awk does this automatically
  }                                 
{
  if (NF == 2) {
    currgrp = $1
    if ($2 ~ uname) {
      netgroups[i] = currgrp     # you shoud check if the username matches 
      i ++                       # when NF == 2 too
      }
    }        
  else
     {
      if ($1 ~ uname) {           # /uname/ matches litteral "uname"
         netgroups[i] = currgrp
         i ++
       }
     }
}

END {                             # do not put END on a line by itself
  for(j = 0; j < i;j ++)             
  {
    print netgroups[j]            # j not i!
  }
}

Sponsored Links
    #7  
Old Unix and Linux 03-31-2010
alister alister is offline
Registered User
 
Join Date: Dec 2009
Last Activity: 11 June 2014, 8:40 PM EDT
Posts: 3,231
Thanks: 179
Thanked 974 Times in 789 Posts
Quote:
Originally Posted by radoulov View Post
Code:
BEGIN {                          # do not put BEGIN on a line by itself   
...snip...
END {                             # do not put END on a line by itself

There's nothing wrong with those two lines. BEGIN and END are syntactically no different from any other pattern. So long as the corresponding action begins on the same line (the opening curly brace is sufficient), it's fine.

Example:

Code:
$ cat beginend.awk 
BEGIN {
    print "begin"
}

END {
    print "end"
}

$ echo | awk -f beginend.awk 
begin
end

Regards,
Alister
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Advanced I/O thangappan UNIX for Advanced & Expert Users 1 04-22-2008 11:10 PM
Advanced replace fed.linuxgossip Shell Programming and Scripting 10 08-22-2007 11:49 AM
Advanced LS? bostonrobot UNIX for Dummies Questions & Answers 2 06-10-2007 03:44 PM
advanced dir listing xenophobian Shell Programming and Scripting 2 08-21-2006 10:19 AM
Advanced Getopts stefan_hery Shell Programming and Scripting 2 05-08-2005 09:16 PM



All times are GMT -4. The time now is 04:58 PM.