1846577 Members
1101 Online
110256 Solutions
New Discussion

Re: Scripting Question

 
James C. Hilley
Occasional Contributor

Scripting Question

I am trying to add a datestamp to some files on a system. The problem I am encountering is I am picking up the files from a network server. The files are coming over with whitespace in the name. I have tried several different they all start with the following characters CR and end with .txt the whitespace comes in between the start and end of the file name. Can anyone tell me how to tell ksh to ignore the white space? Thank you.
Life is what you make it.
7 REPLIES 7
Shannon Petry
Honored Contributor

Re: Scripting Question

It is not quite as easy as you think (someone else may have a better cheater than this) but here is a way to handle this problem.

You will have to use sed, or awk to grab the whitespaces and replace with "\ " (backslash/space) so that the shell will ignore the space.

Then you can rename them with the datestamp.

Regards,
Shannon
Microsoft. When do you want a virus today?
Tracey
Trusted Contributor

Re: Scripting Question

If you are ftp'ing, do a man on ftp and look at the two options: ntrans, and nmap. They should help you rename files as you are ftp'ing given the criteria you give it.
Curtis Larson
Trusted Contributor

Re: Scripting Question

you can change the environment variable IFS (internal field separator).

ksh uses each of the characters in the value of the IFS variable to split into fields: the result from command substitution or parameter expansion of command words, and the result from command substitution or parameter expansion of words after in with for and select, and the characters that ksh reads with read.

ksh uses the first character of the value of IFS to separte arguments when ksh expands the * parameter, and when ksh expands ${varname[*]}.

Any sequence of characters belonging to the space class that are contained in IFS delimit a field, except for any character that appears in IFS more than once consecutively. Otherwise, each occurrence of an IFS character and adjacent space characters contained in IFS, delimit a field. If a space character is repeated consecutively in IFS, the character behaves like a non-space character so taht each occurrence delimits a field. If IFS is set to NULL, ksh does not perform field splitting.

Repeating a space character to cause each instance to act as a delimiter is available only on version of ksh newer than the 11/16/88 version.

To remove the normal meaning the ksh places on any special character, you must quote it. You can quote a single character by preceding it with a \. You can quote a string of characters by enclosing them in: single quotes, '...', to remove the special meaning of all characters except ', or ANSI C strings, $'...', to remove the special meaning of all characters except the ANSI C escape sequences, \b,\n,\t,\r,etc. ANSI C strings are only available on version newer then 11/16/88, or with double quotes, "...", to remove the special meaning of all characters except $,\,and '.
Maureen Gunkel
Trusted Contributor

Re: Scripting Question

James,
I'm not sure what you mean when you say 'ignore' the white space. Do you want filename 'CR filename.txt' to become 'CRfilename.txt.datestamp' or do you want it 'CR filename.txt.datestamp'?
Perhaps I can help in either scenario.
Peace,
Mo
No matter where you go, there you are.
Volker Borowski
Honored Contributor

Re: Scripting Question

Juppie,
I like these puzzles !!!!

ls -1 | awk '{ print "mv \"" $0 "\"" " " "\"" $0 ".2001-03-21\"" }' > doit.sh

Look at doit.sh and execute it !
May be someone can put in how to pass the date as a parameter

OK, I spell it, to be sure it is not misstyped:

Better you clip the line, than retype it.
Syntax is "ls -ONE", not "ELL"

Quoutesequence is:
single
double, EM, VEE, BLANK, backslash double, double
blank, $0, blank
double, backslash double, double
blank
double, blank, double
blank
double, backslash double, double
blank, $0, blank
double, , backslash double, double
single

Have fun
Volker
Volker Borowski
Honored Contributor

Re: Scripting Question

Hmmm,

works not for NEW-LINES, but should do for blanks and tabs :-(

# ls -1
one one
two two
three three
# ls -1 | awk '{ print "mv \"" $0 "\"" " " "\"" $0 ".2001-03-21\"" }'
mv "one one" "one one.2001-03-21"
mv "two two" "two two.2001-03-21"
mv "three three" "three three.2001-03-21"
# ls -1 | awk '{ print "mv \"" $0 "\"" " " "\"" $0 ".2001-03-21\"" }' > doit.sh
# sh doit.sh
# ls -1
doit.sh
one one.2001-03-21
two two.2001-03-21
three three.2001-03-21
#

Sorry, if this was just 20% of it
Volker
Brian Markus
Valued Contributor

Re: Scripting Question

As far as adding the date to the end of a file, I do this

cat file1 > file2`date +%m%d%y`

Hope that helps.

Brian
When a sys-admin say's maybe, they don't mean 'yes'!