-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.

Avinash Raj
  • 166,785
  • 24
  • 204
  • 249
AndriiZ
  • 41
  • 1
  • 2

4 Answers4

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).

Tom Fenech
  • 69,051
  • 12
  • 96
  • 131
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
Guru
  • 15,464
  • 2
  • 31
  • 43
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
Arjun Mathew Dan
  • 5,125
  • 1
  • 15
  • 27
0
x='<timestamp> INFO   [Response--22] Code [00000000] Code was sent to [+0000002345]'
read -a fields <<<"$x"
code=${fields[4]//[][]/}
echo "code=$code"
pjh
  • 4,359
  • 14
  • 15