Foreach issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Foreach issue
# 15  
Old 09-20-2011
Code:
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
# 16  
Old 09-20-2011
Quote:
Originally Posted by Corona688
Code:
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.
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)

Code:
#!/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

Last edited by unknown7; 09-20-2011 at 01:18 PM..
# 17  
Old 09-20-2011
Code:
#!/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
Code:
awk '{... system(a[k]" "b[k]) ...}'

--ahamed

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

eg:-
Code:
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
# 18  
Old 09-20-2011
Thanks for help, but still a small issue.

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

Command to call:
Code:
merge4.sh file1 file2

Quote:
merge4.sh: fileA: not found merge4.sh: fileB: not found

Code:
#!/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...

Quote:
merge4.sh file1 file2
merge4.sh: line 2: fileA: command not found
merge4.sh: line 3: fileB: command not found

Last edited by unknown7; 09-20-2011 at 01:59 PM..
# 19  
Old 09-20-2011
there should not be any space between fileA,=,$1

Code:
...
fileA=$1
fileB=$2
...

--ahamed
# 20  
Old 09-21-2011
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:
Code:
merge2.sh file1 file2

Code:
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!
# 21  
Old 09-21-2011
Not sure if this works, but did you try nesting the foreach loops. Something like:

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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ssh2 foreach loop issue

Hello Everyone, I have the following codes that only works for the first login element. I can't get it work with the second and third login credentials. Can anyone here see the problem? I ran the code with host1, it works as expect. see below: # ./test.pl host1 Trying to connect... (3 Replies)
Discussion started by: tqlam
3 Replies

2. Shell Programming and Scripting

Using foreach with two lists

Hi everybody, I'm trying to use a foreach command with two lists. The file.txt looks like this: var1: 100 200 300 var2: 3 6 9 I'm trying to use a foreach command to associate the two variables together. My script looks like this: #! /bin/tcsh set a=(`cat file.txt | grep 'var1' | cut -d... (8 Replies)
Discussion started by: SimonWhite
8 Replies

3. Shell Programming and Scripting

foreach in csh

I have a foreach loop in a csh script and I noticed that it tries to find the files with the pattern *$searchpt* in the file name. I'm confused as I never specified checking for the files. foreach f ( *$searchpt* ) set fnew = `echo $f | awk -v searchpat=$searchpt \ ... (1 Reply)
Discussion started by: kristinu
1 Replies

4. Shell Programming and Scripting

expect ssh script issue with if and foreach

Hi, I am trying to create an ssh script to login to cisco routers and activate/deactivate bgp neighbors if they match certain conditions. I dont think my "if" and "foreach" are working correctly. Any help is appreciated. Below is my script: ... (0 Replies)
Discussion started by: blahblahsomeone
0 Replies

5. UNIX for Dummies Questions & Answers

foreach question

OK, so I am extremely rusty and am just getting back to Unix after 9 years. I'm stuck on something easy. I want to search line-by-line for a string in a file, and I want to do this to a series of files in a directory. This works fine to do the search: while read i; do grep $i file2; done... (3 Replies)
Discussion started by: moldoverb
3 Replies

6. Shell Programming and Scripting

foreach loop

Hi everyone Does anyone know what is wrong with this script. i keep getting errors foreach filename (`cat testing1`) set string=$filename set depth=`echo "$string" echo $depth end the error is the following testing: line 1: syntax error near unexpected token `(' testing: line 1:... (3 Replies)
Discussion started by: ROOZ
3 Replies

7. Shell Programming and Scripting

foreach loop

Hi Guys, I have a loop which uses a wildcard i.e. foreach f (*) but when I execute the tcsh file in unix then it gives me an error ->>>>>>>foreach: words not parenthesized<<<<<<<<<<- Any help. (1 Reply)
Discussion started by: abch624
1 Replies

8. Shell Programming and Scripting

foreach folder

Hi, I'm having a small issue here and I can't get it to work. I'm programming a script for bash and I need to do something to all the folder in a directory. So I'm in the directory and I want to use the foreach statement but I dont know how to reference all the folders of that directory. To make... (7 Replies)
Discussion started by: eltinator
7 Replies

9. Shell Programming and Scripting

foreach/grep help!

#!/bin/bash foreach x (67402996 67402998) { grep -a x FINAL2006.dat >> MISSING_RECORDS.dat } I'm trying to pass a list to the variable x, and then grep for that string in FINAL2006.dat... Final2006.dat is in the same folder as my .sh file. I call this with a .cmd file... At any rate,... (6 Replies)
Discussion started by: JimWork
6 Replies
Login or Register to Ask a Question