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

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.

Related

How to extract string between quotes in Bash

I need to extract the string between quotation marks in a file.
For example: my file is called test.txt and it has the following content:
"Hello_World"
I am reading it as follows from bash:
string="$(head -1 test.txt)"
echo $string
This prints "Hello_World", but I need Hello_World.
Any help will be appreciated. Thanks.
You can do this in pure bash without having to spawn any external programs:
read -r line < test.txt ; line=${line#\"} ; line=${line%\"} ; echo $line
The read actually reads in the entire line, and the two assignments actually strip off any single quote at the start or end of the line.
I assumed you didn't want to strip out any quotes within the string itself so I've limited it to one at either end.
It also allows you to successfully read lines without a leading quote, trailing quote, or both.
You can use tr:
echo "$string " | tr -d '"'
From man tr:
DESCRIPTION
The tr utility copies the standard input to the standard output with substitution or deletion of selected characters.
The following options are available:
-C Complement the set of characters in string1, that is ``-C ab'' includes every character except for `a' and `b'.
-c Same as -C but complement the set of values in string1.
-d Delete characters in string1 from the input.
You can simply use sed to read the first line and also filter out ", try following command,
sed -n '1 s/"//gp' test.txt
Brief explanation,
-n: suppress automatic print
1: Match only the first line
s/"//gp: filter out ", and then print the line

How to replace a tabulation in shell bash 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

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)

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

Unix command to escape spaces

I am new to shell script. Can someone help me with command to escape the space with "\ ".
I have a variable FILE_PATH=/path/to my/text file ,
I want to escape the spaces alone
FILE_PATH=/path/to\ my/text\ file
I tried with tr -s command but it doesnt help
FILE_PATH=echo FILE_PATH | tr -s " " "\\ "
Can somebody suggest the right command !!
If you are using bash, you can use its builtin printf's %q formatter (type help printf in bash):
FILENAME=$(printf %q "$FILENAME")
This will not only quote space, but also all special characters for shell.
There's more to making a string safe than just escaping spaces, but you can escape the spaces with:
FILE_PATH=$( echo "$FILE_PATH" | sed 's/ /\\ /g' )
You can use 'single quotes' to operate on a path that contains spaces:
cp '/path/with spaces/file.txt' '/another/spacey path/dir'
grep foo '/my/super spacey/path with spaces/folder/*'
in a script:
#!/bin/bash
spacey_dir='My Documents'
spacey_file='Hello World.txt'
mkdir '$spacey_dir'
touch '${spacey_dir}/${spacey_file}'
You can do it with sed :
NEW_FILE_PATH="$(echo $FILE_PATH | sed 's/ /\\\ /g')"
Use quotes to preserve the SPACE character
tr is used only for replacing individual characters 1 for 1. Seems to be that you need sed.
echo $FILE_PATH | sed -e 's/ /\\ /'
seems to do what you want.
Do not forget to use eval when using printf guarding.
Here's a sample from Codegen Script under Build Phases of Xcode (somehow PROJECT_DIR is not guarded of spaces, so build was failing if you have a path with spaces):
PROJECT_DIR_GUARDED=$(printf %q "$PROJECT_DIR")
eval $PROJECT_DIR_GUARDED/scripts/code_gen.sh $PROJECT_DIR_GUARDED
FILE_PATH=/path/to my/text file
FILE_PATH=echo FILE_PATH | tr -s " " "\\ "
That second line needs to be
FILE_PATH=echo $FILE_PATH | tr -s " " "\\ "
but I don't think it matters. Once you have reached this stage, you are too late. The variable has been set, and either the spaces are escaped or the variable is incorrect.
FILE_PATH='/path/to my/text file'

Resources