INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

awk newbie trying to use getline find for loop Tabby

awk newbie trying to use getline find for loop Tabby

awk newbie trying to use getline find for loop Tabby

(OP)
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

RE: awk newbie trying to use getline find for loop Tabby

(OP)
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

RE: awk newbie trying to use getline find for loop Tabby

(OP)
PHV, what's your name, mine is Tabitha but I go by Tabby you probably new that :)

RE: awk newbie trying to use getline find for loop Tabby

(OP)
PHV that worked perfectly, thank you SOOOOOOOOOO much!

RE: awk newbie trying to use getline find for loop Tabby

(OP)
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

RE: awk newbie trying to use getline find for loop Tabby

Just add double-quotes.

CODE

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

Annihilannic
tgmlify - code syntax highlighting for your tek-tips posts

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close