How can I print $1 and all matching words on line but no other words?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
The input looks like this:
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4 groupx groupy
user3 : MPT_group2 MPT_group3 groupn
I want to get back:
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
Thanks.
bash text-processing
add a comment |Â
up vote
0
down vote
favorite
The input looks like this:
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4 groupx groupy
user3 : MPT_group2 MPT_group3 groupn
I want to get back:
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
Thanks.
bash text-processing
1
What defines "matching" here?
â nohillside
Mar 23 at 18:45
any word with MPT_* in it.
â doneladio
Mar 23 at 22:29
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
The input looks like this:
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4 groupx groupy
user3 : MPT_group2 MPT_group3 groupn
I want to get back:
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
Thanks.
bash text-processing
The input looks like this:
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4 groupx groupy
user3 : MPT_group2 MPT_group3 groupn
I want to get back:
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
Thanks.
bash text-processing
edited Mar 23 at 18:37
Thomas
3,39741123
3,39741123
asked Mar 23 at 18:33
doneladio
31
31
1
What defines "matching" here?
â nohillside
Mar 23 at 18:45
any word with MPT_* in it.
â doneladio
Mar 23 at 22:29
add a comment |Â
1
What defines "matching" here?
â nohillside
Mar 23 at 18:45
any word with MPT_* in it.
â doneladio
Mar 23 at 22:29
1
1
What defines "matching" here?
â nohillside
Mar 23 at 18:45
What defines "matching" here?
â nohillside
Mar 23 at 18:45
any word with MPT_* in it.
â doneladio
Mar 23 at 22:29
any word with MPT_* in it.
â doneladio
Mar 23 at 22:29
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
0
down vote
An awk
solution:
$ awk 'printf "%s : ", $1; for( i=3; i<=NF; i++ ) if( $i ~ /^MPT_group/ ) printf "%s ", $i print "" ' input
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
To make that more readable, here is the bare awk
script portion:
printf "%s : ", $1;
for( i=3; i<=NF; i++ )
if( $i ~ /^MPT_group/ )
printf "%s ", $i
print ""
add a comment |Â
up vote
0
down vote
Awk
solution:
awk ' r=$1 OFS $2; for(i=3; i<=NF; i++) if ($i ~ /^MPT_group/) r=r OFS $i; print r ' file
The output:
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
Perfect. Thanks!
â doneladio
Mar 23 at 22:17
add a comment |Â
up vote
0
down vote
With grep
:
$ grep -Po '^S*s*: (MPT_groupd+s*)+' infile
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
An awk
solution:
$ awk 'printf "%s : ", $1; for( i=3; i<=NF; i++ ) if( $i ~ /^MPT_group/ ) printf "%s ", $i print "" ' input
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
To make that more readable, here is the bare awk
script portion:
printf "%s : ", $1;
for( i=3; i<=NF; i++ )
if( $i ~ /^MPT_group/ )
printf "%s ", $i
print ""
add a comment |Â
up vote
0
down vote
An awk
solution:
$ awk 'printf "%s : ", $1; for( i=3; i<=NF; i++ ) if( $i ~ /^MPT_group/ ) printf "%s ", $i print "" ' input
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
To make that more readable, here is the bare awk
script portion:
printf "%s : ", $1;
for( i=3; i<=NF; i++ )
if( $i ~ /^MPT_group/ )
printf "%s ", $i
print ""
add a comment |Â
up vote
0
down vote
up vote
0
down vote
An awk
solution:
$ awk 'printf "%s : ", $1; for( i=3; i<=NF; i++ ) if( $i ~ /^MPT_group/ ) printf "%s ", $i print "" ' input
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
To make that more readable, here is the bare awk
script portion:
printf "%s : ", $1;
for( i=3; i<=NF; i++ )
if( $i ~ /^MPT_group/ )
printf "%s ", $i
print ""
An awk
solution:
$ awk 'printf "%s : ", $1; for( i=3; i<=NF; i++ ) if( $i ~ /^MPT_group/ ) printf "%s ", $i print "" ' input
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
To make that more readable, here is the bare awk
script portion:
printf "%s : ", $1;
for( i=3; i<=NF; i++ )
if( $i ~ /^MPT_group/ )
printf "%s ", $i
print ""
answered Mar 23 at 18:45
DopeGhoti
40.2k54779
40.2k54779
add a comment |Â
add a comment |Â
up vote
0
down vote
Awk
solution:
awk ' r=$1 OFS $2; for(i=3; i<=NF; i++) if ($i ~ /^MPT_group/) r=r OFS $i; print r ' file
The output:
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
Perfect. Thanks!
â doneladio
Mar 23 at 22:17
add a comment |Â
up vote
0
down vote
Awk
solution:
awk ' r=$1 OFS $2; for(i=3; i<=NF; i++) if ($i ~ /^MPT_group/) r=r OFS $i; print r ' file
The output:
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
Perfect. Thanks!
â doneladio
Mar 23 at 22:17
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Awk
solution:
awk ' r=$1 OFS $2; for(i=3; i<=NF; i++) if ($i ~ /^MPT_group/) r=r OFS $i; print r ' file
The output:
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
Awk
solution:
awk ' r=$1 OFS $2; for(i=3; i<=NF; i++) if ($i ~ /^MPT_group/) r=r OFS $i; print r ' file
The output:
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
answered Mar 23 at 18:45
RomanPerekhrest
22.4k12144
22.4k12144
Perfect. Thanks!
â doneladio
Mar 23 at 22:17
add a comment |Â
Perfect. Thanks!
â doneladio
Mar 23 at 22:17
Perfect. Thanks!
â doneladio
Mar 23 at 22:17
Perfect. Thanks!
â doneladio
Mar 23 at 22:17
add a comment |Â
up vote
0
down vote
With grep
:
$ grep -Po '^S*s*: (MPT_groupd+s*)+' infile
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
add a comment |Â
up vote
0
down vote
With grep
:
$ grep -Po '^S*s*: (MPT_groupd+s*)+' infile
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
add a comment |Â
up vote
0
down vote
up vote
0
down vote
With grep
:
$ grep -Po '^S*s*: (MPT_groupd+s*)+' infile
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
With grep
:
$ grep -Po '^S*s*: (MPT_groupd+s*)+' infile
user1 : MPT_group1
user2 : MPT_group1 MPT_group3 MPT_group4
user3 : MPT_group2 MPT_group3
answered Mar 23 at 18:56
ñÃÂsýù÷
14.8k82462
14.8k82462
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f433119%2fhow-can-i-print-1-and-all-matching-words-on-line-but-no-other-words%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
What defines "matching" here?
â nohillside
Mar 23 at 18:45
any word with MPT_* in it.
â doneladio
Mar 23 at 22:29