1

We are using some outdated encoded script which does not to be allowed to be modified. Therefor I though of a solution to modify the input automatically by using sed. This didn't work and after digging on forums I came up with the "solution" below, however it does not work. That or the output is not modified as expected.

What am I trying to achieve?

There are two kinds of inputs (so I will need two seperate rules/scripts for it). One is with a minus between every four (4) characters and the other is adding a minus between every six (6) characters.

Example 1 input:

0HU1L1LQXRFF2W8146LXX5WL

Needs to be formatted as:

0HU1-L1LQ-XRFF-2W81-46XX-L5WL

Example 2 input:

C00W00M9JN0777XT06XJHFGCM7Y195

Needs to be formatted as:

C00W00-M9JN07-77XT06-XJHFGC-M7Y195

I will be needed two seperate rules based on the above, which is not an issue for me.

So what I have tried so far?

After digging around here and on other forums I found the following command to work for both cases:

For example 1:

echo "0HU1L1LQXRFF2W8146LXX5WL" | fold -w4 | paste -sd'-' -
Result: 0HU1-L1LQ-XRFF-2W81-46XX-L5WL

Perfect!

For example 2:

echo "C00W00M9JN0777XT06XJHFGCM7Y195" | fold -w6 | paste -sd'-' -
Result: C00W00-M9JN07-77XT06-XJHFGC-M7Y195

Also perfect!

However...

I having a hard time to translate this into a bash script based on variables. I tried the following:

#!/bin/sh
example=C00W00M9JN0777XT06XJHFGCM7Y195
echo "Unedited:"
echo $example
echo " "
echo "Modified:"
stripped=($example | fold -w6 | paste -sd'-' -)
echo $stripped

With the above I am getting the following error: syntax error near unexpected token `|'

So I modified it by escaping the '|' like this:

example=C00W00M9JN0777XT06XJHFGCM7Y195
echo "Unedited:"
echo $example
echo " "
echo "Modified:"
stripped=($example \| fold -w6 \| paste -sd'-' -)
echo $stripped

I am not getting any errors with the above, but it's not working as it should. Both results are the same or better said the $stripped is without the minus (-) symbols in between.

Maybe someone can be so kind to tell me what I am doing wrong here and how I can fix this?

For the record; I don't need a "combined" script which adds minus (-) symbols between both 4 and 6 chars. Just either one and I will translate it to the other obvious.

Thank you kindly in advance.

1 Answer 1

3

In your tests, you used echo to send the value to standard output and thence the pipe, but you left that out in your script. You also need an extra $ to make it a command substitution.

stripped=$(echo $example | fold -w6 | paste -sd'-' -)

will give the desired result.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .