Solved

Unix Script

Posted on 2014-04-23
6
182 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 38

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 38

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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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 38

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
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 look for a specific file type in a local or remote server directory using PHP.

830 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