Akhil
Akhil

Reputation: 81

Need to extract the word in between patterns

I have a file where it contains 30,000 lines below where I need to get the word in between 1st {}.

I tried this :

grep "{*}" out.txt

I got this as output:

 domain { CORE_0_RX_AcScan } -sync -module { tx20rx16_n7 }
 domain { CORE_RX_Charz_ft } -sync -module { tx20rx16_n7 }
 domain { CORE_dft0_RX_Update } -clo { CORE_0_RX_Shift } -module { i3_tx20rx16_n7 }

But I need the output as below (Need the words in between 1st {} ):

CORE_0_RX_AcScan 
CORE_RX_Charz_ft 
CORE_dft0_RX_Update

Upvotes: 0

Views: 59

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

You may use

sed -n 's/^domain *{ *\([[:alnum:]_]*\) *}.*/\1/p' out.txt > results.txt

See the online demo

Here, -n suppresses the default line output mode, ^domain *{ *\([[:alnum:]_]*\) *}.* matches domain at the start, then 0+ spaces, {, 0+ spaces, captures into Group 1 any 0 or more alphanumeric or _ chars, then again 0+ spaces, } and then any text to the end, and replaces the whole match with Group 1 value, and p prints this result only.

Or, with awk:

awk -F' *[{}] *' '{print $2}' out.txt > results.txt

Here, the field delimiter is a regex, see -F' *[{}] *'. It matches 0+ spaces, then { or } and then again 0+ spaces, only Column 2 values are output with {print $2}.

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133428

Could you please try following, tested and written with shown samples.

awk 'match($0,/{ [^}]*/){print substr($0,RSTART+2,RLENGTH-2)}' Input_file

2nd solution: considering that your Input_file is same as shown Input_file.

awk -F'domain { | }' '{print $2}' Input_file

3rd solution: With sed.

sed 's/^ domain { \([^ }]*\).*/\1/'  Input_file

Upvotes: 3

Related Questions