How to replace a tabulation in shell bash linux - linux

to replace a spaces by , (as example) in a giving string in shell I use the following command
str=${str// /,}
How to replace a tabulation by , in a string?
I tried
str=${str//\t/,}
But it does not work

To stay in your context of using shell internal replacement while expanding variables:
str=$'foo\tbar'
echo "${str//$'\t'/,}"

You can use tr for this:
$ cat a
hello how are you? blabla
$ tr '\t' ',' <a
hello,how are you?,blabla

Related

How to add quotes to a string in shell script

I have a string for which i need to add quotes to a substring. How can i do that in shell scripting?
the string is
Database.Procedure(Frequency,P1,P2,P3)
I have to add single quotes around Frequency.
Database.Procedure('Frequency',P1,P2,P3)
This should be generic for all such inputs. Please help
Thanks
Something like this works: sed "s/Frequency/\'Frequency\'/"
$ echo "Database.Procedure(Frequency,P1,P2,P3)" | sed "s/Frequency/\'Frequency\'/"
Database.Procedure('Frequency',P1,P2,P3)
It is a matter of changing the word Frequency by the same one with quotes. As the ' is a reserved word, we have to escape it with \'.
Your comment:
But the problem is Frequency can be anything like 'DAILY' , 'WEEKLY' ,
'MONTHLY' etc. What should i do for that?
If you want to make it more general, do
var="Your_name"
sed "s/$var/\'$var\'/"
Examples:
$ var=Frequency
$ echo "Database.Procedure(Frequency,P1,P2,P3)" | sed "s/$var/\'$var\'/"
Database.Procedure('Frequency',P1,P2,P3)
$ var=Weekly
$ echo "Database.Procedure(Weekly,P1,P2,P3)" | sed "s/$var/\'$var\'/"
Database.Procedure('Weekly',P1,P2,P3
try using an escape character
Database.Procedure(\'Frequency\',P1,P2,P3)

how to replace a special characters by character using shell

I have a string variable x=tmp/variable/custom-sqr-sample/test/example
in the script, what I want to do is to replace all the “-” with the /,
after that,I should get the following string
x=tmp/variable/custom/sqr/sample/test/example
Can anyone help me?
I tried the following syntax
it didnot work
exa=tmp/variable/custom-sqr-sample/test/example
exa=$(echo $exa|sed 's/-///g')
sed basically supports any delimiter, which comes in handy when one tries to match a /, most common are |, # and #, pick one that's not in the string you need to work on.
$ echo $x
tmp/variable/custom-sqr-sample/test/example
$ sed 's#-#/#g' <<< $x
tmp/variable/custom/sqr/sample/test/example
In the commend you tried above, all you need is to escape the slash, i.e.
echo $exa | sed 's/-/\//g'
but choosing a different delimiter is nicer.
The tr tool may be a better choice than sed in this case:
x=tmp/variable/custom-sqr-sample/test/example
echo "$x" | tr -- - /
(The -- isn't strictly necessary, but keeps tr (and humans) from mistaking - for an option.)
In bash, you can use parameter substitution:
$ exa=tmp/variable/custom-sqr-sample/test/example
$ exa=${exa//-/\/}
$ echo $exa
tmp/variable/custom/sqr/sample/test/example

Split a string based on backslash delimiter

I'm new to bash scripting. I have a string which is like so: \\abc\def\ghi
I want to parse the string using a delimiter and need a one line command for converting it to /abc/def/ghi (convert Windows path to unix path).
Try doing this :
$ x='\abc\def\ghi'
$ echo ${x//\\//}
/abc/def/ghi
See parameter expansion
NOTE
parameter expansions are bash built-ins, so it's quicker than external commands
string=$( echo "$string" | tr '\' '/' )
or with sed:
kent$ echo -E "\abc\def\ghi"|sed 's:\\:/:g'
/abc/def/ghi

How can I remove the "Enter character"(ascii=13) in the given string using this linux shell command?

I want to use this linux shell command to remove the "Enter character"(ascii=13):
${variable//pattern/string}
My script is :
${var//\n/}. It doesn't work.
So I change to ${var//\r/} and it doesn't work too.
So how should I write this script?
Thanks in advance.
Use dollar expansion $'\n':
${var//$'\n'/}
$'\n' expands to a literal newline. See ANSI-C quoting in the bash manual for more about this sort of expansion.
Edit
The above replaces newline, to replace carriage return use $'\r':
${var//$'\r'/}
You can do:
var=$(echo var | tr -d '\n')
to remove the newline. Or var=$(echo var | tr '\n' ' ') to replace it with a space.

How can I convert a string from uppercase to lowercase in Bash? [duplicate]

This question already has answers here:
How to convert a string to lower case in Bash
(29 answers)
Closed 4 years ago.
I have been searching to find a way to convert a string value from uppercase to lowercase. All the search results show approaches of using the tr command.
The problem with the tr command is that I am able to get the result only when I use the command with the echo statement. For example:
y="HELLO"
echo $y| tr '[:upper:]' '[:lower:]'
The above works and results in 'hello', but I need to assign the result to a variable as below:
y="HELLO"
val=$y| tr '[:upper:]' '[:lower:]'
string=$val world
When assigning the value like above it gives me an empty result.
PS: My Bash version is 3.1.17
If you are using Bash 4, you can use the following approach:
x="HELLO"
echo $x # HELLO
y=${x,,}
echo $y # hello
z=${y^^}
echo $z # HELLO
Use only one , or ^ to make the first letter lowercase or uppercase.
One way to implement your code is
y="HELLO"
val=$(echo "$y" | tr '[:upper:]' '[:lower:]')
string="$val world"
This uses $(...) notation to capture the output of the command in a variable. Note also the quotation marks around the string variable -- you need them there to indicate that $val and world are a single thing to be assigned to string.
If you have Bash 4.0 or higher, a more efficient & elegant way to do it is to use Bash built-in string manipulation:
y="HELLO"
string="${y,,} world"
Note that tr can only handle plain ASCII, making any tr-based solution fail when facing international characters.
Same goes for the Bash 4-based ${x,,} solution.
The AWK tool, on the other hand, properly supports even UTF-8 / multibyte input.
y="HELLO"
val=$(echo "$y" | awk '{print tolower($0)}')
string="$val world"
Answer courtesy of liborw.
Execute in backticks:
x=`echo "$y" | tr '[:upper:]' '[:lower:]'`
This assigns the result of the command in backticks to the variable x. (I.e., it's not particular to tr, but it is a common pattern/solution for shell scripting.)
You can use $(..) instead of the backticks. See here for more info.
I'm on Ubuntu 14.04 (Trusty Tahr), with Bash version 4.3.11. However, I still don't have the fun built-in string manipulation ${y,,}
This is what I used in my script to force capitalization:
CAPITALIZED=`echo "${y}" | tr '[a-z]' '[A-Z]'`
If you define your variable using declare (old: typeset) then you can state the case of the value throughout the variable's use.
declare -u FOO=AbCxxx
echo $FOO
Output:
ABCXXX
Option -l to declare does lowercase:
When the variable is assigned a value, all upper-case characters are converted to lower-case. The upper-case attribute is disabled.
Building on Rody's answer, this worked for me.
y="HELLO"
val=$(echo $y | tr '[:upper:]' '[:lower:]')
string="$val world"
One small modification: if you are using underscore next to the variable, you need to encapsulate the variable name in {}.
string="${val}_world"

Resources