Solved

Unix Script

Posted on 2014-04-23
6
183 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

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…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

734 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