-1

I have string from log file about sending the code like:

 <timestamp> INFO   [Response--22] Code [00000000] Code was sent to [+0000002345]

Could you help me with parsing it using BASH to put the code value (8chars) to variable? Thank you for any help.

6
  • 2
    could you provide expected output? Commented Oct 24, 2014 at 8:09
  • Did you already tried something? Please detail your goal! Commented Oct 24, 2014 at 8:10
  • expected output is: 'code = 00000000' extracted from string "<timestamp> INFO [Response--22] Code [00000000] Code was sent to [+0000002345]" Commented Oct 24, 2014 at 8:12
  • You should edit your question to include the expected output. Commented Oct 24, 2014 at 8:19
  • 1
    @TomFenech I do wish there was better tooling for finding probable duplicates; if you can propose a better duplicate, please do. There are probably hundreds, but finding a good one is hard work. Commented Oct 24, 2014 at 8:31

4 Answers 4

0

You could use sed to extract the value:

sed 's/.*Code *\[\([0-9]*\)].*/\1/' <<<"<timestamp> INFO   [Response--22] Code [00000000] Code was sent to [+0000002345]"
00000000

If the line is in a file, you can do sed '...' filename. To store the output to a variable, you can do x=$(sed '...' filename).

Sign up to request clarification or add additional context in comments.

Comments

0
sed -r 's/(.*)(Code )\[([0-9]+)\](.*)/code = \3/'

Output:

sdlcb@ubuntu:~/AMD_C$ echo "<timestamp> INFO   [Response--22] Code [00000000] Code was sent to [+0000002345]" | sed -r 's/(.*)(Code )\[([0-9]+)\](.*)/code = \3/'
code = 00000000

Comments

0

If I understood your requirement correctly:

$ x="<timestamp> INFO   [Response--22] Code [00000000] Code was sent to [+0000002345]"
$ echo "$x" | sed -r 's/.*(Code )\[([0-9]+)\].*/\1= \2/'
Code = 00000000

3 Comments

You should quote "$x" when you echo it.
Thank you very much. Sorry for dump way of asking.
@TomFenech : Thanks tom for pointing it out. updated
0
x='<timestamp> INFO   [Response--22] Code [00000000] Code was sent to [+0000002345]'
read -a fields <<<"$x"
code=${fields[4]//[][]/}
echo "code=$code"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.