Sponsored Content
Full Discussion: awk nested looping?
Top Forums Shell Programming and Scripting awk nested looping? Post 302996361 by kellyanneghj on Monday 24th of April 2017 09:48:49 PM
Old 04-24-2017
awk nested looping?

I am trying to parse a text file and send its output to another file but I am having trouble conceptualizing how I am supposed to do this in awk.

The text file has a organization like so:

Name
Date
Status
Location (city, state, zip fields)

Where each of these is on a separate line in the file and there are hundreds of such records. Note that Name does not have a standard set of fields (columns) but the other lines do.

What I want to do is search for a string in the Name line such as "Jones" and if and only if there is a match print the city for that record else go to the next record. The records, however, are not defined in any machine readable way like with some kind of delimiter.

So the program flow as I imagine it should look something like this:

Step 1. Find a line that begins with "N" (indicating a line with Name)

Step 2. Check to see if that line has the word "Jones" in it.

Step 3. If it does not, skip down to the next line that begins with "N" and repeat Step 2. Otherwise Step 4.

Step 4. If the line does have word "Jones " in it skip down two lines from the line that began with "N" to the line that begins with "L"

Step 5. Locate the City (which is in the 1st column) and write that text string to a file.

Step 6. Goto step 1.

Step 7. Repeat until end of file is reached.

So the expected output would be something along the lines of :

New York
LA
Dallas

Because all these are places with a customer called "Jones" but a city which had no customer with the name of Jones would not be on this list.

Where I am getting confused is on Step 3 and 4. Awk doesn't have a "where" operator so how do I tell awk to skip down two lines if and only if there was a match on "Jones" otherwise ignore the Location line and continue until it does have a match?

---------- Post updated at 08:48 PM ---------- Previous update was at 08:39 PM ----------

I guess another way to look at this problem would be to say:

How do I tell awk to print the City column in a line that begins with Location if and only if the immediately proceeding occurance of a line beginning with Name contained the word "Jones".
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

nested looping question

Hi, I'm having some trouble with the syntax in constructing a simple nested 'for' loop. My code is as follows: #!/bin/bash dir1="fred flume haystack" for dir2 in ${dir1} do fred="1 2 3" flume="a b c" ... (7 Replies)
Discussion started by: Sn33R
7 Replies

2. Shell Programming and Scripting

awk 2 delimiter nested

Hello All, This work could be very easy for you guys. I would really appreciate help. input file: output file: (Desired) What I am capable of doing: Command: cat inputfile | awk -F\| '{print "num="$1" value="$2" digits="$3" name1="$4" file="$5" code="$6}' > outputfile Result what I am... (5 Replies)
Discussion started by: onlyroshni
5 Replies

3. UNIX for Advanced & Expert Users

sed in awk ? or nested awk ?

Hey all, Can I put sed command inside the awk action ?? If not then can i do grep in the awk action ?? For ex: awk '$1=="174" { ppid=($2) ; sed -n '/$ppid/p' tempfind.txt ; }' tempfind.txt Assume: 174 is string. Assume: tempfind.txt is used for awk and sed both. tempfind.txt... (11 Replies)
Discussion started by: varungupta
11 Replies

4. UNIX for Dummies Questions & Answers

Help with AWK looping

I'm trying to parse a configuration text file using awk. The following is a sample from the file I'm searching. I can retrieve the formula and recipe names easily but now I want to take it one step farther. In addition to the formula name, I would like to also get the value of the attribute... (6 Replies)
Discussion started by: new2awk
6 Replies

5. Shell Programming and Scripting

looping in awk

How do I remove last comma? echo "xx yy zz" | awk 'BEGIN{FS=" "}{for (i=1; i<=NF; i++) printf "%s,", $i}'output: xx,yy,zz, required output: xx,yy,zz or (ideally!): xx, yy & zz many thanks in advance! (4 Replies)
Discussion started by: euval
4 Replies

6. 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

7. Shell Programming and Scripting

Nested case inside awk

please let me know if the below code could be written efficiently inside single awk case "$INP" in ksh) cat catalog | awk 'BEGIN {FS=",";} { print $2 } END {}' ;; pset) cat catalog | awk 'BEGIN {FS=",";} { print $3 } END {}' ;; dml) cat catalog | awk 'BEGIN {FS=",";} {... (2 Replies)
Discussion started by: cvsanthosh
2 Replies

8. Shell Programming and Scripting

Help with nested $s and quotations in bash / awk

Folks - newbie bash coder here and I'd like to get your help to make the code below work. As you can see, I was trying to count the total number of lines with the 3rd value >= 15 in a file and wanted to make the threshold "15" configurable, but apparently the $THRESHOLD value was not populated... (3 Replies)
Discussion started by: bashzipper
3 Replies

9. 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

10. Shell Programming and Scripting

awk - multiple and nested if-then-else

Hello. I would like to convert the following piece of code from bash to awk. Here are bash variables in a bash script. CUR_ROW_ID and ROW_ID_TO_SEARCH contains a string which represent a row id. The string contain a valid row id. CUR_ROW_ID sometimes may be null. CUR_VALUE... (3 Replies)
Discussion started by: jcdole
3 Replies
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 09:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy