Solved

Unix Script

Posted on 2014-04-23
6
179 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
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 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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
Gerwin Jansen 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this tutorial I will aim to show you how simple is making a small application in WhizBase, how to add, remove and update data in the DB. I will make a small address book application where you can add, browse, update and remove addresses. I wi…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now