Regex for sed/awk command
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I would like to change the file content which i specified like below.I want to comment out all the lines which contains ".class" except the class name i provide.
@MyClass1.MyClass2(value =
Class1.class,
Class2.class,
Class3.class,
Class4.class
)
public class Foo
For example if i provide Class2 as parameter to
sed -i -E 's/some_regex/Class2/g'
command,the result should be like this,
@MyClass1.MyClass2(value =
//Class1.class,
Class2.class,
//Class3.class,
//Class4.class
)
public class Foo
What regex should i use for this?
text-processing awk sed regular-expression
New contributor
add a comment |
up vote
0
down vote
favorite
I would like to change the file content which i specified like below.I want to comment out all the lines which contains ".class" except the class name i provide.
@MyClass1.MyClass2(value =
Class1.class,
Class2.class,
Class3.class,
Class4.class
)
public class Foo
For example if i provide Class2 as parameter to
sed -i -E 's/some_regex/Class2/g'
command,the result should be like this,
@MyClass1.MyClass2(value =
//Class1.class,
Class2.class,
//Class3.class,
//Class4.class
)
public class Foo
What regex should i use for this?
text-processing awk sed regular-expression
New contributor
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I would like to change the file content which i specified like below.I want to comment out all the lines which contains ".class" except the class name i provide.
@MyClass1.MyClass2(value =
Class1.class,
Class2.class,
Class3.class,
Class4.class
)
public class Foo
For example if i provide Class2 as parameter to
sed -i -E 's/some_regex/Class2/g'
command,the result should be like this,
@MyClass1.MyClass2(value =
//Class1.class,
Class2.class,
//Class3.class,
//Class4.class
)
public class Foo
What regex should i use for this?
text-processing awk sed regular-expression
New contributor
I would like to change the file content which i specified like below.I want to comment out all the lines which contains ".class" except the class name i provide.
@MyClass1.MyClass2(value =
Class1.class,
Class2.class,
Class3.class,
Class4.class
)
public class Foo
For example if i provide Class2 as parameter to
sed -i -E 's/some_regex/Class2/g'
command,the result should be like this,
@MyClass1.MyClass2(value =
//Class1.class,
Class2.class,
//Class3.class,
//Class4.class
)
public class Foo
What regex should i use for this?
text-processing awk sed regular-expression
text-processing awk sed regular-expression
New contributor
New contributor
edited 2 days ago
Jeff Schaller
35.9k952119
35.9k952119
New contributor
asked 2 days ago
aekber
31
31
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
For a file like this:
$ cat file1
@MyClass1.MyClass2(value =
Class1.class,
Class2.class,
Class3.class,
Class4.class
)
public class Foo
@MyClass11.MyClass2(value =
Class1.class,
Class2.class,
Class3.class,
Class4.class
)
public class Foo2
You can have this result:
$ sed '/@MyClass1.MyClass2(*/,/})/s' file1
@MyClass1.MyClass2(value =
// Class1.class,
Class2.class,
// Class3.class,
// Class4.class
)
public class Foo
@MyClass11.MyClass2(value =
Class1.class,
Class2.class,
Class3.class,
Class4.class
)
public class Foo2
Explanation:
'/@MyClass1.MyClass2(*/,/})/ ....
--> range to operate in form /from/,/to/actions
to avoid escaping
//
edited 2 days ago
answered 2 days ago
George Vasiliou
5,44531028
5,44531028
1
I like how you removed the comment fromClass2
.
– unxnut
2 days ago
Great answer,but i am just wondering, what if i want to apply // in all lines ending with .class? What regex should i use instead of {s|sClass.*|//&|g; ?
– aekber
2 days ago
@aekber Maybe something likes|.class$|//&|g
. The use of$
is a regex anchor that means "end of line".
– George Vasiliou
yesterday
add a comment |
1
I like how you removed the comment fromClass2
.
– unxnut
2 days ago
Great answer,but i am just wondering, what if i want to apply // in all lines ending with .class? What regex should i use instead of {s|sClass.*|//&|g; ?
– aekber
2 days ago
@aekber Maybe something likes|.class$|//&|g
. The use of$
is a regex anchor that means "end of line".
– George Vasiliou
yesterday
1
1
I like how you removed the comment from
Class2
.– unxnut
2 days ago
I like how you removed the comment from
Class2
.– unxnut
2 days ago
Great answer,but i am just wondering, what if i want to apply // in all lines ending with .class? What regex should i use instead of {s|sClass.*|//&|g; ?
– aekber
2 days ago
Great answer,but i am just wondering, what if i want to apply // in all lines ending with .class? What regex should i use instead of {s|sClass.*|//&|g; ?
– aekber
2 days ago
@aekber Maybe something like
s|.class$|//&|g
. The use of $
is a regex anchor that means "end of line".– George Vasiliou
yesterday
@aekber Maybe something like
s|.class$|//&|g
. The use of $
is a regex anchor that means "end of line".– George Vasiliou
yesterday
add a comment |
up vote
1
down vote
awk '/.class/ && !/Class2/ print "//", $0
!(/.class/ && !/Class2/) print $0' filename
I only want to be changed the lines between this annotation @MyClass1.MyClass2(value = )
– aekber
2 days ago
add a comment |
up vote
1
down vote
awk '/.class/ && !/Class2/ print "//", $0
!(/.class/ && !/Class2/) print $0' filename
I only want to be changed the lines between this annotation @MyClass1.MyClass2(value = )
– aekber
2 days ago
add a comment |
up vote
1
down vote
up vote
1
down vote
awk '/.class/ && !/Class2/ print "//", $0
!(/.class/ && !/Class2/) print $0' filename
awk '/.class/ && !/Class2/ print "//", $0
!(/.class/ && !/Class2/) print $0' filename
answered 2 days ago
unxnut
3,5272918
3,5272918
I only want to be changed the lines between this annotation @MyClass1.MyClass2(value = )
– aekber
2 days ago
add a comment |
I only want to be changed the lines between this annotation @MyClass1.MyClass2(value = )
– aekber
2 days ago
I only want to be changed the lines between this annotation @MyClass1.MyClass2(value = )
– aekber
2 days ago
I only want to be changed the lines between this annotation @MyClass1.MyClass2(value = )
– aekber
2 days ago
add a comment |
aekber is a new contributor. Be nice, and check out our Code of Conduct.
aekber is a new contributor. Be nice, and check out our Code of Conduct.
aekber is a new contributor. Be nice, and check out our Code of Conduct.
aekber is a new contributor. Be nice, and check out our Code of Conduct.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482024%2fregex-for-sed-awk-command%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown