Foreach issue

Hello,

I found that this foreach should work with two lists (source: Wikipedia.org)

foreach i {1 2 3} j {a b c}  {   puts "$i $j"}

[LEFT]==
I try smth. like:[/LEFT]
With two text files:
first.part
second.part

 
foreach first (`cat first.part`) second (`cat second.part`)
toolcommand $first -toolcommand $second
end

TCSH is used.

Also script solutions are welcome, like save line x in first.part and line x in second.part to two vars and use them later...

Thanks

set valf=`cat first`
@ num=1
foreach vals ( `cat second` )
toolcommand $valf[$num] ; toolcommand $vals
@ num++
end

regards
ygemici

Thanks for the fast response.
Nearly what I wanted.

But this puts the complete file into the first var and prints always the complete first var and not just the related lines.

Used code:

#! /bin/tcsh -f
set valA=( `cat first` )
@ num=1
foreach valB ( `cat second` )
  echo "$valA""$valB"
  @ num++
end

Issue is, that you assigned the complete "cat FileONE" to your var outside of the loop.
This is why i wanted to use these foreach with two lists. But other working solutions are also welcome.

Thanks

awk 'NR==FNR{a[++i]=$0;next}{b[++j]=$0}END{for(k=1;k<=j;k++){print a[k]b[k] }}' file1 file2

--ahamed

---------- Post updated at 07:17 AM ---------- Previous update was at 07:17 AM ----------

paste will create a space

paste  file1 file2

--ahamed

Thanks,
can you show me how i can use now this in a script with usage of the vars? (nawk BEGIN usage?)

I want to use each element for my commands.

so for example like:

 
echo This is first line from file1 "varA[$1]" and this is the corresponding line from file2 "varB[$2]"

In your example I want to use in my script these vars: a[k]b[k]
Just don't know how to access them in the script (how to run your awk command there)

---------- Post updated 20th Sep 2011 at 02:44 PM ---------- Previous update was 19th Sep 2011 at 04:32 PM ----------

Still not able to get the wanted result... hanging still on the print issue...

With paste and without does not print out what I want...
with paste I do not see any other printouts as the files

#!/bin/sh
awk 'NR==FNR\
{
  a[++i]=$0;next
}
{
  b[++j]=$0;
}
END\
{
  for(k=1;k<=j;k++)
    {
      print COMMAND
      print a[k]
      print ("COMMAND_OPTION")
      print b[k]
    }
}' | paste file1 file2

^^ i see no COMMAND and neither COMMAND_OPTION ^^
Tried with one print and multiple
Tried with one printf and usage of both %s
with double quotes and single quotes - with brakets and w/o...
don't know why I do not see my wanted printout.

i want to get:

Thanks

---------- Post updated at 03:02 PM ---------- Previous update was at 02:44 PM ----------

#!/bin/sh
awk 'NR==FNR\
{
  a[++i]=$0;next
}
{
  b[++j]=$0;
}
END\
{
  for(k=1;k<=j;k++)
    {
      print "COMMAND", a[k], "COMMANDOPTION", b[k]
    }
}' file1 file2

please help... cant get the wanted output:

---------- Post updated at 03:07 PM ---------- Previous update was at 03:02 PM ----------

^^ The print a[k]b[k] } does not work

a[k] is EMPTY
b[k] contains the information from both files!

#!/bin/sh
awk 'NR==FNR\
{
  a[++i]=$0;next
}
{
  b[++j]=$0;
}
END\
{
  for(k=1;k<=j;k++)
    {      
       print "COMMAND " a[k] "COMMAND_OPTION " b[k]    
    }
}' file1 file2

--ahamed

^^ I still tried this... output is:

any hints? :-/

Works for me :-/

[root@D152153 tmp]# awk 'NR==FNR\
{
  a[++i]=$0;next
}
{
  b[++j]=$0;
}
END\
{
  for(k=1;k<=j;k++)
    {
       print "COMMAND " a[k] " COMMAND_OPTION " b[k]
    }
}' f1 f2

COMMAND A COMMAND_OPTION 1
COMMAND B COMMAND_OPTION 2
COMMAND C COMMAND_OPTION 3

Try running it in command line and see. Which OS?

--ahamed

1 Like

Damn... I spent hours for this and it's based on OS? :-/

SUN OS 5.8

command line also does not help.

TCSH and BASH:

awk 'NR==FNR{a[++i]=$0;next}{ b[++j]=$0;}END{ for(k=1;k<=j;k++){print"COMMAND " a[k] "COMMAND_OPTION " b[k] }}' file1 file2
COMMAND COMMAND_OPTION line1_form_file1
COMMAND COMMAND_OPTION line2_form_file1
COMMAND COMMAND_OPTION line3_form_file1
COMMAND COMMAND_OPTION line1_from_file2
COMMAND COMMAND_OPTION line2_from_file2
COMMAND COMMAND_OPTION line3_from_file2

---------- Post updated at 03:55 PM ---------- Previous update was at 03:41 PM ----------

Does any1 have an idea? (I'm lost)
Thanks

---------- Post updated at 04:08 PM ---------- Previous update was at 03:55 PM ----------

Testet now under: Linux 2.6.9
=> and it WORKS!

Omg... does anyone know why this does NOT work as expected (exactly same files and same bash used, but other output) for Sun OS?

[LEFT]What a waste of time :frowning:
Thanks for this important hint... else i would have searched myself dead for print solutions...[/LEFT]

Use nawk or /usr/xpg4/bin/awk on Solaris.
1 Like

Can you please explain me if/why AWK on SunOS is different to the AWK on linux.
Is it just outdated on SunOS? Would be nice to understand what the issue is.
Strange is, that AWK also worked but gave another output.

THANKS, nawk works on SunOS and provides exactly the output which Linux shows.

---------- Post updated at 04:20 PM ---------- Previous update was at 04:18 PM ----------

How can i fiure out the AWK version for SunOS? I get no response on SunOS.

If in Solaris, blindly use "nawk".

--ahamed

How can i run "normal" commands in an awk script?

#!/bin/sh
nawk 'NR==FNR\
{
  a[++i]=$0;next
}
{
  b[++j]=$0;
}
END\
{
  for(k=1;k<=j;k++)
    {
       print "\n======== " a[k] " ========"   
       ls -la
    }
}' file1 file2
system("ls -la");

You can't directly capture their output like backticks though, as far as I know. You might be able to redirect into a file then read the file after.

1 Like
echo "test" | awk '{ cmd="date"; cmd|getline; print }'
Tue Sep 20 08:27:11 IST 2011

But I don't think "ls -la" will give you want you want. It will print only the first line which in most cases is the total count!

--ahamed

---------- Post updated at 08:29 AM ---------- Previous update was at 08:28 AM ----------

Yes, system() is another option in awk.

--ahamed

Yeah processing the output with cat was my "workaround".
Thanks for the additional possibilitys!

====

how to use system in my case? I mean with array element inside.

system("command a[k] -command_option b[k]");

tried single and double quotes and combination of "'xxx'" but prob. the system is then not correct working. Do I need to work with $ now?
Thanks

====

One more question, how can i use these arguments used while skript is called.
I tried with ARGV but this didn't work (finally got no error but also no output)

#!/bin/sh
nawk 'BEGIN\
{
 fileA = ARGV[1]
 fileB = ARGV[2]
}\
NR==FNR\
{
  a[++i]=$0;next
}
{
  b[++j]=$0;
}
END\
{
  for(k=1;k<=j;k++)
    { 
       print a[k] b[k] 
    }
}' $fileA $fileB

Please look at read code.
The bgein and at the end the variable usage is not clear/working.

tried also quotes and w/o $, but didn't provide the wanted solution.
As you probably understood, i try to start the script with 2 arguments => the two files which should be used.

Thanks

#!/bin/sh

fileA=$1
fileB=$2

awk '{...}' $fileA $fileB

Call the script as ./myscript file1 fil2

--ahamed

---------- Post updated at 09:24 AM ---------- Previous update was at 09:22 AM ----------

And regarding the system call

awk '{... system(a[k]" "b[k]) ...}'

--ahamed

---------- Post updated at 09:26 AM ---------- Previous update was at 09:24 AM ----------

eg:-

root@bt:/tmp/test# echo 123 | awk '{a="echo"; b="123"; system(a" "b" >/tmp/c")}'

root@bt:/tmp/test# cat /tmp/c
123

--ahamed

Thanks for help, but still a small issue.

Please have look. It says files not found (files are there!)

Command to call:

merge4.sh file1 file2
merge4.sh: fileA: not found
merge4.sh: fileB: not found
#!/bin/sh
fileA = $1
fileB = $2
nawk 'NR==FNR\
{
  a[++i]=$0;next
}
{
  b[++j]=$0;
}
END\
{
  for(k=1;k<=j;k++)
    {
       print "\n======== " a[k] " ========"   
    }
}' $fileA $fileB

also does not work: "$fileA" "$fileB"

=====

tried under linux...

there should not be any space between fileA,=,$1

...
fileA=$1
fileB=$2
...

--ahamed

Thanks,
I removed the spaces but I get no response from the script.

rights correfct - folder okay - file1 and file2 is in the folder and contain valid information.
Call:

merge2.sh file1 file2

Code:

#!/bin/sh
nawk 'BEGIN\
{
  fileA=$1
  fileB=$2
}\
NR==FNR\
{
  a[++i]=$0;next
}
{
  b[++j]=$0;
}
END\
{
  for(k=1;k<=j;k++)
    {
       print "\n======== " a[k] " ========"   
    }
}' $fileA $fileB

====

Final script works - but the part to overgive the two filenames does not work.

Big thanks so far!