LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-15-2010, 12:30 PM   #1
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Rep: Reputation: 55
Executing awk script without file name


Hi All,

Code:
#!/usr/bin/awk -f

BEGIN{ FS=":"}
{
	"groups "$1 | getline grps

	split(grps,grp," ")
	sec=gensub(".*"grp[3]" ","","1",grps)
	gsub(/ /,",",sec)

	print "RESULT_START __NAME__:"$1":USID:"$3":COMMENTS:"$5":HOME_DIR:"$6":SHELL:"$7":PRIMARY_GRP:"grp[3]":SECONDARY_GRPS:"sec":RESULT_END"
}
Above code works well when executed with
Code:
awk -f script /etc/passwd
But I need to make it work by directly putting /etc/passwd in script. It should execute with just

Code:
./script
I have tried following way..But It doesn't work
Code:
#!/bin/bash
awk
' BEGIN{ FS=":"}
{
	"groups "$1 | getline grps

	split(grps,grp," ")
	sec=gensub(".*"grp[3]" ","","1",grps)
	gsub(/ /,",",sec)

	print "RESULT_START __NAME__:"$1":USID:"$3":COMMENTS:"$5":HOME_DIR:"$6":SHELL:"$7":PRIMARY_GRP:"grp[3]":SECONDARY_GRPS:"sec":RESULT_END"
} ' /etc/passwd
Please advice..
 
Old 07-15-2010, 12:37 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
What exactly does it do?

Have you tried using redirection?--eg:
awk
awk stuff
</etc/passwd
 
Old 07-15-2010, 12:47 PM   #3
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by pixellany View Post
What exactly does it do?

Have you tried using redirection?--eg:
awk
awk stuff
</etc/passwd
Thanks for your response pixellany..

The script simply extracts the fields in /etc/passwd, Based on the usernames their respective primary and secondary groups are extracted..

I tried with your sugesstion this way

Code:
#!/bin/bash
awk
'BEGIN{ FS=":"}
{
        "groups "$1 | getline grps

        split(grps,grp," ")
        sec=gensub(".*"grp[3]" ","","1",grps)
        gsub(/ /,",",sec)

        print "RESULT_START __NAME__:"$1":USID:"$3":COMMENTS:"$5":HOME_DIR:"$6":SHELL:"$7":PRIMARY_GRP:"grp[3]":SECONDARY_GRPS:"sec":RESULT_END"
}'</etc/passwd
Still it gives help menu
Code:
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options:		GNU long options:
	-f progfile		--file=progfile
	-F fs			--field-separator=fs
	-v var=val		--assign=var=val
	-m[fr] val
	-O			--optimize
	-W compat		--compat
	-W copyleft		--
--------------------------------------
-------------------------------------
fetchuser: line 12: BEGIN{ FS=":"}
{
	"groups "$1 | getline grps

	split(grps,grp," ")
	sec=gensub(".*"grp[3]" ","","1",grps)
	gsub(/ /,",",sec)

	print "RESULT_START __NAME__:"$1":USID:"$3":COMMENTS:"$5":HOME_DIR:"$6":SHELL:"$7":PRIMARY_GRP:"grp[3]":SECONDARY_GRPS:"sec":RESULT_END"
}: No such file or directory
But /etc/passwd file exists. And the code Which I have pasted earlier, works with awk -f script /etc/passwd

Thanks again..

Last edited by vinaytp; 07-15-2010 at 12:56 PM.
 
Old 07-15-2010, 01:26 PM   #4
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 728

Rep: Reputation: 74
Hi.

Move the single quote from the line after the awk line to the awk line itself:
Code:
#!/bin/bash
awk '
BEGIN{ FS=":"}
...
Your current script is running the awk command by itself ... cheers, makyo
 
Old 07-15-2010, 01:32 PM   #5
vinaytp
Member
 
Registered: Apr 2009
Location: Bengaluru, India
Distribution: RHEL 5.4, 6.0, Ubuntu 10.04
Posts: 704

Original Poster
Rep: Reputation: 55
Quote:
Originally Posted by makyo View Post
Hi.

Move the single quote from the line after the awk line to the awk line itself:
Code:
#!/bin/bash
awk '
BEGIN{ FS=":"}
...
Your current script is running the awk command by itself ... cheers, makyo
Yes, This works..

But How this will make the difference ?

Thanks a lot..

Last edited by vinaytp; 07-15-2010 at 01:44 PM.
 
Old 07-15-2010, 08:19 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Quote:
But How this will make the difference ?
Like with any script you create, run each line of your script from the command prompt,ie
Code:
awk

#compared to

awk '
If you run the two lines above at the prompt you will see the difference
 
  


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
Script by passing a FILE without executing it...!!! NetRock Programming 5 03-21-2010 07:43 AM
Executing multiple instances of a binary file from a single script? j_65_uk Linux - Newbie 3 07-02-2009 04:56 PM
Executing a script when uploading a file to a SAMBA SERVER rafa_gallego Linux - Server 4 01-18-2008 05:39 AM
executing perl script through .forward file skverma_in Linux - Newbie 3 10-21-2004 07:24 AM
executing perl script through .forward file skverma_in Linux - General 0 10-18-2004 05:06 AM


All times are GMT -5. The time now is 07:11 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration