AWK File Traversal


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK File Traversal
# 1  
Old 01-23-2009
AWK File Traversal

Hey,

I'm working on a file traversal and extraction function and am having problems.

So, my script is finding all files in a directory. Each file has the same format. I need to extract a field from the 4th line of each file..... hence why I am using the substring on NR == 4. So, I am extracting some names and I need to print out a list of unique names..... so, I was thinking of storing the names in an array list called Names. Then, at the end of my for loop, I want to invoke another awk script to print back the names in sorted order. But, everything I have tried hasn't worked. I'm not sure if there is a scope issue.... (by redeclaring another awk block, I can't read from the variables in the 1st block).

Am I approaching this script incorrectly? As you can tell, I'm new to AWK, and am having difficulties.


Code:
ARCHIVE=/home/directory
 
        for file in `find $ARCHIVE -type f`
            do
                awk 'BEGIN { RS = "\n" } ;
                {
                        if (NR == 4)
                        {
                           Names[substr($0,14)] = substr($0,14)
                        }
                }' $file
            done

# 2  
Old 01-23-2009
Maybe this:
Code:
ARCHIVE=/home/directory
 
for file in `find $ARCHIVE -type f` | \
        awk 'BEGIN { RS = "\n" } ;
        {
                if (NR == 4)
                {
                   Names[substr($0,14)]++
                   exit
                }
        }
        END {for (i in Names) {print i} }' | sort -u

# 3  
Old 01-23-2009
Unfortunately there's no 'nextfile' in most popular awk implementations - I believe gawk has it though.
Code:
#!/bin/ksh

ARCHIVE=/home/directory
 
awk '
  FNR==4 { 
     Names[substr($0,14)]
  }
  END {
      for( i in Names)
          print i
  }
' $(find $ARCHIVE -type f) | sort


Last edited by vgersh99; 01-23-2009 at 05:40 PM.. Reason: missing 'i' in the 'END' block - my bad
# 4  
Old 01-23-2009
Hi,

I can't get either of these to work still. And I need to use bash not ksh... (Not exactly sure what the difference is)
# 5  
Old 01-23-2009
Quote:
Originally Posted by jim mcnamara
Maybe this:
Code:
ARCHIVE=/home/directory
 
for file in `find $ARCHIVE -type f` | \
        awk 'BEGIN { RS = "\n" } ;
        {
                if (NR == 4)
                {
                   Names[substr($0,14)]++
                   exit
                }
        }
        END {for (i in Names) {print i} }' | sort -u

Jim,
you're not iterating through $file - 'awk' will wait for its input.
there's no 'do/done' for the 'for' loop
# 6  
Old 01-23-2009
Quote:
Originally Posted by beefeater267
Hi,

I can't get either of these to work still. And I need to use bash not ksh... (Not exactly sure what the difference is)
Edited the original post - missing iterator in the 'END' block - sorry 'bout that.

bash/ksh - should not matter for that script.
# 7  
Old 01-23-2009
Hi...

Thanks for clarifying... however , i get an error:

./scr: line 11: syntax error near unexpected token `|'
./scr: line 11: `for file in `find $ARCHIVE -type f` | \'
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

2. Shell Programming and Scripting

file traversal

Hi, I have to get some value of the from a flat file and my file format is like this. ABC DEF HIJ=1 XYZ xyz HIJ=2 So i need to make sure i get all the value of HIJ under XYZ and not ABC. (4 Replies)
Discussion started by: nidhink
4 Replies

3. Shell Programming and Scripting

awk command to compare a file with set of files in a directory using 'awk'

Hi, I have a situation to compare one file, say file1.txt with a set of files in directory.The directory contains more than 100 files. To be more precise, the requirement is to compare the first field of file1.txt with the first field in all the files in the directory.The files in the... (10 Replies)
Discussion started by: anandek
10 Replies

4. Shell Programming and Scripting

Parse file using awk and work in awk output

hi guys, i want to parse a file using public function, the file contain raw data in the below format i want to get the output like this to load it to Oracle DB MARWA1,BSS:26,1,3,0,0,0,0,0.00,22,22,22.00 MARWA2,BSS:26,1,3,0,0,0,0,0.00,22,22,22.00 this the file raw format: Number of... (6 Replies)
Discussion started by: dagigg
6 Replies

5. UNIX for Advanced & Expert Users

need help in file traversal via FTS(3)

Hi friends, I was trying to traverse a file using FTS(3) . this is the code below.. but I am getting segmentation error in the line fts_read(...) , anybody have any idea , why it is? Please, help me. Thanks in advance.... (2 Replies)
Discussion started by: jack85
2 Replies

6. Shell Programming and Scripting

Creating breadth traversal binary tree

I almost have the entire script written. however the problem is how would i assign the global variable to terminate the process from the bottom up to ensure the child terminates so the parent can. ex. I am proccess 1 I am proccess 2 etc Here is the code $ cat tree.c ... (3 Replies)
Discussion started by: slurpeyatari
3 Replies

7. UNIX for Dummies Questions & Answers

Command line traversal

What is a key, if on command line I have to trverse left & right word by word, instead of character by character? (7 Replies)
Discussion started by: videsh77
7 Replies
Login or Register to Ask a Question