Operating System - HP-UX
1833467 Members
2759 Online
110052 Solutions
New Discussion

how to extract a value from a inputfile in script

 
Frank Zeng
New Member

how to extract a value from a inputfile in script

Hi,
I want to extract a value from a input file so
that I can use it to determine the iteration in
the c shell script file.

Thanks
10 REPLIES 10
boley janowski
Trusted Contributor

Re: how to extract a value from a inputfile in script

set the value in the input file as a variable, or set the script to cat the file and look for something specific and set that output as a variable:

x=`cat file | grep | awk '{print $}`

with this defined in the shell script. If this is not what your looking for, please provide and example.
James R. Ferguson
Acclaimed Contributor

Re: how to extract a value from a inputfile in script

Hi Frank:

You can use 'awk' to read a particular file, selecting record(s) and extracting fields, as for instance:

# X=`awk '$2~/localhost/ {print $1}' /etc/hosts`

# echo $X

...which would display 127.0.0.1

You can also use the 'read' command to split a line read from a file into individual fields. Take a look at 'man read'.

...JRF...
Frank Zeng
New Member

Re: how to extract a value from a inputfile in script

I tried the codes you provided, but they seemed they didn't work. I don't know if I did
something wrong. I also tried "read", it didn't work either. What I did is as following,

while (read -r xx yy)
do
printf "%s %s\n" "$yy" "$xx"
done < filein.dat


and the inputfile "filein.dat" is as following

12. 12.
11. 11.
James R. Ferguson
Acclaimed Contributor

Re: how to extract a value from a inputfile in script

Hi Frank:

Both the 'awk' and the 'read' will work in c-shell. Remove the parentheses surrounding the read argument to make it work:

...
while read -r xx yy
...

...JRF...
Frank Zeng
New Member

Re: how to extract a value from a inputfile in script

Hi, James,

I did what you said. What I got is as the following,

while: Expression syntax

Thanks
James R. Ferguson
Acclaimed Contributor

Re: how to extract a value from a inputfile in script

Hi Frank:

My apologies! I tested the syntax but bungled specifying the C-shell!

I never use the C-shell. It's not nearly as robust as the Posix (superset of ksh) that is the HP default. The syntax I used is Posix.

For the C-shell you would do:

# set X=`awk '$2~/localhost/ {print $1}' /etc/hosts`

...JRF...
Wodisch
Honored Contributor

Re: how to extract a value from a inputfile in script

Hello Frank,

guessing that your "config file" looks like:
NAME=value

you could use a line like:

result=`grep "NAME=" config-file | cut -d"=" -f2-`

that should be pretty "shell-independent" and fast
enough.

HTH,
Wodisch
Jordan Bean
Honored Contributor

Re: how to extract a value from a inputfile in script

The CSH doesn't have a built-in "read" function. Instead, it will use /usr/bin/read, a shell script which invokes the posix shell to use its builtin-in read. This is actually pretty useless since the information is lost when the script terminates. I recommend using the posix shell as much as possible.

If you want to read in a CSV file using the posix shell, try:

IFS=","
while read xx yy
do
echo $yy $xx
done <<.
12,13,
14,15,
.

I don't know of any equivalent for CSH.
Frank Zeng
New Member

Re: how to extract a value from a inputfile in script

Hello, James,

The follwing works, but I still don't how to read the value from the file "filein.dat"
could you tell me explicitly.

Thanks.

set X=`awk '$2~/localhost/{print$1}' /etc/hosts`
echo $X
Jordan Bean
Honored Contributor

Re: how to extract a value from a inputfile in script

Frank, could you tell us exactly what you are trying to do? What is the nature of the input file? Which value do you want? How will it be used? Perhaps we can engineer a efficient solution for you with more details.