How to add text to end of line when pattern is matched?

Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
inputs:
line1 with the PATTERN that contains ( )
line2 with the PATTERN that contains ( )
lineN with the PATTERN that contains ( )
outputs:
line1 with the PATTERN that contains ( ) ;
line2 with the PATTERN that contains ( ) ;
...
lineN with the PATTERN that contains ( ) ;
I tried this:
find . -name "test.txt" -print | xargs sed -i "/PATTERN/ s/$)/); /g"
but it didn't work.
shell sed line-editor
add a comment |Â
up vote
8
down vote
favorite
inputs:
line1 with the PATTERN that contains ( )
line2 with the PATTERN that contains ( )
lineN with the PATTERN that contains ( )
outputs:
line1 with the PATTERN that contains ( ) ;
line2 with the PATTERN that contains ( ) ;
...
lineN with the PATTERN that contains ( ) ;
I tried this:
find . -name "test.txt" -print | xargs sed -i "/PATTERN/ s/$)/); /g"
but it didn't work.
shell sed line-editor
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
inputs:
line1 with the PATTERN that contains ( )
line2 with the PATTERN that contains ( )
lineN with the PATTERN that contains ( )
outputs:
line1 with the PATTERN that contains ( ) ;
line2 with the PATTERN that contains ( ) ;
...
lineN with the PATTERN that contains ( ) ;
I tried this:
find . -name "test.txt" -print | xargs sed -i "/PATTERN/ s/$)/); /g"
but it didn't work.
shell sed line-editor
inputs:
line1 with the PATTERN that contains ( )
line2 with the PATTERN that contains ( )
lineN with the PATTERN that contains ( )
outputs:
line1 with the PATTERN that contains ( ) ;
line2 with the PATTERN that contains ( ) ;
...
lineN with the PATTERN that contains ( ) ;
I tried this:
find . -name "test.txt" -print | xargs sed -i "/PATTERN/ s/$)/); /g"
but it didn't work.
shell sed line-editor
shell sed line-editor
edited May 25 '15 at 13:14
kenorb
7,756365103
7,756365103
asked Feb 28 '14 at 22:03
user3342338
90125
90125
add a comment |Â
add a comment |Â
6 Answers
6
active
oldest
votes
up vote
3
down vote
perl -ipe 's/$/;/ if /PATTERN/'
This will add a ; at the end if the line contains PATTERN.
add a comment |Â
up vote
3
down vote
The $ matches the end of the line, so your pattern should be )$ instead of $) like in your example.
Also, you don't need xargs here, it's safer to use the -exec flag of fine:
find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' +
If your version of find doesn't work with + at the end, then use ; instead (thank you @glenn-jackman):
find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' ;
Finally, there's no need for the g flag in a s/something$// idiom, as there is only one occurrence of $ per line.
1
You'll gain some efficiency with-exec ... +instead of-exec ... ;, if your find allows it.
â glenn jackman
Feb 28 '14 at 22:36
add a comment |Â
up vote
2
down vote
Assuming that PATTERN is actually ( ) and that something might go in between the ( ) and that they are not necessarily at the end of the line:
sed -i '/(.*)/ s/$/ ;/' test.txt
add a comment |Â
up vote
1
down vote
Using ex (which is equivalent to vi -e/vim -e).
One file:
ex +"g/local/s/$/;/g" -cwq foo.txt
All test.txt files recursively:
ex +"bufdo g/local/s/$/;/g" -cxa **/test.txt
Note: Make sure that globbing option (**) is enabled by: shopt -s globstar if your shell supports it.
Note: The :bufdo command is not POSIX.
note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
â Steven Penny
Apr 17 '16 at 0:43
add a comment |Â
up vote
0
down vote
Try:
sed --in-place '/PATTERN/s/.*/&;/' /path/to/file.txt
add a comment |Â
up vote
0
down vote
como seria en las lineas que no coicidan con la palabra
New contributor
jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
perl -ipe 's/$/;/ if /PATTERN/'
This will add a ; at the end if the line contains PATTERN.
add a comment |Â
up vote
3
down vote
perl -ipe 's/$/;/ if /PATTERN/'
This will add a ; at the end if the line contains PATTERN.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
perl -ipe 's/$/;/ if /PATTERN/'
This will add a ; at the end if the line contains PATTERN.
perl -ipe 's/$/;/ if /PATTERN/'
This will add a ; at the end if the line contains PATTERN.
answered Feb 28 '14 at 22:11
michas
14.7k33569
14.7k33569
add a comment |Â
add a comment |Â
up vote
3
down vote
The $ matches the end of the line, so your pattern should be )$ instead of $) like in your example.
Also, you don't need xargs here, it's safer to use the -exec flag of fine:
find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' +
If your version of find doesn't work with + at the end, then use ; instead (thank you @glenn-jackman):
find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' ;
Finally, there's no need for the g flag in a s/something$// idiom, as there is only one occurrence of $ per line.
1
You'll gain some efficiency with-exec ... +instead of-exec ... ;, if your find allows it.
â glenn jackman
Feb 28 '14 at 22:36
add a comment |Â
up vote
3
down vote
The $ matches the end of the line, so your pattern should be )$ instead of $) like in your example.
Also, you don't need xargs here, it's safer to use the -exec flag of fine:
find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' +
If your version of find doesn't work with + at the end, then use ; instead (thank you @glenn-jackman):
find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' ;
Finally, there's no need for the g flag in a s/something$// idiom, as there is only one occurrence of $ per line.
1
You'll gain some efficiency with-exec ... +instead of-exec ... ;, if your find allows it.
â glenn jackman
Feb 28 '14 at 22:36
add a comment |Â
up vote
3
down vote
up vote
3
down vote
The $ matches the end of the line, so your pattern should be )$ instead of $) like in your example.
Also, you don't need xargs here, it's safer to use the -exec flag of fine:
find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' +
If your version of find doesn't work with + at the end, then use ; instead (thank you @glenn-jackman):
find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' ;
Finally, there's no need for the g flag in a s/something$// idiom, as there is only one occurrence of $ per line.
The $ matches the end of the line, so your pattern should be )$ instead of $) like in your example.
Also, you don't need xargs here, it's safer to use the -exec flag of fine:
find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' +
If your version of find doesn't work with + at the end, then use ; instead (thank you @glenn-jackman):
find . -name test.txt -exec sed -i '/PATTERN/ s/)$/); /' '' ;
Finally, there's no need for the g flag in a s/something$// idiom, as there is only one occurrence of $ per line.
edited Apr 13 '17 at 12:36
Communityâ¦
1
1
answered Feb 28 '14 at 22:14
janos
6,92322247
6,92322247
1
You'll gain some efficiency with-exec ... +instead of-exec ... ;, if your find allows it.
â glenn jackman
Feb 28 '14 at 22:36
add a comment |Â
1
You'll gain some efficiency with-exec ... +instead of-exec ... ;, if your find allows it.
â glenn jackman
Feb 28 '14 at 22:36
1
1
You'll gain some efficiency with
-exec ... + instead of -exec ... ;, if your find allows it.â glenn jackman
Feb 28 '14 at 22:36
You'll gain some efficiency with
-exec ... + instead of -exec ... ;, if your find allows it.â glenn jackman
Feb 28 '14 at 22:36
add a comment |Â
up vote
2
down vote
Assuming that PATTERN is actually ( ) and that something might go in between the ( ) and that they are not necessarily at the end of the line:
sed -i '/(.*)/ s/$/ ;/' test.txt
add a comment |Â
up vote
2
down vote
Assuming that PATTERN is actually ( ) and that something might go in between the ( ) and that they are not necessarily at the end of the line:
sed -i '/(.*)/ s/$/ ;/' test.txt
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Assuming that PATTERN is actually ( ) and that something might go in between the ( ) and that they are not necessarily at the end of the line:
sed -i '/(.*)/ s/$/ ;/' test.txt
Assuming that PATTERN is actually ( ) and that something might go in between the ( ) and that they are not necessarily at the end of the line:
sed -i '/(.*)/ s/$/ ;/' test.txt
edited Mar 1 '14 at 0:15
answered Feb 28 '14 at 22:15
Graeme
24.4k46294
24.4k46294
add a comment |Â
add a comment |Â
up vote
1
down vote
Using ex (which is equivalent to vi -e/vim -e).
One file:
ex +"g/local/s/$/;/g" -cwq foo.txt
All test.txt files recursively:
ex +"bufdo g/local/s/$/;/g" -cxa **/test.txt
Note: Make sure that globbing option (**) is enabled by: shopt -s globstar if your shell supports it.
Note: The :bufdo command is not POSIX.
note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
â Steven Penny
Apr 17 '16 at 0:43
add a comment |Â
up vote
1
down vote
Using ex (which is equivalent to vi -e/vim -e).
One file:
ex +"g/local/s/$/;/g" -cwq foo.txt
All test.txt files recursively:
ex +"bufdo g/local/s/$/;/g" -cxa **/test.txt
Note: Make sure that globbing option (**) is enabled by: shopt -s globstar if your shell supports it.
Note: The :bufdo command is not POSIX.
note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
â Steven Penny
Apr 17 '16 at 0:43
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Using ex (which is equivalent to vi -e/vim -e).
One file:
ex +"g/local/s/$/;/g" -cwq foo.txt
All test.txt files recursively:
ex +"bufdo g/local/s/$/;/g" -cxa **/test.txt
Note: Make sure that globbing option (**) is enabled by: shopt -s globstar if your shell supports it.
Note: The :bufdo command is not POSIX.
Using ex (which is equivalent to vi -e/vim -e).
One file:
ex +"g/local/s/$/;/g" -cwq foo.txt
All test.txt files recursively:
ex +"bufdo g/local/s/$/;/g" -cxa **/test.txt
Note: Make sure that globbing option (**) is enabled by: shopt -s globstar if your shell supports it.
Note: The :bufdo command is not POSIX.
edited Nov 30 '17 at 13:12
answered May 25 '15 at 12:55
kenorb
7,756365103
7,756365103
note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
â Steven Penny
Apr 17 '16 at 0:43
add a comment |Â
note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
â Steven Penny
Apr 17 '16 at 0:43
note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
â Steven Penny
Apr 17 '16 at 0:43
note bufdo is not POSIX pubs.opengroup.org/onlinepubs/9699919799/utilities/ex.html
â Steven Penny
Apr 17 '16 at 0:43
add a comment |Â
up vote
0
down vote
Try:
sed --in-place '/PATTERN/s/.*/&;/' /path/to/file.txt
add a comment |Â
up vote
0
down vote
Try:
sed --in-place '/PATTERN/s/.*/&;/' /path/to/file.txt
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Try:
sed --in-place '/PATTERN/s/.*/&;/' /path/to/file.txt
Try:
sed --in-place '/PATTERN/s/.*/&;/' /path/to/file.txt
answered Feb 28 '14 at 22:05
DopeGhoti
41.6k55180
41.6k55180
add a comment |Â
add a comment |Â
up vote
0
down vote
como seria en las lineas que no coicidan con la palabra
New contributor
jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
0
down vote
como seria en las lineas que no coicidan con la palabra
New contributor
jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
como seria en las lineas que no coicidan con la palabra
New contributor
jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
como seria en las lineas que no coicidan con la palabra
New contributor
jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 8 mins ago
jean cesar quintanilla rivera
1
1
New contributor
jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
jean cesar quintanilla rivera is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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%2f117570%2fhow-to-add-text-to-end-of-line-when-pattern-is-matched%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