-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
  • 172,303
  • 28
  • 230
  • 274
AndriiZ
  • 41
  • 1
  • 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
  • 72,334
  • 12
  • 107
  • 141
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
  • 16,456
  • 2
  • 33
  • 46
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,240
  • 1
  • 16
  • 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
  • 6,388
  • 2
  • 16
  • 17