3

I'm trying to set up a script that parses a string.

My loop is currently

while [ -n "$RAW" ]; do
    // do some processing here
    RAW=$(echo $RAW| sed -r 's/^.{15}//')
done

However, the script never seems to end

0

3 Answers 3

3

It is not ending because the sed expression is not correct. It expects minimum 15 characters and does not work for anything less than 15 chars. Try this:

RAW=$(echo $RAW| sed -r 's/^.{0,15}//')
Sign up to request clarification or add additional context in comments.

1 Comment

+1. @user1772510, in bash, you should replace // to #. Second, the sed command didn't work properly, so RAW is never changed, that's why it goes infinite loop
2

Maybe you just want this:

#!/bin/bash
RAW=012345678901234567890
.
.
.
RAW=${RAW:15}
echo $RAW
567890

Comments

0

It might not end because of the logic inside your while loop.

You're doing to overwrite variable RAW:

RAW=$(echo $RAW| sed -r 's/^.{15}//')

Which means match and replace first 15 characters in original variable by empty string. What is there are only 10 characters left. In that sed won't match (and replace) and your varialbe RAW will remain at that value.

You probably want upto 15 characters from start to be replaced and if that's the case this is what you will need:

RAW=$(echo $RAW | sed -r 's/^.{1,15}//')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.