Yes, this works, but it is horribly inefficient. Consider these two scripts which both chop the last character from the string stored in 'foo' and assign it to 'x':
and
The first, using shell string manipulation, takes about 2/100ths of a second to execute:
while the second script takes more than 5 seconds:
If I've done my math correctly, that's about 2500 times slower, just to do this one assignment. Imagine if the script needed to chop 100 strings in its loop and did them all this way?
You'll note that the majority of the time in the second script is system time -- that's the time that bash had to wait on the system to start each sed process and deal with the I/O from sed back to bash.
Last edited by agama; 02-18-2012 at 09:46 AM..
Reason: clarification in text
We can achieve that easily with traditional POSIX filters .
Quote:
Originally Posted by balajesuri
In my opinion, agama's solution is the best of the bunch. It doesn't require any external tools and uses only a standard sh parameter expansion operation.
However, the use of echo in each solution makes is unsuitable for general purpose usage. If, $foo or ${foo%?} were to expand to something beginning with a leading dash, something resembling an option, the pipeline could give an unexpected result.
I suggest using printf %s "${foo%?}" in lieu of echo.
I've tried to delete but for whatever reason I am not able to. I didn't want to post this here but I can not even send a pm about this... kind of annoying...
Hi,
I am new in bash scripting. In my work, I provide support to several users and when I connect to their computers I use the same admin and password, so I am trying to create a script that will only ask me for the IP address and then connect to the computer without having me to type the user... (5 Replies)
hello !
I have to remove string between a number and set of characters. For example,
35818 -stress - - -stress - - - - - - DB-3754
44412 caul kid notify DB-3747
54432 roberto -, notify DB-3725
55522 aws _ _int _ _classified 2_a _a 2_m _m 2_classified 2_search... (7 Replies)
I am outputting a line like this
print $2 "/" $4The last character though is a ":" and I want to remove it. Is there any neat way to remove it? Or am I forced to do something like this:
print $2 "/" substr($4, 1, length($4) - 1)Thanks. (6 Replies)
How can i remove the first and last character of strings like below:
"^^^613*"
"admt130"
"^^^613*"
"123456"
"adg8484"
"DQitYV09dh1C"
Means i wanna remove the quotes("").
Please help (17 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)
Hello!
Please bare with me, I'm a total newbie to scripting. Here's the sudo code of what I'm trying to do:
Get file name
Does file exist?
If true
get length of file name
get network id (this will be the last 3 numbers of the file name)
loop x 2
If... (1 Reply)
Hello,
The last character is a comma ,
I have tried the following:
sed -e 's/\,$//' filename-to-read
however - there are still commas at the end of each line...:confused: (5 Replies)
Hi all,
Does anyone know how to code in ksh that will remove the first character in a string variable and replace that variable without the first character?
Example:
var1=ktest1 will become var1=test1
var2=rtest2 will become var2=test2
Need help please. (10 Replies)
hi
I have a list of words in a text file. these words are appended by "." at their end. They look something like this.
word1.
word2.
word3.
word4.
word5.
I need to remove the last character "." from all the words. The output must look something like this.
word1
word2
word3... (7 Replies)
Hi,
I've made a script to grep a file
for i in `cat filename.txt`
do
strings ./binfile | grep "$i" 2>&1 > /dev/null
done
this works fine as long as in filename.txt i don't have any entries with spaces. But in my case i want to grep something with spaces like "lala tata"
and... (3 Replies)