Sponsored Content
Top Forums Shell Programming and Scripting rename file with whitespace embedded Post 99009 by orlando47 on Monday 13th of February 2006 10:38:02 PM
Old 02-13-2006
a)searching for then b)removing spaces from filenames(from " " to "_")

Say a directory contains files
1) "file name 1.xxx"
2) "file name2.yyy"
3) etc

Using a cshell script, is there a way to (1)search for all files that contain " " in the filename and then (2)rename the files so that you replace the whiitespace " " with a "_".

First problem I run into is when I put "file name 1.xxx" into a list contianing other filenames with spaces each word seperated by a space is treated as a seperate element when used in a foreach loop. Instead of getting "file name 1.xxx" as an element I get 1) "file" 2) "name" 3) "1.xxx".

Example:

set list = `find -type d -name '* *' | tr -d "./"`
foreach i ( $list )
set newfile = `echo $file | sed 's/ /_/' `
mv $i $newfile
end

Second problem, let's assume the $i contained the directory/file name string "file name 1.xxx". If we were to use $i in a mv statement such as

mv $i $newfile

the mv would interpret "file name 1.xxx" as three seperate files 1) "file" 2) "name" 3) "1.xxx" . mv would attempt to mv 3 files into $newfile. Of course this is not what we want. We would like to rename the file named "file name 1.xxx" to a newfile name.

mv "file name 1.xxx" to file_name_1.xxx

It is easy enough at the command line. But it becomes a tedious task when you have many files/directories that you have to rename. It would be nice to do it with a script but I am at a lost. It kills me that I am actually considering doing this by hand. What a waste of time. Spaces in file/directory names are a _itch.

Thanks
O

Last edited by orlando47; 02-14-2006 at 10:58 AM.. Reason: makeing title clearer
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

embedded exe file into rtf file

Hi All I want a method in shell script which will embed the exe file into rtf file si that the rtf file can be open in word and so my exe. Actually what is happening in my system at present : The report in my system is a self-extracting Zip file with filetype '.EXE'. Double clicking... (1 Reply)
Discussion started by: rawatds
1 Replies

2. AIX

Unix shell scripting to find latest file having timestamp embedded...

Hi guys, I have a directory in UNIX having files with the below format, i need to pickup the latest file having recent timestamp embedded on it, then need to rename it to a standard file name. Below is the file format: filename_yyyymmdd.csv, i need to pick the latest and move it with the... (2 Replies)
Discussion started by: kaushik25
2 Replies

3. Shell Programming and Scripting

removing whitespace from middle of file -help

I have a file in which I clean out a bunch of nonsense text as well as path information. What I end up with is something like the following: johnson.........................................................933 Where the periods represent the whitespace The file comes out originally with... (2 Replies)
Discussion started by: roninuta
2 Replies

4. Shell Programming and Scripting

Copy files listed in a text file - whitespace problem.

Hi, Say I have this text file <copy.out> that contains a list of files/directories to be copied out to a different location. $ more copy.out dir1/file1 dir1/file2 dir1/file3 "dir1/white space" dir1/file4 If I do the following: $copy=`more copy.out` $echo $copy dir1/file1... (4 Replies)
Discussion started by: 60doses
4 Replies

5. Shell Programming and Scripting

Removing Embedded Newline from Delimited File

Hey there - a bit of background on what I'm trying to accomplish, first off. I am trying to load the data from a pipe delimited file into a database. The loading tool that I use cannot handle embedded newline characters within a field, so I need to scrub them out. Solutions that I have tried... (7 Replies)
Discussion started by: bbetteridge
7 Replies

6. Shell Programming and Scripting

Minimum whitespace separated CSV file generation

Hi, I have a flat text file consisting of rows (each field separated by '|') from a table. e.g; NSW|Gulliver Travels|236||5000|BW This has to be converted to the following format NSW "Gulliver Travels" 236 5000 BW No data field has to be left as a blank character so that we have... (6 Replies)
Discussion started by: vharsha
6 Replies

7. Shell Programming and Scripting

How to match (whitespace digits whitespace) sequence?

Hi Following is an example line. echo "192.22.22.22 \"33dffwef\" 200 300 dsdsd" | sed "s:\(\ *\ \):\1:" I want it's output to be 200 However this is not the case. Can you tell me how to do it? I don't want to use AWK for this. Secondly, how can i fetch just 300? Should I use "\2"... (3 Replies)
Discussion started by: shahanali
3 Replies

8. Shell Programming and Scripting

Replace last line of file in memory via embedded command?

Further to my earlier question regarding embedded commands I now want to delete then replace the last line in a file in memory.... FILEHERE=$((echo "$FILEHERE" | sed -e '$d' ) echo "this now added") this is throwing a syntax error but as I'm still getting used to this structure I... (1 Reply)
Discussion started by: Bashingaway
1 Replies

9. Shell Programming and Scripting

Putting two perl scripts together... triming whitespace off of recently parsed file

Thanks to people's help, I have composed a single line within a .sh script that Ports a file into a csv: perl -p -i -e... (5 Replies)
Discussion started by: Astrocloud
5 Replies

10. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies
CG(1)																	     CG(1)

NAME
cg - Recursively grep for a pattern and store it. SYNOPSIS
cg [ -l ] | [ [ -i ] pattern [ files ] ] DESCRIPTION
cg does a search though text files (usually source code) recursively for a pattern, storing matches and displaying the output in a human- readable fashion. It is intended to give some of the functionaly of AT&T's cscope(1) tool, with the advantages of simplicity and not being language-specific. The script will colorize output if configured as such. It is typically run with a Perl regular expression to search for. The search can be made case insensitive by using the -i option. A list of files may also be specified with an additional argument after the pattern. Put the files pattern in quotes to make it be matched by Perl rather than by the shell. Running the script with no arguments will recall the results of the previous search. After the search, entries found can be edited using the vg(1) script. The -l option shows the last log made. SOME EXAMPLES
cg - alone recalls the previous search results. cg -i pattern - search the default list of files for all files matching the pattern (and case-insensitively). cg pattern '*.c' - search recursively for pattern in all *.c files. This automatically converts '*' to '.*' and '.' to '.' for you and does a Perl pattern match on all files in the tree. cg pattern *.c - search through the shell-expanded list of *.c files, so not done recursively (in other words, only the files your shell pass to the script as arguments). cg -l - show the last log made. COMMAND-LINE OPTIONS -i Do a case-insensitive search. -l Show the last log made. -p Toggle the default pager option. cg has a bulit-in pager function, which can be enabled or disabled by default (in .cgvgrc). If the default is enabled, this option disables the pager; if the default is disabled, this option enables it. -P Force the built-in pager to be disabled. FILES
${HOME}/.cglast Log file of the last search. ${HOME}/.cgvgrc Per-user configuration file (if the defaults are not desireable). ${HOME}/.cgvg/* Log files in $HOSTNAME.shell_pid form with the log of the last search. SEE ALSO
vg(1), perl(1), find(1), grep(1), cscope(1) AUTHOR
cg was written by Joshua Uziel <uzi@uzix.org>. 13 Mar 2002 CG(1)
All times are GMT -4. The time now is 04:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy