Basic loop awk/shell script question..


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Basic loop awk/shell script question..
# 8  
Old 05-18-2014
Hi pc2001,
Would you please explain what you are trying to do with the following code?:
Code:
        pattern="*${Barcode}*"
        names=`find /Users/pc/test -name "*${Barcode}*"`
        myname=`echo $names | cut -d' ' -f1`

Does dataworksheet.txt contain the final component of pathnames to be copied or is it just a string or pattern that can appear anywhere in the name of a file (including a directory) found anywhere in the file hierarchy rooted in /Users/pc/test?

Given that your destination directory is a subdirectory of the root of your search, do you really want find to include files found in /Users/pc/test/newdata while it is listing matching files found in /Users/pc/test?
# 9  
Old 05-19-2014
Hi Don,

The 'Barcode' column of the worksheet contains the name of the files to be copied. These ids are unique. For example, one id could be 'A123'. The file itself may be called 'xyz_A123.jpg'. Since there could be multiple copies of the same file scattered over different directories, I use the cut command to get the filepath of the first (of these identical) files.

The find statement precedes the copy statement, so there is no worry that it will find 'xyz_A123.jpg' in the test/newdata directory. Does that answer the question? Or have I misunderstood?

Is there a better way of doing it?

thanks!
# 10  
Old 05-23-2014
Quote:
Originally Posted by pc2001
Hi Don,

The 'Barcode' column of the worksheet contains the name of the files to be copied. These ids are unique. For example, one id could be 'A123'. The file itself may be called 'xyz_A123.jpg'. Since there could be multiple copies of the same file scattered over different directories, I use the cut command to get the filepath of the first (of these identical) files.

The find statement precedes the copy statement, so there is no worry that it will find 'xyz_A123.jpg' in the test/newdata directory. Does that answer the question? Or have I misunderstood?

Is there a better way of doing it?

thanks!
I apologize for taking so long to get back to you.

In the first message in this thread, you said that a you had a file where the 2nd column on lines in that file named files to be processed. In the 3rd message in this thread, we found out that that you had file name fragments (not full filenames) in the 1st column (not the 2nd column) in that file.

We don't know what happens after a file is copied into test/newdata and we don't know how often this script is run. But if you have file name fragments like A123 and you could have filenames such as xyz_A1230.jpg, xyz_A1231.jpg, abc_A123.jpg, etc. that will match the pattern you create from the file name fragments in your input file; find will match a file under test/newdata, any of several files that have already been copied, or any one of multiple files that match the pattern from the file name fragment in addition to the file (or files) you intended to match (and the order in which the returned pathnames are presented may seem random to your script).

With the sketchy details you have provided, I have no idea whether or not this is a real problem or just a possible problem depending on the names of files that have been processed and the names of files that you may process at some point in the future.

If you run the script multiple times with the same input data and the same file hierarchy, the second time you run the script, it could easily match a file under test/newdata. Assuming that a file has already been copied in this case, I assume it wouldn't matter, but, again, I can only guess at what the side effects might be.

If you want to keep find from returning any files under test/newdata, that is fairly easy to do. Only you know enough about what you're doing to know if that is important or not. Only you know enough about the filenames being processed to know whether or not the patterns you're using to look for files will always only find duplicates of the file you want to match; or if a search could find several different files for different images from a single pattern.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 05-24-2014
Thanks for the detailed reply, Don. You are, of course right. What I didn't mention about the filenames that the number '124' (for example) is a unique 3 digit number. So there is no chance of a file '1241' or '1249' being part of a filename.

Thanks for all the replies. I managed to solve this satisfactorily.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Basic question on shell script execution

I have two shell scripts in the different directories listed below, /root/dev/dir1/test.sh /root/dev/dir2/master.sh I am executing the master.sh script from the test.sh like below and getting 'Permission denied' error. #! /bin/sh #test.sh path='/root/dev' $path/dir2/master.sh But it... (2 Replies)
Discussion started by: vel4ever
2 Replies

2. Shell Programming and Scripting

Question about a basic shell script: How to make sure it receives only one input?

Hello all! I am very new to shell and Linux in general (I just started 2 days ago), I am trying to write a script that adds the size of the directories and files in a given directory and displays messages if the user puts in something wrong. I think I have covered all the possible problems except... (3 Replies)
Discussion started by: 2358
3 Replies

3. Shell Programming and Scripting

Unix Shell basic loop massive n00b

hey guys I would really appreciate some help, i need to do a project for a job that requires minimal UNIX scripting and im REALLY stuck basically Im stuck at what i believe is something really simple but i just dont have a clue how to do it efficiently and properly and i REALLY appreciate some... (16 Replies)
Discussion started by: thurft
16 Replies

4. Shell Programming and Scripting

Very Basic Question regarding "while" loop

Hi, I have a loop like this - while read item do // fire insert query done < itemList.txt The itemList.txt has say, 1000 records. Now what I do is that rhough another program, I make the itemList.txt EMPTY, but still the INSERT query keeps firing the sequence of records. ... (18 Replies)
Discussion started by: angshuman_ag
18 Replies

5. Shell Programming and Scripting

Shell script / Grep / Awk to variable and Loop

Hi, I have a text file with data in that I wish to extract, assign to a variable and process through a loop. Kind of the process that I am after: 1: Grep the text file for the values. Currently using: cat /root/test.txt | grep TESTING= | awk -F"=" '{ a = $2 } {print a}' | sort -u ... (0 Replies)
Discussion started by: Spoonless
0 Replies

6. Shell Programming and Scripting

Basic script question

I'm trying to approach a problem but all I'm coming up with are complex ways to manipulate the data. But still not getting the desired outcome. directory of files.... file-100-foo file-100-man file-100-chu Need to copy the files and increment the number in the file name ... (4 Replies)
Discussion started by: suphawk
4 Replies

7. Shell Programming and Scripting

really basic for loop question

sorry for being dumb here, but is there a way my for loop can take an entire line of a file into consideration instead of each word in a line... ill explain if i have a file like this # cat list serial: 23124 hostname: server1 and a script that does this # cat list.sh #!/bin/sh ... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

8. UNIX for Dummies Questions & Answers

Basic awk question...getting awk to act on $1 of the command itself

I have a script problem that I am not able to solve due my very limited understanding of unix/awk. This is the contents of test.sh awk '{print $1}' From the prompt if I enter: ./test.sh Hello World I would expect to see "Hello" but all I get is a blank line. Only then if I enter "Hello... (2 Replies)
Discussion started by: JasonHamm
2 Replies

9. UNIX for Dummies Questions & Answers

A basic question of FOR loop

Hi, have a basic query. Please see the below code: list="one two three" for var in $list ; do echo $var list="nolist" Done Wht if I want to print only first/ last line in the list Eg one & three Regards er_ashu (3 Replies)
Discussion started by: er_ashu
3 Replies

10. Shell Programming and Scripting

basic shell scripting question

If I did indeed grep something out of it, why woudln't $result show nothing? When I do $? , it does show success... What is the proper syntax so that $result shows actual thing it's grepping out? result=`(ssh $host tail -1 /something/somethingelse) | egrep -i "value" >dev/null` #echo... (3 Replies)
Discussion started by: convenientstore
3 Replies
Login or Register to Ask a Question