for loop in awk script using ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting for loop in awk script using ksh
# 1  
Old 06-15-2011
Tools for loop in awk script using ksh

Guys,

I am new in awk , I face problem while i try to use for loop in awk,
I am using ksh, i am trying to set a for loop which runs as man times as the records in a file , the for loop like for(a=1;a<=5;a++) is working in my awk script but the one i need is not working Smilie
for example

Code:
awk '
{ for a in `cat file`
{ print a }
}
' ab

# 2  
Old 06-15-2011
Hi,
Please throw some light what exactly these records are. So can come up with a solution. And also your syntax for for is wrong.

Regards,
Mayur
# 3  
Old 06-15-2011
I don't understand what you are trying to do, you are mixing awk and ksh statements.
Please show us input file(s) and the required output.

Jean-Pierre.
# 4  
Old 06-15-2011
awk loops for each line in the file placing the line contents in $0 variable so what you want is

awk '
{
print $0;
}
' ab

assuming ab is your file

Last edited by SteveDawe; 06-15-2011 at 09:20 AM.. Reason: mistake
# 5  
Old 06-15-2011
Sorry i confused u guys, my question is that how i use this for loop in awk , the simple for loop is running but when i try to put it between awk it fails .
Code:
for a in `cat any_file`
do
print $a
done

# 6  
Old 06-15-2011
Why you want for loop in awk? Since awk itself processes each and every line one at a time!!!

Post the exact need with sample input and desired output.
# 7  
Old 06-15-2011
Thinks this is what you're after, each version does exactly the same as that shell snippet, but each time it removes one layer of default action that awk does for you to demonstrate how you can override different parts when you require more control.

Hope it's useful.

Code:
awk '1;' any_file
awk '{print;}' any_file
 awk '1==1{print;}' any_file
awk '{print $0;}' any_file
awk 'BEGIN {while(getline) print $0;}' any_file
awk 'BEGIN {while(getline l) print l;}' any_file
awk 'BEGIN {while(getline l<"any_file") print l;}'


Last edited by SteveDawe; 06-15-2011 at 12:12 PM.. Reason: oops, you didn't want the contents of the files
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with loop in ksh script

Hi, I am new to UNIX. I am working on a script where it takes the input and produces a desired output and it works fine for one instance. Input(One Instance): CREATE TABLE TAB1 ( COL1, COL2 ); CREATE UNIQUE INDEX XPKTAB1 ( COL1 )TAB1; Output: CREATE TABLE TAB1 ( COL1, COL2... (8 Replies)
Discussion started by: varun2327
8 Replies

2. Shell Programming and Scripting

Aix .ksh for loop script.

Hi, I'm trying to write a for loop to run through a list of servers and for each server copy a file to a backup file. But I can't seem to get it to run through my server list. It work for individual servers, please see below. #!/bin/ksh SSH_USERID=khcuser webservers="server1 server2" ... (2 Replies)
Discussion started by: elmesy
2 Replies

3. Shell Programming and Scripting

explain while loop in ksh shell script

#!/bin/ksh log=ABCl log=EFG log=HIJ i=0 while <------ what is the meaning of ($i - lt 3) do print ${log} (( i=i+1 )) done (1 Reply)
Discussion started by: Bperl1967
1 Replies

4. Shell Programming and Scripting

Setting a variable in a while loop (.ksh script)

Hello Everyone, I'm still trying to grasp many concepts in .ksh scripting, one of them being variables inside loops. My problem is the following: * I'm trying to set a variable inside a while read loop to reuse it outside of said loop. My lines are the following :... (13 Replies)
Discussion started by: jimmy75_13
13 Replies

5. Shell Programming and Scripting

KSH Script -- loop and data copy question

I am trying to write a script that will allow me to train others with commands that I run manually by only allowing the exact command before continuing onto the next set of commands. Here is where I come into an issue. I have changed the directories for this post. Software we run creates files... (2 Replies)
Discussion started by: hurtzdonut
2 Replies

6. Shell Programming and Scripting

using awk in a for loop (getting ksh variable)

Hi, I've tried searching the forums for a case similar to mine but was unsuccessful. This is my first time to use awk so any help would be really appreciated :) I have one file containing data for with the first value in each row being a State Name. I would need to create a separate file... (1 Reply)
Discussion started by: karver
1 Replies

7. Shell Programming and Scripting

Awk in ksh shell script - help

Hello, Im a beginner. Im writing a ksh script with awk. Is it possible to assign the output of the awk to a shell variable? Like, shell_variable= awk '$1 == "shell" {abc= $2 }' /tmp/cust_det echo $shell_variable Please excuse my ignorance. Thanks in advance. (4 Replies)
Discussion started by: Nic_writes
4 Replies

8. Shell Programming and Scripting

need help with simple awk/ksh script

I need help finding out why this script wont run. The chmod is okay, but i get an error saying that I need '&&' on line 5. (18 Replies)
Discussion started by: tefflox
18 Replies

9. Shell Programming and Scripting

passing variables to awk from ksh script

I'm trying to write a ksh script that uses awk, but I want to pass variables to awk. For example (not working): if ];then searchstr=$1 lsof -i | awk '{if($9~/SEARCHSTR/) print $2} SEARCHSTR=$searchstr' else echo "usage: $0 <search string>" fi I tried several options. Is it... (3 Replies)
Discussion started by: rein
3 Replies

10. Shell Programming and Scripting

script ksh/awk/grep

How can I extract the 9th line of a text file. thanks vm.:confused: (2 Replies)
Discussion started by: hoang
2 Replies
Login or Register to Ask a Question