Execute command on first column (filename) retrieved from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Execute command on first column (filename) retrieved from a file
# 1  
Old 09-13-2010
Question Execute command on first column (filename) retrieved from a file

Hi,

I have a file abcd.txt which in turn contains 20 file names as column 1.
[
$cat abcd.txt
a.txt
b.txt
...
...
...
u.txt
]
Now I want to run a command "ct co -nc" / "cp" / "mv" on each of these $1 column content i.e. on each of the 20 file names.

How can do this using a script so that I need not run the same command 20 times repeatedly?

Kindly help as I am stuck badly in something due to this. Smilie

Regards,
Roy
# 2  
Old 09-13-2010
Hi.

For the first part you could use:

Code:
$ xargs -n1 -I{} < abcd.txt cleartool co -nc {}

(be careful, ct is an alias to cleartool - and won't work in a subshell)

Where exactly does the "cp" and "mv" fit into this?
This User Gave Thanks to Scott For This Post:
# 3  
Old 09-13-2010
Hi Scott,
Its working!

yes.. i also need to copy the files to another directory.. thats why cp command is also requried.. please help on that.
i.e
a.txt , b. txt etc needs to be copied at one go to another directory
# 4  
Old 09-13-2010
Code:
while read file; do cp $file /some_dir; mv $file /some_other_dir; done<abcd.txt

# 5  
Old 09-13-2010
Code:
xargs -n1 -I{} < file1 ksh -c "cleartool co -nc {}; cp {} /tmp/newdir/{}"

If the target file is also a ClearCase file, be sure that it's checked out before copying the file over it.
# 6  
Old 09-13-2010
Scott,

one tweak is required i guess.

The file abcd.txt's content are actually:

Code:
./dir1/a.txt
./dir2/b.txt
./c.txt

So now I need to copy the files to a new directory but in that case in the new target directory, only a.txt or b.txt should be created.

With your last command checkout command is working fine but copy is failing

What i used :
Code:
$srcdir> xargs -n1 -I{} < abcd.txt ksh -c "cp {} ~/targetdir/{}"
cp: cannot create /targetdir/./dir1/a.txt: No such file or directory

Please help !

Last edited by Scott; 09-13-2010 at 05:02 AM.. Reason: Please use code tags
# 7  
Old 09-13-2010
Queue hideous command:

Code:
xargs -n1 -I{} < abcd.txt ksh -c 'P=$(dirname {}); F=$(basename {}); if [ $P/$F = {} ]; then cleartool co -nc {}; mkdir -p $P; cp $F {}; else echo "No copy of {}"; fi'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using filename to determine a prefix that needs to be added to string column on file?

Possible filenames: CDD_Whatever.txt DDD_Whatever.txt If the file prefix = CDD, I'd like to prefix every person ID (second column in my examples below) on the file with "c-" If the file prefix = DDD, I'd like to prefix ever person ID with "d-" Input: Desired Output: Any help... (2 Replies)
Discussion started by: lrluis
2 Replies

2. Shell Programming and Scripting

Which shell script will call if i execute sh (without filename)?

Hi Friends, The below shell script is written by third party to create B2k_session_id.iam trying to execute this script.When i execute below script it is calling some other scripts.How to find which scripts is calling? . `execom commfunc.com` echo " $PRESENTATION_MODE " if then echo... (1 Reply)
Discussion started by: vadlamudy
1 Replies

3. UNIX for Dummies Questions & Answers

Add a new column to txt file containing filename

I would like help adding a new column to a large txt file (~10MB) that contains the filename. I have searched other posts but have not found an adequate solution. I need this extra column so I can concatenate >100 files and perform awk searches on this large file. My current txt file look... (4 Replies)
Discussion started by: kellywilliams
4 Replies

4. Shell Programming and Scripting

find specific file names and execute a command depending on file's name

Hi, As a newbie, I'm desperate ro make my shell script work. I'd like a script which checks all the files in a directory, check the file name, if the file name ends with "extracted", store it in a variable, if it has a suffix of ".roi" stores in another variable. I'm going to use these two... (3 Replies)
Discussion started by: armando110
3 Replies

5. Shell Programming and Scripting

KSH Script to Execute command from specific column in file

Hi Unix Experts,Team I have a file (say comand_file.prm), The file has a command specified in column 6 (say "./execute_script.sh"). I want to execute this command in my script. I am writing a KSH script to achieve this. Could you please assist me with this. (6 Replies)
Discussion started by: Jeevanm
6 Replies

6. Shell Programming and Scripting

Concatenate strings retrieved from a file and write it into another file

Hi, I have a file files.txt containing data as below: abc;xyz uvw;pqr 123;456 I want to develop strings like below using the above data and write them into another file: www/xxx/abc/yyy/xyz www/xxx/uvw/yyy/pqr www/xxx/123/yyy/456 All this needs to be done through .sh file. ... (4 Replies)
Discussion started by: archana.n
4 Replies

7. Shell Programming and Scripting

Merge CSV files and create a column with the filename from the original file

Hello everyone!! I am not completely new to shell script but I havent been able to find the answer to my problem and I'm sure there are some smart brains here up for the challenge :D. I have several CSV files that I need to combine into one, but I also need to know where each row came from.... (7 Replies)
Discussion started by: fransanchezoria
7 Replies

8. Shell Programming and Scripting

Take the value of variable from a file and execute a command for each value

Hello Experts, I would like to know the best way to assign a value to variable from a given file and execute a command including this variable for each entry from the file. to be more clear, i have a file with different lines (each line with a different value). i want to substitute the variable... (2 Replies)
Discussion started by: Dendany83
2 Replies

9. Programming

Assign a command to execute a file

Hi all, I want to assign a command name to a file.e.g. suppose I have a .sh file "xyz.sh". I want to execute the file by typing in "abc". The desired output is: $ abc should execute the "xyz.sh" file. Kind Regards, Qasim (4 Replies)
Discussion started by: qasim
4 Replies

10. HP-UX

How to execute a remote file with local command

Hello, I know this is somewhat strange, but please let me know if possible. I want to execute a program file in the remote machine with command on the local machine. Let me make things more clear. Suppose I have a cc on my local system and do not have that on the remote system. I want to use... (2 Replies)
Discussion started by: Veera_Raghav
2 Replies
Login or Register to Ask a Question