Basic loop awk/shell script question..

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Basic loop awk/shell script question..
# 1  
Basic loop awk/shell script question..


Sorry if this is a newbie question. I guess you can use either awk or shell script for this sequence of operations, but knowing very little about either of them I'm not sure how I should try to write this.

The basic objective is to copy certain files that are scattered all over my computer into a single/new directory. I have a tab separated txt files, one of whose column contains the filename. So, I need to do the following operations:

1. Open a txt file
2. Read in the 2nd column which contains the filenames
3. Make a for loop to iterate over each filename
4. Within this loop, search my computer for the location of that particular filename
5. Copy the file into a new directory
6. End

I think I know how to get to step 2 using awk, but don't know how to iterate over all the filenames using awk. Also, is there a way that I can use grep to find the files from within awk.

thanks for any suggestions/hints!
# 2  
Is this a homework assignment?
# 3  
Hi Don,

I wish!

Okay, here's what I've go so far:

# 1. Read in input file and separate out filename
# 2. Search for it
# 3. Copy it in a directory
# 4. Create file with old and new file paths

header="From    To"
echo "${header}" >> $ofile

while read Barcode Sampleid Other
        [ "$Barcode" == "barcode" ] && continue
        names=`find /Users/pc/test -name "*${Barcode}*"`
        myname=`echo $names | cut -d' ' -f1`
        cp ${myname} ${outdir}
        oline=`echo ${from}"\t"${to}`
        echo ${oline} >> $ofile

done < ../data/worksheet.txt

echo "Finished"
exit 0

Not elegant, but works for the original purpose. The input file has 3 columns and the filename is in the column 'Barcode'. I am also trying to create a file that will keep a record of the origin and destination of the file. My questions:

1. The code lines:
        oline=`echo ${from}"\t"${to}`
        echo ${oline} >> $ofile

don't give a tab separated output. The '\t' is written into the file as it is. What am I doing wrong?

2. Any comments to improve the code -- I'm just beginning here...

# 4  
oline=`echo ${from}"\t"${to}`
echo, most likely, need the flag -e in order to interpret the backslash

oline=`echo -e ${from}"\t"${to}`

This User Gave Thanks to Aia For This Post:
# 5  
Thanks! Got it.
# 6  
If portability matters one should use printf
printf "%s\t%s\n" "$from" "$to" >> $ofile

This User Gave Thanks to MadeInGermany For This Post:
# 7  
Thanks! That is probably a cleaner way to do it.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #346
Difficulty: Easy
AWK was created at Bell Labs in the 1960s,
True or False?

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/ /root/dev/dir2/ I am executing the script from the like below and getting 'Permission denied' error. #! /bin/sh path='/root/dev' $path/dir2/ 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 #!/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 awk '{print $1}' From the prompt if I enter: ./ 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

Featured Tech Videos