0

Continuation of my initial question:

shell script taking input from python program

So I have standard output from my python program being stored in a variable in my shell script. Now, I have to parse this output so that I can take a substring of the last 22 characters. These are the only ones that matter to me. Unfortunately there's no way to really identify these last characters ("keyword=", etc.), meaning I have to do this completely by their position

1
  • 1
    would be helpful to show some sample output Commented Aug 30, 2012 at 23:53

2 Answers 2

1

Assuming you are using bash, you can use substring expansion:

key=$(python ...)   # From  your previous question

suffix=${key: -22}  # The space between : and - is important
Sign up to request clarification or add additional context in comments.

Comments

1

If you need the last N characters of a string, you can use a simple slice:

>>> v="this is the stdout from my shell command stored in a variable"
>>> v[-22:]
'd stored in a variable'

The last 22 chars are returned.

2 Comments

This is probably the best approach, but the 22 chars at the end are to be kept, so v[-22:] is more appropriate
@WilliamPursell you're right, I misread the question. I've updated the answer accordingly.

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.