Translate wildcard-char "*"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Translate wildcard-char "*"
# 1  
Old 07-13-2011
Translate wildcard-char "*"

Hi,

I am facing an issue while translating "*" character.

This is what I need
Code:
 
Input=/path/to/the/file/data_file*.txt*.tar.gz
Output=/path/to/the/file/data_file\*.txt\*.tar.gz

When I try -
Code:
echo $Input | tr '*' '\*'

nothing happens.

Is there a way to achieve the Output?

-dips
# 2  
Old 07-13-2011
Some shells (zsh, recent bash versions) support the q printf format specification:
Code:
% v=/path/to/the/file/data_file*.txt*.tar.gz 
% printf '%q\n' "$v"                         
/path/to/the/file/data_file\*.txt\*.tar.gz

With bash (>= 3.x) you can assign directly the printf output to a variable:

Code:
4.1.10(4)-release$ v=/path/to/the/file/data_file*.txt*.tar.gz
4.1.10(4)-release$ printf -vv '%q' "$v"
4.1.10(4)-release$ echo "$v"
/path/to/the/file/data_file\*.txt\*.tar.gz

But that will affect other special characters too.
Another option (zsh, ksh93, bash) is:

Code:
4.1.10(4)-release$ v=/path/to/the/file/data_file*.txt*.tar.gz
4.1.10(4)-release$ echo "${v//\*/\*}"
/path/to/the/file/data_file\*.txt\*.tar.gz

sed (not tr!) should be the last resort.
# 3  
Old 07-13-2011
Code:
sed -n 's/\*/\\*/gp' input


Last edited by radoulov; 07-13-2011 at 12:24 PM.. Reason: Code tags.
# 4  
Old 07-13-2011
tr only substitutes single chars for single chars, it can't substitute two chars for one char, which is why you need sed instead.
# 5  
Old 07-15-2011
Thanks everyone!!

I tried ltomuno's command. But the strange thing is that while it worked on unix box it does not in ksh script. I am puzzled.

Unix Box command line:
Code:
[dips ~]$ echo data_file*.txt*.tar.gz | sed 's/\*/\\*/g'
[dips ~]$ data_file\*.txt\*.tar.gz

KSH script:
Code:
 
trans_file_name=`echo data_file*.txt*.tar.gz | sed 's/\*/\\*/g'`

When I ran the script in debug mode; the sed cmd is reduced to
Code:
 
sed 's/\*/\*/g'`

I don't know why it shows only 1 such "\" char. Then I thought that in the script we must escape both "\" & "*" so I changed the cmd to
Code:
 
sed 's/\*/\\\*/g'

Still no change it showed only 1 escape (\) char (while running in debug mode) and hence the output is same as input.
Please suggest what needs to be done to make it work in a KSH script?
- dips
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Advanced & Expert Users

"GET" command retrieves multiple files while using wildcard

Hi All I am using GNU/Linux This is regarding the get command to retrieve files (filename with wild card characters) from remote server. I thought Get command can retrieve only 1 file irrespective of the files it has on the remote server And it is the function of mget to retrieve all... (7 Replies)
Discussion started by: sparks
7 Replies

3. Shell Programming and Scripting

Move a line containg "char" above line containing "xchar"

Okay, so I have a rather large text file and will have to process many more and this will save me hours of work. I'm not very good at scripting, so bear with me please. Working on Linux RHEL I've been able to filter and edit and clean up using sed, but I have a problem with moving lines. ... (9 Replies)
Discussion started by: rex007can
9 Replies

4. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

5. Programming

Small query regarding function "char * strerror(int errnum)"

As this function returns the address of the string corressponding to the errno value provided to it. Can someone please let me know where, in the memory, it could be (on freeBSD). The MAN page tells under the BUG section that "For unknown error numbers, the strerror() function will return its... (5 Replies)
Discussion started by: Praveen_218
5 Replies

6. UNIX for Dummies Questions & Answers

Problem with sed wildcard "*"

Hi All, can you help me with "*" wildcard character in sed, i am a bit confused. I am going through a reference and found that below code sed -n '/a*c/' file will also return a line that contain the string output : close the window properly how is that possible when there is no "a"... (11 Replies)
Discussion started by: mukulverma2408
11 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Solaris

significance of "+" char in SunOS "ls -l" output

Hi, I've noticed that the permissions output from "ls -l" under SunOS differs from Linux in that after the "rwxrwxrwx" field, there is an additional "+" character that may or may not be there. What is the significance of this character? Thanks, Suan (6 Replies)
Discussion started by: sayeo
6 Replies

9. UNIX for Dummies Questions & Answers

Preventing wildcard expansion in "read"

Hi, I'm trying to use "read" to read in SQL lines and then apply to UDB. However KSH is expanding the *'s in the SQL into a list of all the files in the current directory... Anyway to turn this off? while read SQLStatement do echo "Running : " $SQLStatement... (8 Replies)
Discussion started by: gaijin 06
8 Replies
Login or Register to Ask a Question