Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using sed to extract a substring in curly brackets

Tags:

bash

sed

I've currently got a string as below:

integration@{Wed Nov 19 14:17:32 2014} branch: thebranch

This is contained in a file, and I parse the string. However I want the value between the brackets {Wed Nov 19 14:17:32 2014}

I have zero experience with Sed, and to be honest I find it a little cryptic.

So far I've managed to use the following command, however the output is still the entire string.

What am I doing wrong?

sed -e 's/[^/{]*"\([^/}]*\).*/\1/'
like image 932
TheMightyLlama Avatar asked Nov 21 '14 08:11

TheMightyLlama


2 Answers

This is very simple to do with awk, not complicate regex.

awk -F"{|}" '{print $2}' file
Wed Nov 19 14:17:32 2014

It sets the field separator to { or }, then your data will be in the second field.

FS could be set like this to:

awk -F"[{}]" '{print $2}' file

To see all field:

awk -F"{|}" '{print "field#1="$1"\nfield#2="$2"\nfield#3="$3}' file
field#1=integration@
field#2=Wed Nov 19 14:17:32 2014
field#3= branch: thebranch
like image 140
Jotne Avatar answered Oct 18 '22 08:10

Jotne


To get the values which was between {, }

$ sed 's/^[^{]*{\([^{}]*\)}.*/\1/' file
Wed Nov 19 14:17:32 2014
like image 28
Avinash Raj Avatar answered Oct 18 '22 08:10

Avinash Raj