Awk replacing file with user input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk replacing file with user input
# 1  
Old 05-20-2012
Awk replacing file with user input

this section of the awk code i have here takes file to work with from the user.

the user specifies the file name from the command line and the file name is assigned to the variable $FLIST

Code:
awk 'BEGIN {
  while((getline < "'${FLIST}'")>0)
     S[$0]

  FS="\n"; RS="}\n"
}

now, i dont want to specify a file anymore. i want the user to be able to specify a value from the command line. but i cant seem to replace FLIST with anything that isn't a file.

how can i modify this so i can specify a value instead of a file?
# 2  
Old 05-20-2012
Perhaps something like this :
Code:
BEGIN {
printf "Input a file :\n"
getline file < "-"
if (system("test -f "file) == 1 ) { # or whatever test you want.
	print "file not found"
	}
else
	{
	print "file found"	
		while((getline < file )>0)
     		S[$0]
	}
for ( i in S )
	print i
}

# 3  
Old 05-20-2012
Quote:
Originally Posted by Peasant
Perhaps something like this :
Code:
BEGIN {
printf "Input a file :\n"
getline file < "-"
if (system("test -f "file) == 1 ) { # or whatever test you want.
	print "file not found"
	}
else
	{
	print "file found"	
		while((getline < file )>0)
     		S[$0]
	}
for ( i in S )
	print i
}

im trying to get away from using the file.

the program runs like this:

Code:
code.sh filename datafile

filename contains a list of host.

but what if i already have a specific host i want to check against the datafile. i dont want to specify a file containing a list of other host names for that.

so, i want to be able to specify the specific host i want from the command like. something like this:

Code:
code.sh hostname datafile

but the code i posted only seems to like files. it doesn't respond to variables.
# 4  
Old 05-20-2012
Using your existing BEGIN section as a starting point, you could do something like this:

Code:
awk  -v host_name="${1:-no-host}" '
   BEGIN {
      S[host_name];
      FS="\n"; RS="}\n"
   }



This will then work with the rest of your code that already uses the S hash to identify which host(s) to dig from the data file. If the user doesn't give command line parms it defaults to some (assumed) unreal value (no-host).
This User Gave Thanks to agama For This Post:
# 5  
Old 05-20-2012
Code:
awk '
    NR == FNR { seen[$1]; next }   # collect hosts from the conf file
    /define host.*{/ { snarf = 1; next; }    # ok to capture data after this point
    !snarf { next; }              # skip record if not capturing
    /}/ {                        # end of chunk, if name was in conf list, print data
        if( name in seen )
            printf( "%20s -------- %s -------- %s\n", name, ip, ip2 );   # now prints both
        name = ip = ip2 = "";    # reset everything;
        snarf = 0;             # end of chunk turn capture off
    }
    /_secondary_address/ { ip2 = $2; next}       # captures secondary too
    /host_name/ { name = $2; next; }
    /address/ { ip = $2; next; }
' $FLIST $LFILE

how can i modify a code like the one above to do the same? instead of looking at the list of host names in FLIST, i want to be able to specify a host from command line.
# 6  
Old 05-20-2012
Try this:
Code:
awk -v host_name="${1:-no-host}" '
    /define host.*{/ { snarf = 1; next; }    # ok to capture data after this point

    !snarf { next; }              # skip record if not capturing

    /}/ {                        # end of chunk, if name was in conf list, print data
        if( name == host_name )
            printf( "%20s -------- %s -------- %s\n", name, ip, ip2 );   # now prints both
        name = ip = ip2 = "";    # reset everything;
        snarf = 0;             # end of chunk turn capture off
    }

    /_secondary_address/ { ip2 = $2; next}       # captures secondary too

    /host_name/ { name = $2; next; }

    /address/ { ip = $2; next; }

' $LFILE

This User Gave Thanks to agama For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to search based on 5 user input fields

Field1=”” Field2=”” Field3=”” Field4=”” Field5=”” USER INPUT UP TO 5 FIELDS awk -F , '{ if ( $3 == Field1 && $6 == Field2 && $8 == Field3 && $9 == Field4 && $10 == Field5) print $0 }' /tmp/rodney.outD INPUT FILE (Rodney.outD): ... (3 Replies)
Discussion started by: rmerrird
3 Replies

2. Programming

Keyboard User Input in awk language ?

Hi, does someone know how to make a keyboard data input in the AWK programming language ? Regards Zabo (6 Replies)
Discussion started by: Zabo
6 Replies

3. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 Replies

4. Shell Programming and Scripting

How to take input from user or awk script?

Hi Jim, I have following script,i which i need to take dynamic value . script, nawk -v v1=grep"INT_EUR" $propertifilename | cut -d"=" -F2` -F'~' '{if (NF-1 !=v1) {print "Error in " $0 " at line number "NR" tilde count " N-1}}' $filename In the above script i want to use INT_EUR as a variable... (2 Replies)
Discussion started by: Ganesh Khandare
2 Replies

5. Homework & Coursework Questions

[Scripting]Find & replace using user input then replacing text after

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: (o) Checkout an auto part: should prompt the user for the name of the auto part and borrower's name: Name:... (2 Replies)
Discussion started by: SlapnutsGT
2 Replies

6. UNIX for Dummies Questions & Answers

Replacing part of a text file with user input.

Ok, I am brand new to UNIX and I am trying to learn a cross between basic script and database use. I had got some ideas off the net on simple ideas for learning UNIX. I am working on creating a simple phone book program that allows myself to enter our employees from work into a phone book text... (0 Replies)
Discussion started by: georgefurbee
0 Replies

7. Shell Programming and Scripting

Replacing data of output file with input

Hi, I have a ksh which peocess and get me data from 3 days... ie if i process it on jan 28.. it gets data for 25, 26 and 27.... the process run every day and get previous 3 days data...all this data is appened to a file lets call time.out Now time.out cannot have deplicate data so what i want... (10 Replies)
Discussion started by: bhagya2340
10 Replies

8. Shell Programming and Scripting

replacing spaces with null or 0 in the input file

hi i have records in my input file like this aaa|1234||2bc||rahul|tamilnadu bba|2234||b4c||bajaj|tamilnadu what i am expecting is in between two pipes if there is no character it should be replaced with null or 0 so my file will look like this aaa|1234|null|2bc|0|rahul|tamilnadu... (4 Replies)
Discussion started by: trichyselva
4 Replies

9. Shell Programming and Scripting

AWK set FILENAME via user input

I am trying to write a awk script that prompts user for input to set the FILENAME varable. I can get it set, but I think awk is not doing anything with it. this is what I have so far #!/usr/bin/nawk -f BEGIN { FILENAME = "" printf "Enter name of file to check in : " ... (2 Replies)
Discussion started by: timj123
2 Replies

10. Shell Programming and Scripting

awk user input

Using the following I'm trying to print the user's response to the prompt Y / N but I get nothing other than the contents of $1? awk '{ printf($1 " ? (Y/N)") getline myresponse < "-" system("read myresponse") if (myresponse == "Y") { print $1... (17 Replies)
Discussion started by: gefa
17 Replies
Login or Register to Ask a Question