1753516 Members
5101 Online
108795 Solutions
New Discussion юеВ

Variable assignment

 
Pavitra
Occasional Advisor

Variable assignment

How can i assign 2 values to a single variable??
for eg: if i have a variable named FILE and i want it to hold 2 filetypes so that in my further comparisons, FILE can take either of the 2 values.
Can anybody suggest???
6 REPLIES 6
Dennis Handly
Acclaimed Contributor

Re: Variable assignment

You can have arrays with up to 1024 values. But you'll have to subscripts to check each value. Or use vector methods to check both at once (grep).

Of course you can always assign a string to a variable with a delimiter between them.

>i want it to hold 2 filetypes so that in my further comparisons, FILE can take either of the 2 values.

I'm not sure what you want here. Is there something in C that does what you want? You don't want enums do you?
Pavitra
Occasional Advisor

Re: Variable assignment

Dennis,

will brief u on this.
i want FILE to hold 2 filetypes.
So that say.. if i want to put all the files with these filetypes somewhere else, i can directly use the variable FILE
Pavitra
Occasional Advisor

Re: Variable assignment

And also,
Can anybody give me the syntax for multiple if`s inside awk??
Dennis Handly
Acclaimed Contributor

Re: Variable assignment

>i want FILE to hold 2 filetypes.
So that say.. if i want to put all the files with these filetypes somewhere else, i can directly use the variable FILE

Well, you can concatenate all of the files in one variable (blank separators) and then use $FILE in commands like mv, etc.
FILE=$(ls *.c)

This will have the long string: a.c ... zzz.c

>Can anybody give me the syntax for multiple if's inside awk??

Same as C:
if (abc == def) {
...
} else {
...
}
You can add if-blocks after, or inside other ifs.

See awk(1).
Dennis Handly
Acclaimed Contributor

Re: Variable assignment

Attached is a non-trivial awk program in C style.
Peter Nikitka
Honored Contributor

Re: Variable assignment

Hi,

a general handling of your request can be done by arrays.
In the following awk example we check, if the second field matches one of the values in 'str' AND when the line contains 'here-we-are'. Having this verified, we process this line later:

awk -v str='one two three' 'BEGIN {n=split(str,ary); found=0}
/here-we-are/ { for(i=1;i<=n;i++) if($2~ary[i]) {found=1;break} }
found { print "processing",$0 }'

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"