Copying and Renaming file through standard input


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Copying and Renaming file through standard input
# 1  
Old 07-26-2011
Copying and Renaming file through standard input

Hi Geeks,

I am relatively new to Unix. Trying out to achive a shell script by hard learning. Here is my requirment.

1. I have to search for specified strings that are given in .csv file in the directory to find the files for matching strings in the .csv file.

2. If match is found, copy that file to another directory with renaming the copied file with the matched string appended to the file name.

The below script reads the file file3.csv succesfully and finds the matching keys in the entire directory, and also copies the file to Target directory.

I am not sure how to rename the file in target directory appending the file name with matching string once the file is copied.

Code:
set -A array $(</infa/frs/dev/data/SrcFiles/New/file3.csv)
 
typeset -i i=0;
 
while (( i < ${#array[*]} ))
 
do
 
grep -R ${array[$i]} /infa/frs/dev/data/SrcFiles/New/ |cp `cut -d ":" -f 1` /infa/frs/dev/data/SrcFiles/New/Target/
 
(( i+= 1))
 
done

Any kind of suggestion/help is very much apprecited.

Regards

Last edited by pludi; 07-26-2011 at 04:32 AM..
# 2  
Old 07-26-2011
See if this works for you:
Code:
for mStr in $(</infa/frs/dev/data/SrcFiles/New/file3.csv); do
  for mFName in $(grep -R ${array[$i]} /infa/frs/dev/data/SrcFiles/New/ | cp `cut -d ":" -f 1`); do
    mNewFName='/infa/frs/dev/data/SrcFiles/New/Target/'${mFName}${mStr}
    cp ${mFName} ${mNewFName}
  done
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files to a directory, renaming it if a file with the same name already exists

Hi All, I need to copy files from one directory to another with the files to be renamed while copying if a file with the same name already exists in the target directory. THanks, Dev (2 Replies)
Discussion started by: dev.devil.1983
2 Replies

2. Shell Programming and Scripting

Copying, renaming the file ftp it from windows to Linux

Hello my dear friends, Two file are auto generated from mon - fri at different directories on same windows box.Every day i have to copy the file, rename it (specific name)and ftp it to linux box specified directory. is it possible to automate this process,If yes this has to be done from windows... (1 Reply)
Discussion started by: umesh yadav
1 Replies

3. How to Post in the The UNIX and Linux Forums

Copying , renaming the file from windox box and ftp to Linux box

Hello my dear friends, Two file are auto generated from mon - fri at different directories on same windows box.Every day i have to copy the file, rename it (specific name)and ftp it to linux box specified directory. is it possible to automate this process,If yes this has to be done from windows... (1 Reply)
Discussion started by: umesh yadav
1 Replies

4. Shell Programming and Scripting

Copying a file to multiple other files using a text file as input

Hello, I have a file called COMPLIST as follows that contains 4 digit numbers.0002 0003 0010 0013 0015 0016 0022 0023 0024 0025 0027 0030 0031 0032 0033 0035 0038 0041 (3 Replies)
Discussion started by: sph90457
3 Replies

5. Homework & Coursework Questions

Removing punctuations from file input or standard input

Just started learning Unix and received my first assignment recently. We haven't learned many commands and honestly, I'm stumped. I'd like to receive assistance/guidance/hints. 1. The problem statement, all variables and given/known data: How do I write a shell script that takes in a file or... (4 Replies)
Discussion started by: fozilla
4 Replies

6. Shell Programming and Scripting

Use the content of a file as standard input

I want to use a content of a file as standard input to a program and dump the output to a file. However, when I try the following code: ./program < input.in > output.out The output.out is empty. So, how can I handle this problem? Thanks in advance! (11 Replies)
Discussion started by: Ray Sun
11 Replies

7. Shell Programming and Scripting

Renaming and copying the file from one to another

Hi, There is file generated automatically at /usr/files as fileYYYYMM(e.g file201005 and so on). I need a script that will i)pick up the latest file from that path ii)rename the copied file to fileYYYYMM and then iii)copy to another server at path /usr/dest.If the file name with same name... (1 Reply)
Discussion started by: Alok Ranjan
1 Replies

8. Shell Programming and Scripting

Copying files and renaming

The goal is to read names of files defined ouside in upload.conf and rename them using date, time and proper extension. I have made short script while read; do cp "$RELAY" "$RELAY(date +%Y-%m-%d_%H:%M:%S)_DEPLOYED.ear" done < upload.conf but unfortunatelly it fails printiong the... (9 Replies)
Discussion started by: Michal Janusz
9 Replies

9. Shell Programming and Scripting

Need help copying and renaming files

I would like to copy files from one directory to another directory while renaming them at the same time. Does anyone have any ideas on how to do this in a script? Basically, would like to copy files from /user/data/prod/*.txt to /user/data/bck/*.txt.bck (3 Replies)
Discussion started by: thunderkiss65
3 Replies

10. Shell Programming and Scripting

opening a file given as standard input

Hi I am trying to write a shell script which should take the file as standard input. As file(content and name both) will change for each run. It should read the file line by line. with each line I have to perform certain operation. For example I have i file foo, it looks like /usr/doc/abc... (4 Replies)
Discussion started by: shashiprakash81
4 Replies
Login or Register to Ask a Question