[ask]awk in csh to extract content from file

Please suggest a method (in c shell or any other shell) to implement following:

-To read file1.txt (sample file1 given below)
-To save name field in a variable <name>
-To save parameter field in a variable <parameter>
for ex. let a line in file1.txt be :

bill height weight

the extracted results can be checked as:
echo "$bill" # prints - bill
echo "$height" #prints - height
echo "$weight" #prints - weight

sample file1.txt

ani year month
sam time day
bill height weight
.
. 

The idea is to enter the data in file1 as and when requiring and the script automatically extracting even the new line.
I guess it can be possible using "awk", but need HELP!

---------- Post updated at 02:44 PM ---------- Previous update was at 01:38 PM ----------

--- added later-------
or is it possible to do what i have asked in previous post?
please advice :frowning:

 
awk '{ a=$1;b=$2;c=$3 } END { print a , b , c}' input_file

Which ensure you wil get only the "last" line fields which i believe is the last added line.

@panyam, thanks for the answer, but its not exactly what i am looking for.

what i want from all of it is to write a script to extract one element of each filed at a time and then save them in a variable of name same as the element extracted.

It is confusing, please check the example in my first post.

Thanks

bash-3.00$ cat test
abc efg
hij
klm
 
bash-3.00$ awk '{for(i=1;i<=NF;i++)printf("%s=%s\n",$i,$i)}' test > test.var && . test.var
 
bash-3.00$ echo $abc
abc
bash-3.00$ echo $efg
efg
bash-3.00$ echo $hij
hij
bash-3.00$ echo $klm
klm

You can try this
set $bill=`cat file1.txt | tr " " "" | cut -f1 -d`
set $height=`cat file1.txt | tr " " "" | cut -f2 -d`
set $weight=`cat file1.txt | tr " " "" | cut -f3 -d`

Something like this:

 
TestBox>for word in `cat input_file`
> do
> eval $word="$word"
> export $word
> done
 
echo $ani
ani
 
echo $bill
bill

@arup
Your answer will work for a specific entries in file1, however i am expecting a generic method.
But still, thanks for your effort :):wink:

---------- Post updated 07-28-11 at 09:35 AM ---------- Previous update was 07-27-11 at 04:13 PM ----------

@itkamaraj
Thanks, your code works. I am trying to understand the logic behind your code.

can you explain the following points:

awk '{for(i=1;i<=NF;i++) printf("%s=%s\n",$i,$i)}' test > test.var && . test.var

Please explain what the test.var && . test.var signify?

test --> inputfile

> test.var ---> redirecting the output to another file ( test.var )

&& --> executes once the previous command is success
Bash Reference Manual

 
eg: 
echo "hello" && echo "echo hello is executed successfully" || echo "echo hello failed"
echho "hello" && echo "echo hello is executed successfully" || echo "echo hello failed"

. --> means dot operator or source operator source or dot operator MAN Page

1 Like

@itkamaraj
thanks for reply

I was actually confused with the last part (in bold)

test.var && . test.var

Is it a typing mistake? coz without the bold part my code is executed successfully.

also..

echo $ani

if i write the above echo after the awk statement, i get an "undefined variable" error. :frowning:

seach for "source or dot operator"

Its not typing mistake.

and read about the && and || operator

1 Like

Thanks, I had never come across dot operator before.
It is available in c shell?

---------- Post updated at 12:45 PM ---------- Previous update was at 11:47 AM ----------

@itkamaraj
It seems dot operator is not available in csh or tcsh :frowning: . My work is on either of both shell. can you suggest some csh/tcsh equivalent.

the problem i am facing is..i cant generate and use the variables as i wish to.

My objective is explained below:

Consider I have a simple text file as below
file1

s1 a1
s2 a2 b2
s3 a3 b3 c3

The file's content (with fixed format as above) can be added by any user.
I need to write a script to automatically generate and use following variables
s1,a1,s2,a2,b2,s3,a3,b3,c4,...

The variables list will depend on entries of file1.

What is mean by dot operator not available ?
Did u go through the source command ?
Did u read the links which i provided ?

1 Like

let me show here what i am doing:

this is the code i am running

filename: nscript

#
echo "-------------------------------"

awk '{for(i=1;i<=NF;i++)printf("%s=%s\n",$i,$i)}' script_name.txt  > script_name.var && . script_name.var
###########################################################

echo "a1 is ---> $a1"
#a1 = $2
echo $a2

###########################################################

in command prompt when i type

$ bash ./nscript

i get following output

-------------------------------
a1 is ---> a1
a2

but when i type

$ csh ./nscript

I get following output

-------------------------------
/bin/.: Permission denied.
a1: Undefined variable.

since i am new to unix, am not able to understand why I am getting such error.
I googled and came across a pdf telling dot operator is not supported in csh/tcsh.
The problem, i think is that we have a code which is interpreted ok with bash, but not in csh.

---------- Post updated at 01:46 PM ---------- Previous update was at 01:37 PM ----------

also if i run the nscript with following changes

awk '{for(i=1;i<=NF;i++)printf("%s=%s\n",$i,$i)}' script_name.txt  > script_name.var && source script_name.var

i get the following results

-------------------------------
script_B_1=script_B_1: Command not found.
a1=a1: Command not found.
script_B_2=script_B_2: Command not found.
a2=a2: Command not found.
b2=b2: Command not found.
script_B_3=script_B_3: Command not found.
a3=a3: Command not found.
b3=b3: Command not found.
c3=c3: Command not found.
a1: Undefined variable.

---------- Post updated at 01:55 PM ---------- Previous update was at 01:46 PM ----------

hey.. its working, with following modifications :p:p:p

awk '{for(i=1;i<=NF;i++)printf("set %s=%s\n",$i,$i)}' script_name.txt  > script_name.var && source script_name.var