sed escapes

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#21 Post by jpeps »

Moose On The Loose wrote: It seems worth the work to create an "escape" script that does all the changes to a string. Then any script that needs to not have a character taken to be a special character can use it. This would be like the thing in JavaScript.
Here's one I'm testing using Bash string manipulation:

Code: Select all

#!/bin/bash 

### SED script

VAR="$1"

   VAR="${VAR//\/\\}"
   VAR="${VAR//\//\/}"
   VAR="${VAR//\*/\*}"
   VAR="${VAR//\[/\[}"
   VAR="${VAR//\]/\]}"
   VAR="${VAR//\-/\-}"
   VAR="${VAR//\^/\^}"

 echo "$VAR"
Example:

VAR="/usr/bin/myfile-2.1[34]"
FILE=`SED "$VAR"`
sed "/${FILE}/d" ./list

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#22 Post by Moose On The Loose »

jpeps wrote:
Moose On The Loose wrote: It seems worth the work to create an "escape" script that does all the changes to a string. Then any script that needs to not have a character taken to be a special character can use it. This would be like the thing in JavaScript.
Here's one I'm testing using Bash string manipulation:

Code: Select all

#!/bin/bash 

### SED script

VAR="$1"

   VAR="${VAR//\/\\}"
   VAR="${VAR//\//\/}"
   VAR="${VAR//\*/\*}"
   VAR="${VAR//\[/\[}"
   VAR="${VAR//\]/\]}"
   VAR="${VAR//\-/\-}"
   VAR="${VAR//\^/\^}"

 echo "$VAR"
Example:

VAR="/usr/bin/myfile-2.1[34]"
FILE=`SED "$VAR"`
sed "/${FILE}/d" ./list
How about this

Code: Select all

# echo "This-is+a*string" | sed "s/\([-+*.]\)/\\\\\1/g"
It can replace several special characters in one go.

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#23 Post by jpeps »

Moose On The Loose wrote:
How about this

Code: Select all

# echo "This-is+a*string" | sed "s/\([-+*.]\)/\\\\\1/g"
It can replace several special characters in one go.
I had tried (something like) that, but didn't seem to work, although there's probably a way. Anyway, sed is slower and I couldn't get it to handle something like "" even though \\ is supposed to work.

edit: Found the problem.

echo "$VAR" | sed "s/\([-+*.]\)/\\\\\1/g"
This\-is\+a\*string

VAR=`"$VAR" | sed "s/\([-+*.]\)/\\\\\1/g"`
This\1is\1a\1string

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#24 Post by seaside »

Interesting....

This works-

Code: Select all

 VAR=$(echo "This-is+a*string" | sed "s/\([-+*.]\)/\\\\\1/g")
# echo $VAR
This\-is\+a\*string

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#25 Post by jpeps »

editing....

Back to using "$()" !!!

Sed doesn't recognize \+, so have to delete that from the script. The bash-replacement ran over 20% faster even with all the extra searches (I averaged over 5 trials for each).

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#26 Post by seaside »

jpeps wrote:editing....

Back to using "$()" !!!

Sed doesn't recognize \+, so have to delete that from the script. The bash-replacement ran over 20% faster even with all the extra searches (I averaged over 5 trials for each).
jpeps,

Is there a bash script to delete lines matching an expression that doesn't have to rewrite the file and runs faster than Sed or it does it run faster even though it rewrites?

Regards,
s

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#27 Post by jpeps »

seaside wrote:
jpeps wrote:editing....

Back to using "$()" !!!

Sed doesn't recognize \+, so have to delete that from the script. The bash-replacement ran over 20% faster even with all the extra searches (I averaged over 5 trials for each).
jpeps,

Is there a bash script to delete lines matching an expression that doesn't have to rewrite the file and runs faster than Sed or it does it run faster even though it rewrites?

Regards,
s
Hi seaside,
I'm just deleting a matched filename from a /tmp list that gets used to build a targz. I added a timer to a test script that didn't actually delete anything (sed vs sed -i). The only difference was the SED script, which used sed vs bash string manipulation.

Edit: actually there's quite a large standard deviation. Running more tests shrank the difference to almost nothing....and we're only talking milliseconds.

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#28 Post by Moose On The Loose »

seaside wrote:
jpeps wrote:editing....

Back to using "$()" !!!

Sed doesn't recognize \+, so have to delete that from the script. The bash-replacement ran over 20% faster even with all the extra searches (I averaged over 5 trials for each).
jpeps,

Is there a bash script to delete lines matching an expression that doesn't have to rewrite the file and runs faster than Sed or it does it run faster even though it rewrites?

Regards,
s
Is there a way to make bash do more than one item?

Code: Select all

# AA="-----"
# echo "${AA/-/\\-}"
\-----
It seems very ugly to have to code a loop or something

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#29 Post by technosaurus »

echo ${A//-/\\-/}
\-/\-/\-/\-/\-/
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#30 Post by Moose On The Loose »

technosaurus wrote:echo ${A//-/\\-/}
\-/\-/\-/\-/\-/
Yes, that does it nicely.
This allows us to make a more general version if needed

Code: Select all

 AA="++++///---"
# Z="+"
# echo ${AA/${Z}/\\${Z}}
\++++///---
# echo ${AA//${Z}/\\${Z}}
\+\+\+\+///---
# 

jpeps
Posts: 3179
Joined: Sat 31 May 2008, 19:00

#31 Post by jpeps »

Moose On The Loose wrote:
technosaurus wrote:echo ${A//-/\\-/}
\-/\-/\-/\-/\-/
Yes, that does it nicely.
...which is the script I already posted.

Post Reply