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

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?

Perhaps something like this :

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.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.sh hostname datafile

but the code i posted only seems to like files. it doesn't respond to variables.

Using your existing BEGIN section as a starting point, you could do something like this:

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).

1 Like
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.

Try this:


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

1 Like