unexpected output within a for loop using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting unexpected output within a for loop using awk
# 1  
Old 06-24-2011
unexpected output within a for loop using awk

Hi all,
after hours of playing around with this and scouring the web I decided to ask my fellow UNIX operators as I can't wrap my head around this.

First off,
I want to parse an input file with tabs (I could pull this off easily with different delimiters) but I was trying to make nicer looking input files.
Here's my input file
Code:
# cat databases.lst
#NOTE: These entries are delimited by tabs "\t"
#oracleSID      name/pass
#
db11    harry/hill
db22    bill/peter
test    erin/ast

Now running this awk statement alone works as intended (I can manipulate fields 1 and 2
Code:
# cat databases.lst | awk 'BEGIN{min=3; RS="\n"; FS="\t"} { if (NR>min) print $1,$2 }'
db11 harry/hill
db22 bill/peter
test erin/ast

But here's my problem; I need to parse through each recordset to create paramter files for oracle (I manipulate fields 1 and 2). And to iterate through the file I put the code in a for loop which I was hoping to manipulate each row (with fields 1 and 2) but with each iteration I am getting each FIELD.

Code:
#!/usr/bin/ksh
count=0
for row in `cat databases.lst | awk 'BEGIN{min=3; RS="\n"; FS="\t"} { if (NR>min) print }'`
        do
                count=$(($count+1))
                echo $row
                echo $count
        done
# ./file22
db11
1
harry/hill
2
db22
3
bill/peter
4
test
5
erin/ast
6

Now I can use my awk statement by itself; but how do put this command in my for loop without taking each field as an interation?

If I were to change my tab delimiter to : it works fine as the entire line is taken in. Any ideas? If I have to I will use the colon; but it would've been cool to know how to manipulate tabbed entries in a file.

Thanks for everything
Erin
# 2  
Old 06-24-2011
Quote:
Originally Posted by Keepcase
First off,
I want to parse an input file with tabs (I could pull this off easily with different delimiters) but I was trying to make nicer looking input files.
Here's my input file
Code:
# cat databases.lst
#NOTE: These entries are delimited by tabs "\t"
#oracleSID      name/pass
#
db11    harry/hill
db22    bill/peter
test    erin/ast

Now running this awk statement alone works as intended (I can manipulate fields 1 and 2
Code:
# cat databases.lst | awk 'BEGIN{min=3; RS="\n"; FS="\t"} { if (NR>min) print $1,$2 }'

That's a useless use of cat.

Quote:
But here's my problem; I need to parse through each recordset to create paramter files for oracle (I manipulate fields 1 and 2). And to iterate through the file I put the code in a for loop which I was hoping to manipulate each row (with fields 1 and 2) but with each iteration I am getting each FIELD.

Code:
#!/usr/bin/ksh
count=0
for row in `cat databases.lst | awk 'BEGIN{min=3; RS="\n"; FS="\t"} { if (NR>min) print }'`
        do
                count=$(($count+1))
                echo $row
                echo $count
        done
# ./file22
db11
1
harry/hill
2
db22
3
bill/peter
4
test
5
erin/ast
6

That's a useless use of backticks, too.

How about
Code:
COUNT=0
awk 'BEGIN{min=3; RS="\n"; FS="\t"} { if (NR>min) print }' databases.lst | while read A B
do
       echo $((++COUNT))
       echo $A
done

But really, for something this simple you could just do it all in awk.
# 3  
Old 06-24-2011
Hi Corona688,

I want to thank you SO much for opening my eyes to my bad coding practices. I've made the changes and it looks good; I'm going to go back and clean up some of my previous scripts too Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print awk output in same line ,For loop

My code is something like below. #/bin/bash for i in `ps -ef | grep pmon | grep -v bash | grep -v grep | grep -v perl | grep -v asm | grep -v MGMT|awk '{print $1" "$8}'` do echo $i ORACLE_SID=`echo $line | awk '{print $2}'` USERNAME=`echo $line | awk '{print $1}'` done ============= But... (3 Replies)
Discussion started by: tapia
3 Replies

2. Shell Programming and Scripting

For loop - unexpected token `do

My requirement is to search for current date-1 .log files in /usr/sas/sas_config/Lev1/SASApp/StoredProcessServer/Logs folder and write the file name to filenames.txt When I ran the script below, I got error as syntax error near unexpected token `do I'm not sure what is wrong in my code. I... (11 Replies)
Discussion started by: Ram Kumar_BE
11 Replies

3. Shell Programming and Scripting

'(' unexpected - Error using for Loop

HI, I am facing an error while executing a script in for loop. the syntax is : for (( i=0;i<=${v_colcnt};i++ )); do Error : '(' unexpected I am new to unix and have tried multiple options such as changing the script from !# /bin/ksh to !# /bin/sh (not sure if this works to... (7 Replies)
Discussion started by: Rahul Raj
7 Replies

4. UNIX for Dummies Questions & Answers

Print each output of loop in new column using awk or shell

I have this output from a loop a11 1,2 3,4 5,6 7,8 12,8 5,4 3,6 a12 10,11 12,13 15,18 20,22 a13 ... (3 Replies)
Discussion started by: maryre89
3 Replies

5. Shell Programming and Scripting

awk: too many output files created from while loop

I am using awk to read lines from a CSV file then put data into other files. These other files are named using the value of a certain column. Column 7 is a name such as "att" or "charter" . I want to end up with file names with the value of column 7 appended to them, like this: ... (5 Replies)
Discussion started by: dodgerfan78
5 Replies

6. Shell Programming and Scripting

awk output error while loop through array

Have built this script, the output is what I needed, but NR 6 is omitted. Why? Is it an error? I am using Gawk. '{nr=$2;f = $1} END{for (i=1;i<=f;i++) if (nr != i) print i, nr }' input1.csv >output1.csvinput1.csv 1 9 3 5 4 1 7 6 8 5 10 6 output1.csv > with the missing line number 6. 6 is... (5 Replies)
Discussion started by: sdf
5 Replies

7. Shell Programming and Scripting

for loop returns more output with awk

I need to get total number of hdisk not assigned to any VGs. PDC # lspv |grep None |awk '{print $1}' |wc 131 131 1099 So, it shows 131 hdisks. I need to look at the individual hdisk fget_config info like below: PDC # fget_config -Av |grep hdisk230 hdisk230 dac1 229... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

8. Shell Programming and Scripting

Trouble with a file path as awk output in for loop

When I run the following command in the shell it works fine. It prints a city name and then a path for a file. ~$ for i in `awk -F':' '{print $0}' /home/knoppix/Desktop/data/subs | grep -m 1 $ city | sed "s/:/ /"` >do >echo $i >done Now, when I place it in this shell script (sh) it prints... (6 Replies)
Discussion started by: afroCluster
6 Replies

9. Shell Programming and Scripting

While loop error: Unexpected token done

I have tried to implement a while loop into the code but upon running the following code i am getting the errors: ./Assigntest: line 42: syntax error near unexpected token `done' ./Assigntest: line 42: `done' The code is as follows: #!/bin/bash #Filename: Assignment Author: Luke... (9 Replies)
Discussion started by: warlock129
9 Replies

10. Shell Programming and Scripting

Perl - Iterating a hash through a foreach loop - unexpected results

i've reworked some code from an earlier post, and it isn't working as expected i've simplified it to try and find the problem. i spent hours trying to figure out what is wrong, eventually thinking there was a bug in perl or a problem with my computer. but, i've tried it on 3 machines with the... (5 Replies)
Discussion started by: quantumechanix
5 Replies
Login or Register to Ask a Question