LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using sed in bash script to serach and replace special characters, and text (https://www.linuxquestions.org/questions/programming-9/using-sed-in-bash-script-to-serach-and-replace-special-characters-and-text-4175473421/)

PoltoX 08-14-2013 08:16 PM

Using sed in bash script to serach and replace special characters, and text
 
Hi guys. I want to run a bash script that searches for and replaces the following text/characters.:

:ns0 with :ll
ns0: with ll:
remove [position() >= 2]
remove [1],[2],[3] up tp [9]
<xsl:apply-templates select="ll: with <xsl:apply-templates select="/ll:WordML/ll:WordML/ll:

I have been trying for a good couple of hours now, but i just cant get it right. I do not have a lot of experience in scripting/programming, so be nice :)

Code:

#!/bin/bash
WordML="<xsl:apply-templates select="ll:WordML" />"
WordMLReplace="<xsl:apply-templates select="ll:WordML/ll:WordML" />"
sed -e "s|ns[0-9]:|ll:|g" -i file.xsl;
sed -e "s|:ns[0-9]|:ll|g" -i file.xsl;
sed -e "s|"${WordML}"|"${WordMLReplace}"|g" -i file.xsl;
sed -e "s|[[0-9]]||g" -i file.xsl;
sed -e "s|"[position() &gt;= 2]"||g" -i file.xsl;

The first 2 sed commands work fine, the other 3, not so much. I tried to define the variables and inserting them in sed, but no luck. Just putting them in as plain text in the sed command with "" or '' around then does not work.

As I said, not a programmer. If you know what's wrong, could you not just write the code, but maybe a short explanation too?

Thanks heaps!

danielbmartin 08-14-2013 08:44 PM

Help us to help you. Provide a sample input file (10-15 lines will do). Construct a sample output file which corresponds to your sample input and post both samples here. With "Before and After" examples we can better understand your needs and also judge if our proposed solution fills those needs.

Daniel B. Martin

Firerat 08-14-2013 08:58 PM

This file.xsl

Code:

:ns0 with :ll
ns0: with ll:
remove [position() &gt;= 2]
remove [1],[2],[3] up tp [9]
<xsl:apply-templates select="ll:WordML" /> with <xsl:apply-templates select="/ll:WordML/ll:WordML/ll:

script
Code:

#!/bin/bash
WordML='<xsl:apply-templates select="ll:WordML" />'
WordMLReplace='<xsl:apply-templates select="ll:WordML/ll:WordML" />'
sed -e "s|ns[0-9]:|ll:|g" \
    -e "s|:ns[0-9]|:ll|g" \
    -e "s|${WordML}|${WordMLReplace}|g" \
    -e "s|[[][0-9][]]||g" \
    -e "s|[[]position() &gt;= 2[]]||g" \
file.xsl

gives you
Code:

:ll with :ll
ll: with ll:
remove
remove ,, up tp
<xsl:apply-templates select="ll:WordML/ll:WordML" /> with <xsl:apply-templates select="/ll:WordML/ll:WordML/ll:

note the use of single quote (') with WordML...= along with not quotes 'in' the sed expr.

and [[] + []]
looks like an optical illusion ,. I swear it is as it make no sense


Ohh, and I strung them all together in one sed,
using \ for line continuation ( so shell thinks all one line )
left off the -i,

PoltoX 08-14-2013 09:22 PM

Thanks Firerat!

That's soo neat!

Just discovered that the file contains [position() &gt;= 3], [position() &gt;= 4] and so on up to 9(did not see this, it's like 10000 lines of code). So i just did the same as with ns[0-9]. [position() &gt;= [0-9]].

So the final code would be:

Code:

#!/bin/bash
WordML='<xsl:apply-templates select="ll:WordMLConveyance" />'
WordMLReplace='<xsl:apply-templates select="ll:WordMLConveyance/ll:WordMLConveyance" />'
sed -e "s|ns[0-9]:|ll:|g" \
    -e "s|:ns[0-9]|:ll|g" \
    -e "s|${WordML}|${WordMLReplace}|g" \
    -e "s|[[][0-9][]]||g" \
    -e "s|[[]position() &gt;= [0-9][]]||g" \
file.xsl


Just 1 last question, why leave out -i? Is it just good "scripting manners"?

Oh, sorry that I forgot to include some samples from the xsl file danielbmartin.

Firerat 08-14-2013 09:41 PM

Quote:

Originally Posted by PoltoX (Post 5009503)

Just 1 last question, why leave out -i? Is it just good "scripting manners"?

Oh, sorry that I forgot to include some samples from the xsl file danielbmartin.

I only add the -i once I know 'it works'
but I guess you test on a backup anyway..

and , talking of backup,
-i.backup-ext-of-your-pref

seeing as you have 10,000 lines, you could test with head or tail,
e.g.

tail -n 200 file.xsl | sed -e .....
just 'sample' last 200 lines

ntubski 08-14-2013 10:20 PM

Quote:

Originally Posted by Firerat (Post 5009499)
and [[] + []]
looks like an optical illusion ,. I swear it is as it make no sense

Might read better with backslash: [[] === \[, and []] === \]

Code:

#!/bin/bash
WordML='<xsl:apply-templates select="ll:WordML" />'
WordMLReplace='<xsl:apply-templates select="ll:WordML/ll:WordML" />'
sed -e "s|ns[0-9]:|ll:|g" \
    -e "s|:ns[0-9]|:ll|g" \
    -e "s|${WordML}|${WordMLReplace}|g" \
    -e "s|\[[0-9]\]||g" \
    -e "s|\[position() &gt;= [0-9]\]||g" \
    file.xsl


PoltoX 08-14-2013 10:25 PM

Thanks guys. really appreciate it! I'll keep you posted on my progress.

cheers


All times are GMT -5. The time now is 01:16 PM.