Solved

Unix Script

Posted on 2014-04-23
6
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

It is becoming increasingly popular to have a front-page slider on a web site. Nearly every TV website,  magazine or online news has one on their site, and even some e-commerce sites have one. Today you can use sliders with Joomla, WordPress or …
This article will show, step by step, how to integrate R code into a R Sweave document
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

626 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