Bash script - stripping away characters that can't be used in filenames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script - stripping away characters that can't be used in filenames
# 1  
Old 06-03-2009
Bash script - stripping away characters that can't be used in filenames

I want to create a temp file which is named based on a search string. The search string may contain spaces or characters that aren't supposed to be used in filenames so I want to strip those out.

My thought was to use 'tr' with [:alnum:] but the result is the opposite of what I want:

Code:
$ echo "test data[]" | tr -d [:alnum:] | tr -d ' '
[]
$

The result I'm looking for is "testdata".

Thanks,

Mike G.
# 2  
Old 06-03-2009
The only character that cannot be used in filename is '/' - act accordingly.
# 3  
Old 06-03-2009
Thank you. I should have done my homework.

MG
# 4  
Old 06-04-2009
Quote:
Originally Posted by vgersh99
The only character that cannot be used in filename is '/' - act accordingly.

However, to guarantee portability, filenames should only contain letters, number, hyphen, underscore and dot, and should not begin with a hyphen.
# 5  
Old 06-04-2009
Quote:
Originally Posted by cfajohnson
However, to guarantee portability, filenames should only contain letters, number, hyphen, underscore and dot, and should not begin with a hyphen.
In this case the files being generated are containers for "previous run" search statistics that are located in /tmp. This script will not be ported and the files never copied off so it's not an issue.

That being said, I am working on some other stuff where it will matter and it will be important to keep this in mind. Thanks for the tip!
# 6  
Old 06-04-2009
Quote:
Originally Posted by mglenney
In this case the files being generated are containers for "previous run" search statistics that are located in /tmp. This script will not be ported and the files never copied off so it's not an issue.

I remember saying that about my first major Unix project, back in the early 1990s.

Guess what? The next year the client changed systems, and it all had to be fixed. (The specifics were different, but the principle's the same.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Stripping unwanted characters in field

I wrote myself a small little shell script to clean up a file I have issues with. In particular, I am stripping down a fully qualified host/domain name to just the hostname itself. The script works, but from a performance standpoint, it's not very fast and I will be working with large data sets. ... (4 Replies)
Discussion started by: dagamier
4 Replies

2. Shell Programming and Scripting

How best to remove certain characters from filenames and folders recursively

hello, I'm trying to figure out which tool is best for recursively renaming and files or folders using the characters \/*?”<>| in their name. I've tried many examples that use Bash, Python and Perl, but I'm not much of a programmer I seem to have hit a roadblock. Does anyone have any... (15 Replies)
Discussion started by: prometheon123
15 Replies

3. UNIX for Dummies Questions & Answers

find & remove characters in filenames

I have a group of files in different directories with characters such as " ? : in the file names. How do I find these files and remove these characters on mass? Thanks (19 Replies)
Discussion started by: barrydocks
19 Replies

4. Shell Programming and Scripting

Stripping characters from a file and reformatting according to another one

Dear experts, my problem is pretty tricky. I want to change a file (see attached input.txt), according to another file (help.txt). The output that is desired is in output.txt. The example is attached. Note that -dashes should not be treated specially, they are considered normal characters,... (2 Replies)
Discussion started by: TheTransporter
2 Replies

5. Shell Programming and Scripting

Stripping characters from a variable

I'm using a shell script to get user input with this command: read UserInput I would then like to take the "UserInput" variable and strip out all of the following characters, regardless of where they appear in the variable or how many occurrences there are: \/":|<>+=;,?*@ I'm not sure... (5 Replies)
Discussion started by: nrogers64
5 Replies

6. Shell Programming and Scripting

Whitespace in filenames in for loop in bash script

I'm trying to search all .odt files in a directory for a string in the text of the file. I've found a bash script that works, except that it can't handle whitespace in the filenames. #!/bin/bash if ; then echo "Usage: searchodt searchterm" exit 1 fi for file in $(ls *.odt); do ... (4 Replies)
Discussion started by: triplemaya
4 Replies

7. Shell Programming and Scripting

Filenames created with '\r' characters at the end

Hi all, Am creating files and doing copy,compare and deletion. As i do not want to mention the filepath everywhere, i store the filepaths in variables. FILENAME="/home/test/create/Myfile.txt" WR_PATH="/home/test/wrie/writefile.txt" RD_PATH="/home/test/myread/readfile.txt" echo "This is my... (2 Replies)
Discussion started by: amio
2 Replies

8. Shell Programming and Scripting

stripping certain characters in at the middle of a string

I am trying to strip out certain characters from a string on both (left & right) sides. For example, line=see@hear|touch, i only want to echo the "hear" part. Well i have tried this approach: line=see@hear|touch templine=${line#*@} #removed "see@" echo ${templine%%\|*} #removed... (4 Replies)
Discussion started by: mcoblefias
4 Replies

9. UNIX for Dummies Questions & Answers

[bash]Stripping lines from a list

Hello! I have a script that is (among other things) doing the following: list=/tmp/list1.txt ncftpls -u <user> -p <password> -x "-l1" server.domain.tld > $list cat $list | nl echo "Choose file: " read file cat /tmp/list1.txt | nl | grep $file | sed -e "s/$file//g" -e "s/ //g" | column -t... (8 Replies)
Discussion started by: noratx
8 Replies

10. Shell Programming and Scripting

stripping leftmost characters from string

Hi there, if i have some strings ie test_324423 test_242332 test_767667 but I only want the number part (the bolded bit) how do I strip the leftmost 5 characters from the output so that i will have just 324423 242332 767667 any help would be greatly appreciated Gary (5 Replies)
Discussion started by: hcclnoodles
5 Replies
Login or Register to Ask a Question