Issue
I have a file that has the following contents and many more.
#set_property board_part my.biz:ab0860_1cf:part0:1.0 [current_project]
set_property board_part my.biz:ab0820_1ab:part0:1.0 [current_project]
My ideal output is as shown below (ie, the text after the first ":" and the second ":".
ab0820_1ab
I generally use python and use regular expression along the lines of below to get the result.
\s*set_property board_part trenz.biz:([a-zA-Z_0-9]+)
I wish to know how can it be done quickly and in a more generic way using commandline tools (sed, awk).
Solution
Your example data has my.biz but your pattern tries to match trenz.biz
If gnu awk
is available, you can use the capture group and then print the first value of which is available in a[1]
awk 'match($0, /^\s*set_property board_part \w+\.biz:(\w+)/, a) {print a[1]}' file
The pattern matches:
^
Start of string\s*
Match optional whitespace charsset_property board_part
Match literally\w+\.biz:
Match 1+ word chars followed by.biz
(note to escape the dot to match it literally)(\w+)
Capture group 1, match 1+ word chars
Notes
- If you just want to match
trenz.biz
then you can replace\w+\.biz
withtrenz\.biz
- If the strings are not at the start of the string, you can change
^
for\s
wo match a whitespace char instead
Output
ab0820_1ab
Answered By - The fourth bird Answer Checked By - Clifford M. (WPSolving Volunteer)