UNIX Replace string without replacing space [duplicate] - bash

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 5 years ago.
For string matching purposes I need to define a bash variable with leading spaces.
I need to define this starting from an integer, like:
jj=5
printf seems to me a good idea, so if I want to fill spaces up to 6 character:
jpat=`printf " %6i" $jj`
but unluckly when I am trying to recall the variable:
echo $jpat
the leading whitespaces are removed and I only get the $jj integer as it was.
Any solution to keep such spaces?
(This is equivalent to this: v=' val'; echo $v$v. Why aren't there leading and multiple spaces in output?)

Use More Quotes! echo "$jpat" will do what you want.
There is another issue with what you're doing: Command substitutions will remove trailing newlines. It's not an issue in the printf command you're using, but for example assigning jpat=$(printf " %6i\n" "$jj") would give you exactly the same result as your command.

Related

bash variable eats multiple spaces, turning them to one [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 3 years ago.
# export var="many spaces"; echo =${var}=
=many spaces=
What is going on here?
Why multiply spaces are turned to one? How to keep all?
You’re simply missing quotations around your variable. Changing your code to this:
$ export var="many spaces"; echo ="${var}"=
=many spaces=
should give the result you’re looking for. One “feature” of bash that you need to watch out for is word splitting, which is based on the value of your IFS (internal field separator) variable. Typically IFS defaults to
IFS=$' \t\n'
so you need to take care in quoting variables that contain spaces, tabs, and newlines.

BASH remove specific tokens from a word [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
I am trying to find te longest word in a given file. Before I check the lengtgh of each word I need to remove all of the following tokens {,.:} that may be attached (once or more) to the word. so for example, for this text:
:,cat dog, encyclopedia; remove:.,
i need the result:
cat dog encyclopedia remove
I am trying this, but I get a "command not found":
longest=0
for word in $(<$1)
do
#new_word = $(echo "${word//[.,:]/}")
new_word = "${word//[.,:]/}"
len=${#new_word}
if (( len > longest ))
then
longest=$len
longword=$new_word
fi
done
echo The longest word is $longword and its length is $longest.
thank you.
Your use of parameter expansion replacement pattern is correct.
The problem is that there must not be any whitespace around = while declaring variables in bash (any shell in general).
So, the following should work:
new_word="${word//[.,:]/}"
As an aside, use a while read ... construct to loop over the lines in a file, using for is pretty fragile.

What does expanding a variable as "${var%%r*}" mean in bash? [duplicate]

This question already has an answer here:
Bash: manipulating with strings (percent sign)
(1 answer)
Closed 6 years ago.
I've got the following variable set in bash:
ver=$(/usr/lib/virtualbox/VBoxManage -v | tail -1)
then I have the following variable which I do not quite understand:
pkg_ver="${ver%%r*}"
Could anyone elaborate on what this does, and how pkg_ver is related to the original ver value?
It is a bash parameter expansion syntax to extract text from end of string upto first occurrence of r
name="Ivory"
printf "%s\n" "${name%%r*}"
Ivo
${PARAMETER%%PATTERN}
This form is to remove the described pattern trying to match it from the end of the string. The operator "%" will try to remove the shortest text matching the pattern, while "%%" tries to do it with the longest text matching.
You will get everything from variable ver until first "r" character and it will be stored inside pkg_ver.
export ver=aaarrr
echo "${ver%%r*}"
aaa

bash script whole file reading into a variable with newline [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 7 years ago.
i want to read whole file in a variable.
for example my file is.
file name : Q.txt
My name is Naeem Rehmat.
I am a student of FAST university.
Now i am in 4th semester.
I am learning bash script.
I have this problem.
code:
text=$(cat Q.txt)
echo $text
out put should be like this:
My name is Naeem Rehmat.
I am a student of FAST university.
Now i am in 4th semester.
I am learning bash script.
I have this problem.
Presumably the problem is that your whitespace is incorrect. Use double quotes:
echo "$text"
When you write echo $text without quotes, bash evaluates the string and performs what is known as "field splitting" or "word splitting" before generating the command. To simplify the case, suppose text is the string foo bar. Bash splits that into two "words" and passes "foo" as the first argument to echo, and "bar" as the second. If you use quotes, bash passes only one argument to echo, and that argument contains multiple spaces which echo will print.
Note that it is probably good style to also use quotes in the assignment of text (ie, text="$(cat Q.txt)"), although it is not necessary since field splitting does not occur in a variable assignment.

Appending text to the end of a variable [duplicate]

This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Closed 8 years ago.
The following works, but I don't want the space that it returns:
read input
file= "$input"
file= "$file ins.b" # how to get rid of the space here?
echo "$file"
This outputs 'file ins.b'
I don't want the space between file and ins.b
If I don't leave that space in the code it returns only '.b'. What can I do to resolve this problem?
Append like:
file="${file}ins.b"
If you don't use braces then it treats fileins as a variable and expands it. Since, it's probably not set it just prints .b.
Related: When do we need curly braces in variables using Bash?
In bash you can also reference variables like ${file}. So this should work for you:
file="${file}ins.b"
You don't need to expand the old value at all; bash has a += operator:
file+="ins.b"
file="${file}ins.b"
or
file=$file"ins.b"

Resources