Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk newbie trying to use getline find for loop Tabby

Status
Not open for further replies.

atjurhs

Programmer
Aug 10, 2012
18
US
Hello guys,

I've been trying to write an awk script that reads a text file that contains directory paths (I'll call it "path.txt") what's inside path.txt is a listing of directory structures something like this:

/home/Tabitha/girly1/ <--- the wanted file "redhead.txt" is in this directory
/home/Tabitha/girly1/wanted_file_not_in_here/
/home/Tabitha/girly1/wanted_file_not_in_here_either/
/home/Tabitha/girly2/ <--- the wanted file "redhead.txt" is in this directory
/home/Tabitha/girly2/wanted_file_not_in_here/
/home/Tabitha/girly2/wanted_file_not_in_here_either/
/home/Tabitha/girly3/ <--- the wanted file "redhead.txt" is in this directory
/home/Tabitha/girly3/wanted_file_not_in_here/
/home/Tabitha/girly3/wanted_file_not_in_here_either/

there's a bunch and bunches of these, way more than just three

only inside the "shortest" directory path will have the filename that I'm really after I'll say, "redhead.txt" and I want to add the "redhead.txt" to the end of the directory path that it came from so when I'm done I'll have a new_path_file.txt that will look like this

/home/Tabitha/girly1/redhead.txt
/home/Tabitha/girly2/redhead.txt
/home/Tabitha/girly3/redhead.txt

so what I've been trying so far, but I can't get it to work is

first do a find . -name "redhead.txt" on the Tabitha directory
then send that to print $0

what I'm having trouble with is read in path (been trying getline), add redhead.txt to the end (been trying cat) loop thru the bunches of files (been trying a for loop like in C)

THANK YOU SOOOOOO MUCH to whoever will help me, Tabby
 
Why do you want to use awk ?
In a shell script, you may try this:
Code:
while read d; do
  [ -f ${d}redhead.txt ] && echo ${d}redhead.txt
done <path.txt >output.txt

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV, thank you soooo much for helping me out!!!!

I'm an intern just from USC and a business major, so I really really have all most no experience with this kinda stuff and I soooo much appreciate your help!!!!

My "boss/manager" suggested that I do this with AWK or SED and he handed me an O'Riely book. So I just thought he expected me to do this in AWK or SED and so that's why I started trying it that way, but maybe not??? None of my sorority girls have ever tried progamming so I really have no where else to get help.

I'll try your "shell script" on monday would it look like this

sh script_name.bash

or do I have to put anything in front of the word "while" ? I read it it was a awk script I would need to put something like #!/bash or something like that as the first line.

sorry, I only know a very little awk and probably have that wrong

thanks again, Tabby
 
PHV, what's your name, mine is Tabitha but I go by Tabby you probably new that :)
 
PHV that worked perfectly, thank you SOOOOOOOOOO much!
 
whoooops, it almost worked,

one of the lower directories has a space in its name something like

/home/Tabitha/girly1/wanted_file not_in_here/ <------ so there's a space between "file not"

is there an easy way to fix this?

thanks sooooo much, Tabby
 
Just add double-quotes.

Code:
while read d; do
  [ -f "${d}redhead.txt" ] && echo "${d}redhead.txt"
done <path.txt >output.txt

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top