syntax question in regards to nested awk statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting syntax question in regards to nested awk statements
# 1  
Old 06-28-2011
syntax question in regards to nested awk statements

Hello all,

I am writing up an input file and I was hoping I could get some guidance as to how to best consolidate these 2 awk statements for 1 while loop.

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

Here is statement 1 which goes through the tabbed input file
Code:
awk 'BEGIN{min=3; FS="\t";} {if (NR>min) print}' databases.lst | while read _sid _credentials
do
        # I set some variables,etc 
        # but for testing purposes
        echo "$_sid and $_credentials"      
 
done

But I also wanted the flexibility to check to see if an entry in the input file was commented out which I can use the following statement for

Code:
awk '!/^#/' databases.lst

The problem is I can't figure out how to consolidate these 2 statements into one while loop. My initial idea was to loop through the code with my first statement and within that statement I would check to see if that row has a comment in the front in my if clause. I will keep trying different methods, but I would love to hear some the input from the community. I'm still learning to use awk so if there are better ways of implementing this I would love to hear your thoughts!

Thanks!
Erin
# 2  
Old 06-28-2011
This maybe?
Code:
awk '!/^#/' databases.lst | while read _sid _credentials; do
	echo "$_sid and $_credentials"
done

NR>min is not neccessary, as the 3-lines comment are matched with !/^#/
# 3  
Old 06-28-2011
Thanks tukuyomi, I was overcomplicating things again Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nested if statements with word/number extracts

Hi everyone! I'm having difficulties setting up a complex script with nested if statements while doing some word extracts, any help is appreciated. Scenario: 1- Check if the file.txt has the word BINGO 2- If so then get the available number (any number) in the line that contains the word... (8 Replies)
Discussion started by: demmel
8 Replies

2. Shell Programming and Scripting

Nested awk Statements

Hello again everyone, yes, I'm back again for more help! So I'm attempting to read two separate files and generate some XML code from that. My current code is: BEGIN { print "<?xml version=\"1.0\" encoding=\"utf-8\">" print "<Export>" } { x=1; print "<section name=\"Query" NR "\">"... (5 Replies)
Discussion started by: Parrakarry
5 Replies

3. Shell Programming and Scripting

Nested looping statements

I cannot get the code below to work correctly. The IF statement works just fine, but not the looping. The inner loop tries to find files for a given vendor; if found, I need to sleep giving another process time to move the files. Once the given vendor's files are gone, then I want to move on to the... (1 Reply)
Discussion started by: dgreene
1 Replies

4. Shell Programming and Scripting

Nested if statements with && and ||?

Hello, I'm a shell scripting noob and new to this forum as well. My question is can nested if statements be done with && and || instead and if it can be done can someone provide an example pls. Thanks in advance for the help (1 Reply)
Discussion started by: zomgshellscript
1 Replies

5. Shell Programming and Scripting

OR operator syntax question in AWK script

Hi guys, I confused about syntax used in OR script as follow: I have this sample file separated by "|" containing: January|Month No. 1 February|Month No. 2 March|Month No. 3 April|Month No. 4 May|Month No. 5 June|Month No. 6 July|Month No. 7 August|Month No. 8 September|Month No. 9... (11 Replies)
Discussion started by: cgkmal
11 Replies

6. Shell Programming and Scripting

awk syntax question

Hi I use awk command to delete the first blanc line of a file: awk '/^$/ && !f{f=1;next}1' infile > outfile can somebody please explain me what the last "1'" in !f{f=1;next}1' stands for... Thansk a lot -A (3 Replies)
Discussion started by: aoussenko
3 Replies

7. UNIX for Dummies Questions & Answers

AWK syntax question

Hi, Have to check file names in some given directory. SO, What is the right syntax here: *$3*=="'$object_list'" - just wanted to check if $3 is in the object_list. And also, Do I need so many quotes around? (5 Replies)
Discussion started by: Leo_NN
5 Replies

8. UNIX for Dummies Questions & Answers

Nested If question

if ]; then if ]; then rm -f ${LOGFILE}.old fi mv ${LOGFILE} ${LOGFILE}.old fi Havent done nested ifs in a while. I'm reading someones code If I'm reading this correctly. It checks for the logfile, and if it exists it checks for the old logfile and if that exists, it removes the... (8 Replies)
Discussion started by: NycUnxer
8 Replies

9. Shell Programming and Scripting

yet another awk field syntax question

I am trying to print the remaing fields and field numbers beginning with a field 'xyz' #cat abc test1:test2:xyz:test3:test4:test5 #cat def test1:test2:test3:xyz:test4:test5 desired output is to be able to print NF and any trailing fields separated by':' test3 3 or test4 3 or test5... (4 Replies)
Discussion started by: prkfriryce
4 Replies

10. Shell Programming and Scripting

awk syntax question

Hi there could someone explain what is happening in the following function/statement for me, im just a little confused code = 'BEGIN{FS=","} { printf ("%-11s,%s%s%s,%07.2f,%14s,%-3s\n",$1,substr($2,9,2),substr($2,6,2),substr($ 2,3,2),$9,$10,$12) } this function is called later in the... (2 Replies)
Discussion started by: hcclnoodles
2 Replies
Login or Register to Ask a Question