find | gawk --- NOT WORKING

Hi every1:)

I have a problem with find and Pipe to gawk.

Find -name '*.txt' -> works fine.. Gives all the txt file under home directory and sub directories

But when I use it like this (see below).

Find -name '*.txt' | gawk -F '\t' "script" :confused:

It doesn't work.

Can you pls help?

Thanks in advance

What is the desired output?

Regards

Hi,

I have large number of txt files (some with same name) located under my home directory and subdirectories --

All files are under this directory (Administrator/home/acokayan) -- in cygwin

What I need is:

1-Find any txt files under home directory and subdirectories
2-then pipe these files to this script " gawk -F '\t' '$8 == "HP" || $8 == "IBM" || $8 == "Dell" {print $1, $4, $8 }'"

The find command doesn't have an output with tab seperated fields.
What is the output of your find command and what is the desired output?

The find command should find all the txt files .

Then use gwak script to read every txt files (output from find) and print column 1,4 8 only if column 8 has the following values "HP" OR "IBM" OR "DELL"

thanks

Please post the output of your find command and the desired ouput.

Thanks for your response.

This works fine
Gawk -F '\t' '$8 == "HP" || $8 == "IBM" || $8 == "Dell" {print $1, $4, $8 }' c:\cygwin\home\acokayan\export\/*.txt

The only problem that I have is that not all txt files are under export directory.

Some of it located under export\Pctype2007 or export\Pctype2005..and so on...

So is there anything we could do to this code "/*.txt" which could reference it to all other subdirectories...

thanks for your help

the problem is that the output of find is beeing interpreted by awk as the text to filter, not the files to open with the text inside.
you have to do something like this

find . -name '*.txt' | while read line
do
         Gawk -F '\t' '$8 == "HP" || $8 == "IBM" || $8 == "Dell" {print $1, $4, $8 }' $line
done

i try to run this code but didnt work

file name new.sh

#! /usr/bin/bash
find .-name '*.txt' | while read line
do
gawk -F '\t' '$2== "MS2" {print $1, $2}'$line
done < output_file.txt

i am getting this error
$ ./new.sh
./new.sh: line 5: syntax error near unexpected token `done':confused:
./new.sh: line 5: `done ':confused:

im not an expert, but i think if you want the text to go out, you need > instad of < (which makes teh text go in)

Also you need a space before $line

hi guys,

still gettting the same error msg.

post the current script (pls pls pls use [code] tags)
and post the error

hi broli

i am new to this page and also new to shell scripting in fact this is my first script

can u pls tell me what is a code tags?

this the code that i am running

#!/usr/bin/sh
find .-name '*.FMT' | while read line
do
gawk -F '\t' '$2== "MS2" {print $1, $2}' $line
done

and this is the error msg.

$ ./new.sh
./new.sh: line 5: syntax error near unexpected token `done'
./new.sh: line 5: `done'

thanks for your help.

code tags are like this.

[ code]
some text that will remain indented
[ /code]

(i puted some space after the [ cause i cant find a way to escape the tag itself :P)
about your script, i dont see the error....

hi broli,

do i need a search Path ?

if i do, then how should i get the search path?

"All shell scripts should include a search path specification:

PATH=/usr/ucb:/usr/bin:/bin; export PATH

A PATH specification is recommended -- often times a script will fail for some people because they have a different or incomplete search path.

The Bourne Shell does not export environment variables to children unless explicitly instructed to do so by using the export command. "

I don't think the PATH is the problem, the "done" error means something funny is in the file. You seem to be using DOS file names; are you using Cygwin? Is /usr/bin/bash the correct path to the Bash interpreter? Maybe try with /bin/sh there instead. Also try bash -vx new.sh and post the output here.

By the by, here is an equivalent script without the while loop:

find . -name '*.FMT' | xargs gawk -F '\t' '$2== "MS2" {print $1, $2}'

Note: you should have a space between "find ." and "-name '*.FMT'"

This is really weird, as I mentioned earlier, the find command doesn't have an output with tab separated fields.
I've asked several times to post the output of the find command and the desired output but I've give it up. :frowning:

Hi Era,

Thank you so much, the xargs did the job.No problems:)

I am using cygwin. Can please help on how to check the correct path to the Bash interpreter?

Here is the output of "bash -vx new.sh"

$ bash -vx new.sh
#!/usr/bin/sh
find . -name '*.FMT' | while read line
do
gawk -F '\t' '$2== "MS2" {print $1, $2}' $line
done
new.sh: line 5: syntax error near unexpected token `done'
new.sh: line 5: `done'

There maybe non-printing characters in your script. Highlight them by running your script through cat and post its output here that is...

cat -vet your_script