Reputation: 175
I am looking a method to extract a particular text from multiple parentheses and wanted to store in a file. The content looks like this.
{& Vendor CGIO} 1100 650} {{& IP_OWNER cjohn} 1100 550} {{& Product pk_sgmii_serdes_sx_ico_idac_sw_by2} 1100 450} {{& DATE_TIME Aug 29 03:27:36 2016} 1100 750} {{& Version 1.1} 1100 350} {{& PDK_RELEASE_VERSION V1} 1100 850}
I wanted to extract following and print into a file.
& Vendor CGIO
& IP_OWNER cjohn
& Product pk_sgmii_serdes_sx_ico_idac_sw_by2
& Version 1.1
I tried using cut command but its not so useful to extract all variables.
cat Tags_file | cut -d '{' -f2 | cut -d '}' -f1
Upvotes: 1
Views: 69
Reputation: 6185
First, change all {&
to newlines,
then get rid of the remaining noise (note that I removed the initial &
also):
$ echo "{& Vendor CGIO} 1100 650} {{& IP_OWNER cjohn} 1100 550} {{& Product pk_sgmii_serdes_sx_ico_idac_sw_by2} 1100 450} {{& DATE_TIME Aug 29 03:27:36 2016} 1100 750} {{& Version 1.1} 1100 350} {{& PDK_RELEASE_VERSION V1} 1100 850}"
| sed 's/{&/\n/g' | awk -F\} '{ print $1 }'
Vendor CGIO
IP_OWNER cjohn
Product pk_sgmii_serdes_sx_ico_idac_sw_by2
DATE_TIME Aug 29 03:27:36 2016
Version 1.1
PDK_RELEASE_VERSION V1
Upvotes: 1
Reputation: 1653
Try
grep -Poh '(?<={)& (?!DATE|PDK)[^}]+' Tags_file
What I got:
& Vendor CGIO
& IP_OWNER cjohn
& Product pk_sgmii_serdes_sx_ico_idac_sw_by2
& Version 1.1
Just what you needed. (excluded DATE_TIME
and PDK
as in your example)
Upvotes: 1
Reputation: 2589
Perhaps while
loop will also print the outputs:
my $str = '{& Vendor CGIO} 1100 650} {{& IP_OWNER cjohn} 1100 550} {{& Product pk_sgmii_serdes_sx_ico_idac_sw_by2} 1100 450} {{& DATE_TIME Aug 29 03:27:36 2016} 1100 750} {{& Version 1.1} 1100 350} {{& PDK_RELEASE_VERSION V1} 1100 850}';
print "$1\n" while($str=~m/\{([^\{\}]*)\}/g);
Upvotes: 1
Reputation: 785691
Using grep -oP
with lookaround based regex:
grep -oP '(?<={)[^{}]+(?=})' file
& Vendor CGIO
& IP_OWNER cjohn
& Product pk_sgmii_serdes_sx_ico_idac_sw_by2
& DATE_TIME Aug 29 03:27:36 2016
& Version 1.1
& PDK_RELEASE_VERSION V1
Upvotes: 3