That gives the desired result with that specific value of $x, but it is generally wrong.
The parameter expansion pattern "# *" will only match one leading space, not more. The use of a single "#" means that the wildcard is useless; the shortest match will be selected and that match will always be a single space, if any. If you had used "##", it would've matched all characters (including non-spaces) that followed a leading space, effectively deleting the entire string. Remember, in sh globs, * is not a repeater that acts on the previous character, as it is in regular expressions; it can match an arbitrary length of arbitrary characters.
Also, the unquoted parameter expansion will convert any contiguous sequences of IFS characters (by default, spaces, tabs, and newlines) into a single space. Nothing in the problem statement indicates that such a conversion is desirable.
Quote:
Originally Posted by tukuyomi
(using Bash) It can be a good solution:
The first echo removes leading spaces while the second doesn't
That is not a good solution. It removes more than just leading spaces. See above.
A proper pure posix-sh alternative:
Regards,
Alister
How does the sed command actually works? I fail to understand its functionality.
For ex -
sed removes the leading space from the string and returns the output as
But what if I want all the spaces removed from a string and the final output that i want is as follows
Hi,
In my file, I have few records which are split across multiple lines.
File 1:
=====
james,\n
pre-auth completed,in patient,\n
Fac_Id:23451,ramson,Dallas
Expected is:
==========
james,pre-auth completed,in patient,Fac_Id:23451,ramson,Dallas (8 Replies)
Hi there,
i need some help to remove all occurrences of a certain character at the beginning of a string.
Example: my string is 00102030 and i want to remove all zeros from beginning of string so the result is 102030 (3 Replies)
Suppose, I have a variable var=" name is ".
I want to remove the blank spaces from the begining and endonly, not from the entire string.
So, that the variable/string looks like following
var="name is".
Please look after the issue. (3 Replies)
Hi,
I have a string like this and i want to remove extra spaces that exists between the words.
Here is the sentence.
$string="The small DNA genome of hepadnaviruses is replicated by reverse transcription via an RNA intermediate. This RNA "pregenome" contains ... (2 Replies)
seems easy but havent found in other posts...
i want to delete any spaces if found before first occurence of ${AI_RUN}
sed 's/ *\\$\\{AI_RUN\\}/\\$\\{AI_RUN\\}/' $HOME/temp1.dat
i think i'm close but can't put my finger on it. :rolleyes: (6 Replies)
dear pro-coders,
is there any command out there that takes out the front spaces from a string?
sample strings:
4 members
5 members
3 members
but it has to be like so:
4 members
5 members
3 members (3 Replies)
can any help how to remove blank spaces in a string?
STR="GOOD BYE"
by removing blank spaces, the string should be GOOD,BYE
thanks in advance (2 Replies)
Hi all, I'm a newbie to the Linux world and I got a couple of shell script questions:
(1) How do combine two variables and make it equal to a third variable? For example, I got a variable $A=FirstName, $B=LastName, and I want to combine the variable into one variable so when you echo the final... (4 Replies)
Hello,
I have the following to remove spaces from beginning and end of a string.
infile=`echo "$infilename" | sed 's/^ *//;s/ *$//`
How do I modify the above code to remove spaces from beginning, end and in the middle of the string also.
ex:
... (4 Replies)