revision of copy script to allow for single quote


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting revision of copy script to allow for single quote
# 1  
Old 09-28-2012
revision of copy script to allow for single quote

I have the following script that I use to copy a list of files from one dir to another,
Code:
#!/usr/bin/bash
# $1=filename of file with the list of files to copy
# $2=column header for col in list file with filenames (filePath in most cases)
# $3=src dir
# $3=destination dir

FILE_LIST="$1"
FILE_FIELD="$2"
SOURCE_DIR="$3"
DEST_DIR="$4"

if [ $# -ne 4 ]
then
   echo "usage: $0 file_list field src_dir dest_dir" >&2
   exit 1
fi
if [ ! -f "$FILE_LIST" ]
then
    echo "Invalid filelist $FILE_LIST">&2
    exit 2
fi
# create destination directory if it doesnt' exist
if [ ! -d "$DEST_DIR" ]
then
    mkdir $DEST_DIR
fi
if [ ! -d "$SOURCE_DIR" ]
then
    echo "Invalid source directory">&2
    exit 4
fi
 
awk -vS="$SOURCE_DIR" -vFLD="$FILE_FIELD" 'NR==1{for(i=1;i<=NF;i++)if($i==FLD)w=i;next}
w {print S "/" $w}' $FILE_LIST | xargs  -r cp --target-directory="$DEST_DIR"

This reads the list of file names from a column in a text file and then copies those files from the src dir to the target dir.

I have run into a problem as some of my file names contain single quotes and I am getting an xargs error,

"xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option"

I tried adding -0, but I got an error that the argument line was too long. I don't know xargs well enough to know exactly which arguments I need, or if I need to use a different approach to the script. The filenames will never have spaces or double quotes, but may have . , ' - _ ~ ( ) [ ] { } < >

I appreciate the assistance as there are often a lot of files and no good solution in the absence of a script.

LMHmedchem
# 2  
Old 09-28-2012
try this:
Code:
awk -vS="$SOURCE_DIR" -vFLD="$FILE_FIELD" -v qq='"' 'NR==1{for(i=1;i<=NF;i++)if($i==FLD)w=i;next} w {print qq S "/" $w qq}' $FILE_LIST | xargs  -r cp --target-directory="$DEST_DIR"

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 09-28-2012
Thanks, I will give that a try. I made an error in my post in indicating that filenames may have<>, since those are not legal windows characters.

---------- Post updated at 01:30 PM ---------- Previous update was at 01:18 PM ----------

Also, the file names are not all bracked with single quotes, but just may contain a single quote here and there,

Such as,

N-ethyl-N'-ethylpropane-1,3-diamide.mol

LMHmedchem
# 4  
Old 09-28-2012
Quote:
Originally Posted by LMHmedchem
Code:
xargs  -r cp --target-directory="$DEST_DIR"

Instead of xargs I would use pax -rw "$DEST_DIR". pax is part of nearly every unix flavor out there. Linux is the only notable exception, where its presence isn't guaranteed. If your system has it, it should do nicely.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 09-28-2012
This is winXP running under cygwin. I did which pax and don't find anything. I will look and see if it is in the package manager.

LMHmedchem
# 6  
Old 09-28-2012
Did you try vgersh99's suggestion? It should work if, as you said, there are no double quotes.

Another alternative would be to insert a backslash before every quote.

Regards,
Alister
# 7  
Old 09-28-2012
Quote:
Originally Posted by alister
Did you try vgersh99's suggestion? It should work if, as you said, there are no double quotes.

Another alternative would be to insert a backslash before every quote.

Regards,
Alister
I am in the middle of revising one of the lists. When I am done with that, I will test the modifications suggested by vgersh99. This is the sort of operation where OSs seem to fall very short in terms of providing reasonable tools. I don't know what I would do without scripts. Just try opening a folder in windows when it has 500,000 files in it, let alone trying to pick out a few thousand that you need. Smilie

---------- Post updated at 04:07 PM ---------- Previous update was at 02:44 PM ----------

I tried the suggestion of vgersh99 and it worked fine. I think I need to add stripping out of double quotes if they occur since excel tends to put those in when you save to a text file.

It would also be nice to have a similar script that would rm the file names on a list from a specified directory. I guess it makes sense to stick to the linux tool mentality of one tool per operations instead of trying to make a script that does two different things based on arguments.

LMHmedchem
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Single quote _error_.

Hi all... (This is Don's domain.) I have come across an anomaly in sh and dash compared to bash. It involves echoing a character set to a file in sh and dash compared to bash. It is probably easier to show the code and results first. #!/usr/local/bin/dash #!/bin/sh #!/bin/bash echo... (4 Replies)
Discussion started by: wisecracker
4 Replies

2. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

3. Shell Programming and Scripting

Passing parameter with single quote to shell script

Hello All, I am trying below +++++++++ #/bin/bash set -x Host=$1 Port=$2 User=$3 Pass=$4 Warning=$5 Critical=$6 SCRIPT_LOC=/opt/agent/ Parameters="'""hostname=$Host&""port=$Port&""username=$User&""password=$Pass&""jvm=UsedMemory,$Warning,$Critical""'" echo $Parameters... (10 Replies)
Discussion started by: sundari127
10 Replies

4. Shell Programming and Scripting

How to match character with single quote?

I need to check whether first character of variable is single quote. I tried the below constructions but they are all not working (always return true) if (test `echo "$REGEXP" |cut -c1` != "'"); then echo "TRUE"; fi if (test `echo "$REGEXP" |cut -c1` != '\''); then echo "TRUE"; fi if (test... (5 Replies)
Discussion started by: urello
5 Replies

5. Shell Programming and Scripting

single quote replacement

hi all, i have a data in the file which of the formate : 100,102,103 and the required formate is \'100\',\'102\',\'103 Idealy we need to replace , with \',\' Regards arkesh (2 Replies)
Discussion started by: arkeshtk
2 Replies

6. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

7. Shell Programming and Scripting

Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi, I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file: Name = "abc" The regex I'm using to match the same is: egrep -l '(^) *= *" ** *"$' /PATH_TO_SEARCH... (6 Replies)
Discussion started by: NanJ
6 Replies

8. Shell Programming and Scripting

escaping single quote

hi, echo 'abc' will give output abc how can i get output as 'abc' plz help. thanks in advance (3 Replies)
Discussion started by: javeed7
3 Replies

9. Shell Programming and Scripting

single quote

Hi I have a shell script with many lines as below: comment on column dcases.proj_seq_num is dcases_1sq; .... .... I want the above script to be as below: comment on column dcases.proj_seq_num is 'dcases_1sq'; I want to have single quotes like that as above for the entire shell... (2 Replies)
Discussion started by: dreams5617
2 Replies

10. Shell Programming and Scripting

Replacing a single quote

Hi there I have a data file like so below 'A/1';'T100002';'T100002';'';'01/05/2004';'31/05/2004';'01/06/2004';'08/06/2004';'1.36';'16';'0.22';'0';'0';'1.58';'0';'0';'0';'0';'0';'0';'clientes\resumen\200405\resumen_T100002_T100002_1.pdf';'';'0001';'S';'20040501';'';'02';'0';'S';'N'... (3 Replies)
Discussion started by: rjsha1
3 Replies
Login or Register to Ask a Question