How to handle variable with special character?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to handle variable with special character?
# 1  
Old 11-21-2018
How to handle variable with special character?

Hi Gurus,


I have a requirement which needs to pass a parameter when calling the script, using this parameter to find a file name stored in master file. then read the file content. the issue is in the file name has a special character "$". don't know how to handle this. below is example:
the master file is:


Code:
abc, v$xyz
bcd, x$efg

my script like:
Code:
#!/bin/ksh


genfile=$1


while IFS=','  filedir, name
do
cd $filedir
if [ -f ${genfile}* ]; then
cat $name ... "read the file and do something. 
fi
done < Masterfile

the real file name like v$xyz_xxxx_xxxx.xxx


I call the script: ksh scriptname 'v$xyz'.


without "$", the script. with "$", I can put single quote when call the script, but in the script, I don't know how to add single quote in variable "name". please share you idea with me how to handle this.


I know I need to wrap the code with code tag, for some reason, the code tag doesn't work for me.


thanks in advance.

Last edited by Corona688; 11-21-2018 at 05:58 PM..
# 2  
Old 11-21-2018
What is your browser? If the button doesn't work, you can always do it by yourself, [code]stuff[/code]
# 3  
Old 11-21-2018
What is the script supposed to do with $xyz, etc? Anything? variable xyz doesn't appear in your script.

Your loop is slightly wrong:

Code:
while IFS=", " read filedir name
do
        echo "filedir [$filedir] name [$name]"
done < masterfile

# 4  
Old 11-21-2018
Quote:
Originally Posted by Corona688
What is the script supposed to do with $xyz, etc? Anything? variable xyz doesn't appear in your script.

Your loop is slightly wrong:

Code:
while IFS=", " read filedir name
do
        echo "filedir [$filedir] name [$name]"
done < masterfile

thanks Corona688.
the variable is "v$xyz", the file name like v$xyz_xxxx_xxxx.txt.
basically, I want to use variable "v$xyz" to find "v$xyz_xxxx_xxxx.txt" in the directory, if the file exist, then copy file to ABCD_v$xyz.txt. the directory name saved in master file with .
Code:
 abc, v$xyz
bcd, x$efg

# 5  
Old 11-21-2018
'name' is the variable, 'v$xyz' is the value it holds.

The $ is not a problem at all then, the shell won't do anything weird with it. Your [ -f ... ] is problematic though, since * returns more than one file sometimes, which would be a syntax error. So, how about this?

Code:
#!/bin/ksh

genfile="$1"

while IFS=", " read filedir name
do
        echo "filedir [$filedir] name [$name]"
        set -- "${name}"* # Overwrites $1, $2, etc with file1, file2, ...
        if [ -f "$1" ]
        then
                echo "Found file $1"
        fi
done < masterfile

# 6  
Old 11-21-2018
Quote:
Originally Posted by green_k
the variable is "v$xyz",
No, it is not. A "variable" is something like "$name" or "${name}", where "name" is a name you choose. "$xyz" might be a variable, but "v$xyz" is a literal "v" followed by the content of a variable with the name "xyz".

Quote:
Originally Posted by green_k
the file name like v$xyz_xxxx_xxxx.txt.
OK, but is the "$xyz" in this filename now meant literally or should that signify the variable portion of the filename which you want to transport via the variable? If the first is the case a simple quotation will suffice, because this is what it is for - inside of (single) quotes the "$" loses its special meaning to the shell and reverts back to a normal character:

Code:
ls -l v$xyz_xxxx_xxxx.txt

will first replace "$xyz_xxxx_xxxx" with the content of a variable named "xyz_xxxx_xxxx" (which most probably will not exist so that it evaluates to "", the empty string) and therefore search for a file named "v.txt". Whereas:

Code:
ls -l 'v$xyz_xxxx_xxxx.txt'

(notice the single quotes) will look for a file named exactly this: "v$xyz_xxxx_xxxx.txt".

If "$xyz" is a variable and you want to use that as the changing part of an otherwise fixed filename then this code snippet is for you:

Code:
xyz="some$thing"
ls -l "v${xyz}_xxxx_xxxx.txt"

This will search for a file name "vsome$thing_xxxx_xxxx.txt". Because of the braces around "xyz" the shell knows that only this (and not the rest of the string) is the name of the variable. Notice the double quotes: you should habitually double-quote every use of any variable - ever and always! (There are only very few exceptions to this rule of thumb.) This way blanks (yes, filename can contain blanks) will not break your script.

I hope this helps.

bakunin
# 7  
Old 11-22-2018
thanks everybody, it works. I am not sure what i did wrong yesterday. below is my dummy code
Code:
$1 is 'v$abc', filename is v$abc_xxxxx_20180120.txt, the value in filename is v$abc_xxxxx

Code:
#!/bin/ksh
feed=$1
echo $feed
while read filename
do
if [ -f ${filename}* ]; then
mv ${1}* ${1}_newname
fi
done<filename|grep ${1}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Special character $$

Hi, on ksh What does the following do? grep -v "toolbox" $home_oracle/.profile >$home_oracle/.profile.$$ Thanks. Please use CODE tags as required by forum rules! (3 Replies)
Discussion started by: big123456
3 Replies

2. Shell Programming and Scripting

Handle special characters in awk -F

Hello Folks, Need to bisect strings based on a subset. Below works good. echo /a/b/c/d | awk -F"/c/d$" '{print $1}' /a/b However, it goes awry with special characters. echo /a/b/c+/d | awk -F"/c+/d$" '{print $1}' /a/b/c+/d Desired output: /a/b Escaping the special characters... (11 Replies)
Discussion started by: vibhor_agarwali
11 Replies

3. Shell Programming and Scripting

Vi special character

When editing a file, vi displays a special character as ^L. Can you tell me the escaped character to be used in awk? And can that escaped character be used in a regexp in both sed and awk? (7 Replies)
Discussion started by: dmesserly
7 Replies

4. Shell Programming and Scripting

How to substitute variable in sed for special character?

Hi , I have input file like below Hi this is "vinoth". Hi happy to work with 'unix' USA(united states of America) My script variables are below : Dquote=Ộ Squote=&#$567 Obrac=&^986 Cbrac=&^745 I want to read the variables in my SED command to replace the double quote,single... (9 Replies)
Discussion started by: vinothsekark
9 Replies

5. Shell Programming and Scripting

How to handle variable with spaces in ksh

I'm having issue capturing a value from file.list with a multiple spaces in a variable $i, tried various options like using double quotes, no quotes, single quotes, curly braces but to no avail. cat file.list aaa test bbb ccc test ddd eee test fff for i in `cat file.list` do echo "$i";... (2 Replies)
Discussion started by: mbak
2 Replies

6. Shell Programming and Scripting

Deleteing one character after an special character

I have below line in a unix file, I want to delete one character after "Â". 20091020.Non-Agency CMO Daily Trade Recap Â~V Hybrids The result should be : 20091020.Non-Agency CMO Daily Trade Recap  Hybrids i dont want to use "~V" anywhere in the sed command or any other command, just remove... (1 Reply)
Discussion started by: mohsin.quazi
1 Replies

7. Shell Programming and Scripting

Special character \

Hi, In the shell script, i need to remove the special charater "\" with "\\". For example, i need to replace "D:\FXT\ABC.TXT" with "D:\\FXT\\ABC.TXT". However, when trying to do something like , i get the below error :- -->echo "D:\FXT\ABC.TXT" | sed -e 's#\#\\#g' sed: 0602-404 Function... (7 Replies)
Discussion started by: amit_arora
7 Replies

8. Shell Programming and Scripting

special character

Hi, I am trying to unload file from a database. Which contains few lines with the character below. Rest of the data was unloaded appropriately. a) What does this below character means? b) How can i remove it, I already have sed '/^$/d' c) Will this effect the file by any means... (4 Replies)
Discussion started by: tostay2003
4 Replies

9. Shell Programming and Scripting

How ro handle backslash character in grep?

Hi , I am doing invert grep using -v but the string contain "/" which break the grep command and it do not skip the lines with "/" on it. Diffu.txt ======== 1159c1159 < <td align="right" valign="middle" class="paddingRight2px" id="featureListItemChannelButton7466"> --- > <td... (1 Reply)
Discussion started by: rajbal
1 Replies

10. Programming

special character ?

hey there im a bit stuck on executing commands that include the special character '?'. can someone recommend a way on how i would be able to execute it?? i thought the glob function could be useful (still mite be) but upon entering the command 'ls pars?' it listed all the files in the... (1 Reply)
Discussion started by: mile1982
1 Replies
Login or Register to Ask a Question