Solved

Unix Script

Posted on 2014-04-23
6
181 Views
Last Modified: 2014-04-24
Hi,

I need a unix script which can take as input a list of invoice numbers in a text file and do a search in a particular directory and find the file containing that invoice number.

It should then copy this particular file found into a different directory. Please note I only need to copy this file to a different directory and not move it.

So if there are 100 invoice numbers in the text file then this script should be able to copy 100 files to the destination directory. I can enter the invoice numbers in the text file in any manner.

Thanks for your help.

Regards
Pradeep
0
Comment
Question by:Pradeep0308
  • 3
  • 2
6 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 40017247
Something like this:

cat $1 | while read invoiceno
do
  for ffound in `grep -l -R ${invoiceno} *`
  do
  echo cp \"${ffound}\" $2
  done;
done;

Open in new window

Put the above in a script file, call it like this:

sh script.sh files.txt /var/tmp

Where files.txt contains one invoicenumber per line and /var/tmp is your output folder. Note the echo before the copy command, the script will show you what it would to. Remove the echo to make the copy.
0
 

Author Comment

by:Pradeep0308
ID: 40017304
Hi Gerwin,

I would also like to specify the directory where the files should be searched. As I understand /var/tmp is the path where files will be copied. Where do I put the path where the files should be searched for the invoice numbers?

Regards
Pradeep
0
 
LVL 37

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 500 total points
ID: 40017341
Extra parameter:
cat $1 | while read invoiceno
do
  for ffound in `grep -l -R ${invoiceno} $2/*`
  do
  echo cp \"${ffound}\" $3
  done;
done;

Open in new window

Call like this:

sh script.sh files.txt /path/to/source /var/tmp
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 22

Expert Comment

by:earth man2
ID: 40018008
#! /bin/sh
cd $2
while read invoiceno
do
  if [ -e "$invoiceno" ]
  then
    cp $invoiceno $3
  fi
done < $1

or

#! /bin/sh
cd $2
for invoiceno in `cat $1`
do
  if [ -e "$invoiceno" ]
  then
    cp $invoiceno $3
  fi
done
0
 

Author Comment

by:Pradeep0308
ID: 40019584
Hi Gerwin,

Is it possible to keep the"echo" and just pipe the output into a file that you can use as "copy-script" afterwards. Then I will also have a chance to review before copying.

How to achieve this?

Regards
Pradeep
0
 
LVL 37

Assisted Solution

by:Gerwin Jansen, EE MVE
Gerwin Jansen, EE MVE earned 500 total points
ID: 40019609
Hello Pradeep, of course, just redirect the output of the script to a file, in general, like this:

script > outputfile

So in your case:

sh script.sh files.txt /path/to/source /var/tmp > copy_script

This way the script.sh creates an output file (copy_script) that you can use later or as a reference.

If you would run the script and collect 'error' output, you can direct to a second file, like this:

sh script.sh files.txt /path/to/source /var/tmp > copy_script 2>error_output
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
React or Angular? 6 74
Chocolatey under PowerShell is not working properly 3 61
Auto-indent certain lines in Notepad++ 10 76
ROBOFTP UNZIP 1 46
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question