Move lines matching a pattern from one file to another
I want to move lines matching certain pattern from file1 to file2. Analogous to operation cut and paste from one file to another in windows
Example
let's say I want to cut all lines containg bar
from file1 and paste it into newly created file2
Input:
file1
bla foo bla
bla bar bla
bla aaa bla
bla bar bla
bla foo bla
Desired output after processing:
file1
bla foo bla
bla aaa bla
bla foo bla
file2
bla bar bla
bla bar bla
What I have tried
grep
creates desired file2 but doesn't modify file1
grep 'bar' file1 > file2
sed -i
modifies desired file1 but doesn't create file2
sed -i '/bar/d' file1
If I execute both commands one after another, I get desired result. But here I am looking for a single line command out of curiosity and to make a script more concise.
Your help would be appreciated.
linux perl sed awk grep
add a comment |
I want to move lines matching certain pattern from file1 to file2. Analogous to operation cut and paste from one file to another in windows
Example
let's say I want to cut all lines containg bar
from file1 and paste it into newly created file2
Input:
file1
bla foo bla
bla bar bla
bla aaa bla
bla bar bla
bla foo bla
Desired output after processing:
file1
bla foo bla
bla aaa bla
bla foo bla
file2
bla bar bla
bla bar bla
What I have tried
grep
creates desired file2 but doesn't modify file1
grep 'bar' file1 > file2
sed -i
modifies desired file1 but doesn't create file2
sed -i '/bar/d' file1
If I execute both commands one after another, I get desired result. But here I am looking for a single line command out of curiosity and to make a script more concise.
Your help would be appreciated.
linux perl sed awk grep
Why-1
? No comment? One who did that should show how to solve this in single line command before down voting!
– jkshah
Oct 19 '13 at 11:35
Vote to close without providing any reason or comment?! That's too much.
– jkshah
Oct 19 '13 at 13:09
add a comment |
I want to move lines matching certain pattern from file1 to file2. Analogous to operation cut and paste from one file to another in windows
Example
let's say I want to cut all lines containg bar
from file1 and paste it into newly created file2
Input:
file1
bla foo bla
bla bar bla
bla aaa bla
bla bar bla
bla foo bla
Desired output after processing:
file1
bla foo bla
bla aaa bla
bla foo bla
file2
bla bar bla
bla bar bla
What I have tried
grep
creates desired file2 but doesn't modify file1
grep 'bar' file1 > file2
sed -i
modifies desired file1 but doesn't create file2
sed -i '/bar/d' file1
If I execute both commands one after another, I get desired result. But here I am looking for a single line command out of curiosity and to make a script more concise.
Your help would be appreciated.
linux perl sed awk grep
I want to move lines matching certain pattern from file1 to file2. Analogous to operation cut and paste from one file to another in windows
Example
let's say I want to cut all lines containg bar
from file1 and paste it into newly created file2
Input:
file1
bla foo bla
bla bar bla
bla aaa bla
bla bar bla
bla foo bla
Desired output after processing:
file1
bla foo bla
bla aaa bla
bla foo bla
file2
bla bar bla
bla bar bla
What I have tried
grep
creates desired file2 but doesn't modify file1
grep 'bar' file1 > file2
sed -i
modifies desired file1 but doesn't create file2
sed -i '/bar/d' file1
If I execute both commands one after another, I get desired result. But here I am looking for a single line command out of curiosity and to make a script more concise.
Your help would be appreciated.
linux perl sed awk grep
linux perl sed awk grep
edited Oct 19 '13 at 11:00
jkshah
asked Oct 19 '13 at 10:41
jkshahjkshah
8,95132539
8,95132539
Why-1
? No comment? One who did that should show how to solve this in single line command before down voting!
– jkshah
Oct 19 '13 at 11:35
Vote to close without providing any reason or comment?! That's too much.
– jkshah
Oct 19 '13 at 13:09
add a comment |
Why-1
? No comment? One who did that should show how to solve this in single line command before down voting!
– jkshah
Oct 19 '13 at 11:35
Vote to close without providing any reason or comment?! That's too much.
– jkshah
Oct 19 '13 at 13:09
Why
-1
? No comment? One who did that should show how to solve this in single line command before down voting!– jkshah
Oct 19 '13 at 11:35
Why
-1
? No comment? One who did that should show how to solve this in single line command before down voting!– jkshah
Oct 19 '13 at 11:35
Vote to close without providing any reason or comment?! That's too much.
– jkshah
Oct 19 '13 at 13:09
Vote to close without providing any reason or comment?! That's too much.
– jkshah
Oct 19 '13 at 13:09
add a comment |
5 Answers
5
active
oldest
votes
This might work for you (GNU sed):
sed -i -e '/bar/{w file2' -e 'd}' file1
An alternative:
sed -i -e '/bar/w file2' -e '//d' file1
To append to file2, write to a temporary file and use cat to append at the end of file in a bash script, or use:
sed -i -e '/bar/w tmpfile' -e '$e cat tmpfile >> file2 && rm tmpfile' -e '//d' file1
N.B. For the last solution, only one input file can be modified at a time.
It did the trick! Magical use ofw
andd
with{}
. Thanks a lot..
– jkshah
Oct 19 '13 at 22:48
Only thing that this can't do is append lines to file2, it insists on overwriting.
– Tim
Nov 25 '18 at 8:54
add a comment |
You can use perl and select a different filehandle based in a match of a regular expression when printing:
perl -i.bak -ne 'BEGIN { open $oh, q|>|, pop or die } { print { m/bar/ ? $oh : q|ARGVOUT| } $_ }' file1 file2
It yields:
==> file1 <==
bla foo bla
bla aaa bla
bla foo bla
==> file2 <==
bla bar bla
bla bar bla
Thanks for your response. Unfortunately no success with following errorCan't rename file1 to file1.bak: Text file busy, skipping file.
I think it's trying to modifyfile1
while being processed. I wonder how it worked for you and what's going wrong on my system!
– jkshah
Oct 19 '13 at 11:14
1
What OS are you using. I have seen this error message running under Linux but when the file is on a virtual box shared filesystem.
– justintime
Oct 19 '13 at 12:45
@justintime That's very much true. I was trying with Ubuntu on virtual box! Now tried on standalone system and it worked! Thanks a lot.
– jkshah
Oct 19 '13 at 13:11
add a comment |
This awk
script will do the trick:
awk '{a[NR]=$0}END{for(i=1;i<=NR;i++)print a[i] > "file"(a[i]~/bar/?2:1)}' file1
Outputs:
$ cat file1
bla foo bla
bla aaa bla
bla foo bla
$ cat file2
bla bar bla
bla bar bla
1
That's awesome! Did the trick. I shortened it a bitawk '{print $0 > ($0~/bar/?"file2":"file1")}' file1
. Thanks a lot!
– jkshah
Oct 19 '13 at 11:44
Not sure why you createda[i]
. simple$0
worked for me. Am I missing something?
– jkshah
Oct 19 '13 at 11:45
add a comment |
You can put a &&
between the two commands to make them a single line. But that won't be more readable, so I don't recommend you do that.
To do the same in one command, you would need something that can edit a file in-place, removing lines you don't want and at the same time printing those lines to stdout
or stderr
so you could redirect it to the other file.
Maybe ed
can do this but I don't know how to write that.
I don't think there is another "standard" UNIX tool to do this.
Btw, cut & paste actually has 3 steps:
- Copy selected text to clipboard
- Remove from original file
- Paste from clipboard to new file
The 2-step UNIX command does it without a clipboard.
add a comment |
I like the other answers, but just an (obvious) alternative approach in case you are worried about making a mistake in your pattern and want an easy way to rollback:
grep 'bar' file1 > file2
grep -v 'bar' file1 > file3
Once you're happy with your results (hint: tee
instead of >
will allow you to see what's getting written to your files):
mv file3 file1
Generally I prefer perl to do substitution like in the above answer, but when the regex idiom gets too complicated and you are semi-blindly copying and pasting commands you don't understand, you're not in control of your precious data.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2fstackoverflow.com%2fquestions%2f19465127%2fmove-lines-matching-a-pattern-from-one-file-to-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
This might work for you (GNU sed):
sed -i -e '/bar/{w file2' -e 'd}' file1
An alternative:
sed -i -e '/bar/w file2' -e '//d' file1
To append to file2, write to a temporary file and use cat to append at the end of file in a bash script, or use:
sed -i -e '/bar/w tmpfile' -e '$e cat tmpfile >> file2 && rm tmpfile' -e '//d' file1
N.B. For the last solution, only one input file can be modified at a time.
It did the trick! Magical use ofw
andd
with{}
. Thanks a lot..
– jkshah
Oct 19 '13 at 22:48
Only thing that this can't do is append lines to file2, it insists on overwriting.
– Tim
Nov 25 '18 at 8:54
add a comment |
This might work for you (GNU sed):
sed -i -e '/bar/{w file2' -e 'd}' file1
An alternative:
sed -i -e '/bar/w file2' -e '//d' file1
To append to file2, write to a temporary file and use cat to append at the end of file in a bash script, or use:
sed -i -e '/bar/w tmpfile' -e '$e cat tmpfile >> file2 && rm tmpfile' -e '//d' file1
N.B. For the last solution, only one input file can be modified at a time.
It did the trick! Magical use ofw
andd
with{}
. Thanks a lot..
– jkshah
Oct 19 '13 at 22:48
Only thing that this can't do is append lines to file2, it insists on overwriting.
– Tim
Nov 25 '18 at 8:54
add a comment |
This might work for you (GNU sed):
sed -i -e '/bar/{w file2' -e 'd}' file1
An alternative:
sed -i -e '/bar/w file2' -e '//d' file1
To append to file2, write to a temporary file and use cat to append at the end of file in a bash script, or use:
sed -i -e '/bar/w tmpfile' -e '$e cat tmpfile >> file2 && rm tmpfile' -e '//d' file1
N.B. For the last solution, only one input file can be modified at a time.
This might work for you (GNU sed):
sed -i -e '/bar/{w file2' -e 'd}' file1
An alternative:
sed -i -e '/bar/w file2' -e '//d' file1
To append to file2, write to a temporary file and use cat to append at the end of file in a bash script, or use:
sed -i -e '/bar/w tmpfile' -e '$e cat tmpfile >> file2 && rm tmpfile' -e '//d' file1
N.B. For the last solution, only one input file can be modified at a time.
edited Nov 25 '18 at 12:46
answered Oct 19 '13 at 20:30
potongpotong
36.1k43062
36.1k43062
It did the trick! Magical use ofw
andd
with{}
. Thanks a lot..
– jkshah
Oct 19 '13 at 22:48
Only thing that this can't do is append lines to file2, it insists on overwriting.
– Tim
Nov 25 '18 at 8:54
add a comment |
It did the trick! Magical use ofw
andd
with{}
. Thanks a lot..
– jkshah
Oct 19 '13 at 22:48
Only thing that this can't do is append lines to file2, it insists on overwriting.
– Tim
Nov 25 '18 at 8:54
It did the trick! Magical use of
w
and d
with {}
. Thanks a lot..– jkshah
Oct 19 '13 at 22:48
It did the trick! Magical use of
w
and d
with {}
. Thanks a lot..– jkshah
Oct 19 '13 at 22:48
Only thing that this can't do is append lines to file2, it insists on overwriting.
– Tim
Nov 25 '18 at 8:54
Only thing that this can't do is append lines to file2, it insists on overwriting.
– Tim
Nov 25 '18 at 8:54
add a comment |
You can use perl and select a different filehandle based in a match of a regular expression when printing:
perl -i.bak -ne 'BEGIN { open $oh, q|>|, pop or die } { print { m/bar/ ? $oh : q|ARGVOUT| } $_ }' file1 file2
It yields:
==> file1 <==
bla foo bla
bla aaa bla
bla foo bla
==> file2 <==
bla bar bla
bla bar bla
Thanks for your response. Unfortunately no success with following errorCan't rename file1 to file1.bak: Text file busy, skipping file.
I think it's trying to modifyfile1
while being processed. I wonder how it worked for you and what's going wrong on my system!
– jkshah
Oct 19 '13 at 11:14
1
What OS are you using. I have seen this error message running under Linux but when the file is on a virtual box shared filesystem.
– justintime
Oct 19 '13 at 12:45
@justintime That's very much true. I was trying with Ubuntu on virtual box! Now tried on standalone system and it worked! Thanks a lot.
– jkshah
Oct 19 '13 at 13:11
add a comment |
You can use perl and select a different filehandle based in a match of a regular expression when printing:
perl -i.bak -ne 'BEGIN { open $oh, q|>|, pop or die } { print { m/bar/ ? $oh : q|ARGVOUT| } $_ }' file1 file2
It yields:
==> file1 <==
bla foo bla
bla aaa bla
bla foo bla
==> file2 <==
bla bar bla
bla bar bla
Thanks for your response. Unfortunately no success with following errorCan't rename file1 to file1.bak: Text file busy, skipping file.
I think it's trying to modifyfile1
while being processed. I wonder how it worked for you and what's going wrong on my system!
– jkshah
Oct 19 '13 at 11:14
1
What OS are you using. I have seen this error message running under Linux but when the file is on a virtual box shared filesystem.
– justintime
Oct 19 '13 at 12:45
@justintime That's very much true. I was trying with Ubuntu on virtual box! Now tried on standalone system and it worked! Thanks a lot.
– jkshah
Oct 19 '13 at 13:11
add a comment |
You can use perl and select a different filehandle based in a match of a regular expression when printing:
perl -i.bak -ne 'BEGIN { open $oh, q|>|, pop or die } { print { m/bar/ ? $oh : q|ARGVOUT| } $_ }' file1 file2
It yields:
==> file1 <==
bla foo bla
bla aaa bla
bla foo bla
==> file2 <==
bla bar bla
bla bar bla
You can use perl and select a different filehandle based in a match of a regular expression when printing:
perl -i.bak -ne 'BEGIN { open $oh, q|>|, pop or die } { print { m/bar/ ? $oh : q|ARGVOUT| } $_ }' file1 file2
It yields:
==> file1 <==
bla foo bla
bla aaa bla
bla foo bla
==> file2 <==
bla bar bla
bla bar bla
answered Oct 19 '13 at 10:59
BireiBirei
31.1k25773
31.1k25773
Thanks for your response. Unfortunately no success with following errorCan't rename file1 to file1.bak: Text file busy, skipping file.
I think it's trying to modifyfile1
while being processed. I wonder how it worked for you and what's going wrong on my system!
– jkshah
Oct 19 '13 at 11:14
1
What OS are you using. I have seen this error message running under Linux but when the file is on a virtual box shared filesystem.
– justintime
Oct 19 '13 at 12:45
@justintime That's very much true. I was trying with Ubuntu on virtual box! Now tried on standalone system and it worked! Thanks a lot.
– jkshah
Oct 19 '13 at 13:11
add a comment |
Thanks for your response. Unfortunately no success with following errorCan't rename file1 to file1.bak: Text file busy, skipping file.
I think it's trying to modifyfile1
while being processed. I wonder how it worked for you and what's going wrong on my system!
– jkshah
Oct 19 '13 at 11:14
1
What OS are you using. I have seen this error message running under Linux but when the file is on a virtual box shared filesystem.
– justintime
Oct 19 '13 at 12:45
@justintime That's very much true. I was trying with Ubuntu on virtual box! Now tried on standalone system and it worked! Thanks a lot.
– jkshah
Oct 19 '13 at 13:11
Thanks for your response. Unfortunately no success with following error
Can't rename file1 to file1.bak: Text file busy, skipping file.
I think it's trying to modify file1
while being processed. I wonder how it worked for you and what's going wrong on my system!– jkshah
Oct 19 '13 at 11:14
Thanks for your response. Unfortunately no success with following error
Can't rename file1 to file1.bak: Text file busy, skipping file.
I think it's trying to modify file1
while being processed. I wonder how it worked for you and what's going wrong on my system!– jkshah
Oct 19 '13 at 11:14
1
1
What OS are you using. I have seen this error message running under Linux but when the file is on a virtual box shared filesystem.
– justintime
Oct 19 '13 at 12:45
What OS are you using. I have seen this error message running under Linux but when the file is on a virtual box shared filesystem.
– justintime
Oct 19 '13 at 12:45
@justintime That's very much true. I was trying with Ubuntu on virtual box! Now tried on standalone system and it worked! Thanks a lot.
– jkshah
Oct 19 '13 at 13:11
@justintime That's very much true. I was trying with Ubuntu on virtual box! Now tried on standalone system and it worked! Thanks a lot.
– jkshah
Oct 19 '13 at 13:11
add a comment |
This awk
script will do the trick:
awk '{a[NR]=$0}END{for(i=1;i<=NR;i++)print a[i] > "file"(a[i]~/bar/?2:1)}' file1
Outputs:
$ cat file1
bla foo bla
bla aaa bla
bla foo bla
$ cat file2
bla bar bla
bla bar bla
1
That's awesome! Did the trick. I shortened it a bitawk '{print $0 > ($0~/bar/?"file2":"file1")}' file1
. Thanks a lot!
– jkshah
Oct 19 '13 at 11:44
Not sure why you createda[i]
. simple$0
worked for me. Am I missing something?
– jkshah
Oct 19 '13 at 11:45
add a comment |
This awk
script will do the trick:
awk '{a[NR]=$0}END{for(i=1;i<=NR;i++)print a[i] > "file"(a[i]~/bar/?2:1)}' file1
Outputs:
$ cat file1
bla foo bla
bla aaa bla
bla foo bla
$ cat file2
bla bar bla
bla bar bla
1
That's awesome! Did the trick. I shortened it a bitawk '{print $0 > ($0~/bar/?"file2":"file1")}' file1
. Thanks a lot!
– jkshah
Oct 19 '13 at 11:44
Not sure why you createda[i]
. simple$0
worked for me. Am I missing something?
– jkshah
Oct 19 '13 at 11:45
add a comment |
This awk
script will do the trick:
awk '{a[NR]=$0}END{for(i=1;i<=NR;i++)print a[i] > "file"(a[i]~/bar/?2:1)}' file1
Outputs:
$ cat file1
bla foo bla
bla aaa bla
bla foo bla
$ cat file2
bla bar bla
bla bar bla
This awk
script will do the trick:
awk '{a[NR]=$0}END{for(i=1;i<=NR;i++)print a[i] > "file"(a[i]~/bar/?2:1)}' file1
Outputs:
$ cat file1
bla foo bla
bla aaa bla
bla foo bla
$ cat file2
bla bar bla
bla bar bla
answered Oct 19 '13 at 11:26
Chris SeymourChris Seymour
62.8k20124162
62.8k20124162
1
That's awesome! Did the trick. I shortened it a bitawk '{print $0 > ($0~/bar/?"file2":"file1")}' file1
. Thanks a lot!
– jkshah
Oct 19 '13 at 11:44
Not sure why you createda[i]
. simple$0
worked for me. Am I missing something?
– jkshah
Oct 19 '13 at 11:45
add a comment |
1
That's awesome! Did the trick. I shortened it a bitawk '{print $0 > ($0~/bar/?"file2":"file1")}' file1
. Thanks a lot!
– jkshah
Oct 19 '13 at 11:44
Not sure why you createda[i]
. simple$0
worked for me. Am I missing something?
– jkshah
Oct 19 '13 at 11:45
1
1
That's awesome! Did the trick. I shortened it a bit
awk '{print $0 > ($0~/bar/?"file2":"file1")}' file1
. Thanks a lot!– jkshah
Oct 19 '13 at 11:44
That's awesome! Did the trick. I shortened it a bit
awk '{print $0 > ($0~/bar/?"file2":"file1")}' file1
. Thanks a lot!– jkshah
Oct 19 '13 at 11:44
Not sure why you created
a[i]
. simple $0
worked for me. Am I missing something?– jkshah
Oct 19 '13 at 11:45
Not sure why you created
a[i]
. simple $0
worked for me. Am I missing something?– jkshah
Oct 19 '13 at 11:45
add a comment |
You can put a &&
between the two commands to make them a single line. But that won't be more readable, so I don't recommend you do that.
To do the same in one command, you would need something that can edit a file in-place, removing lines you don't want and at the same time printing those lines to stdout
or stderr
so you could redirect it to the other file.
Maybe ed
can do this but I don't know how to write that.
I don't think there is another "standard" UNIX tool to do this.
Btw, cut & paste actually has 3 steps:
- Copy selected text to clipboard
- Remove from original file
- Paste from clipboard to new file
The 2-step UNIX command does it without a clipboard.
add a comment |
You can put a &&
between the two commands to make them a single line. But that won't be more readable, so I don't recommend you do that.
To do the same in one command, you would need something that can edit a file in-place, removing lines you don't want and at the same time printing those lines to stdout
or stderr
so you could redirect it to the other file.
Maybe ed
can do this but I don't know how to write that.
I don't think there is another "standard" UNIX tool to do this.
Btw, cut & paste actually has 3 steps:
- Copy selected text to clipboard
- Remove from original file
- Paste from clipboard to new file
The 2-step UNIX command does it without a clipboard.
add a comment |
You can put a &&
between the two commands to make them a single line. But that won't be more readable, so I don't recommend you do that.
To do the same in one command, you would need something that can edit a file in-place, removing lines you don't want and at the same time printing those lines to stdout
or stderr
so you could redirect it to the other file.
Maybe ed
can do this but I don't know how to write that.
I don't think there is another "standard" UNIX tool to do this.
Btw, cut & paste actually has 3 steps:
- Copy selected text to clipboard
- Remove from original file
- Paste from clipboard to new file
The 2-step UNIX command does it without a clipboard.
You can put a &&
between the two commands to make them a single line. But that won't be more readable, so I don't recommend you do that.
To do the same in one command, you would need something that can edit a file in-place, removing lines you don't want and at the same time printing those lines to stdout
or stderr
so you could redirect it to the other file.
Maybe ed
can do this but I don't know how to write that.
I don't think there is another "standard" UNIX tool to do this.
Btw, cut & paste actually has 3 steps:
- Copy selected text to clipboard
- Remove from original file
- Paste from clipboard to new file
The 2-step UNIX command does it without a clipboard.
answered Oct 19 '13 at 10:53
janosjanos
93.4k17148177
93.4k17148177
add a comment |
add a comment |
I like the other answers, but just an (obvious) alternative approach in case you are worried about making a mistake in your pattern and want an easy way to rollback:
grep 'bar' file1 > file2
grep -v 'bar' file1 > file3
Once you're happy with your results (hint: tee
instead of >
will allow you to see what's getting written to your files):
mv file3 file1
Generally I prefer perl to do substitution like in the above answer, but when the regex idiom gets too complicated and you are semi-blindly copying and pasting commands you don't understand, you're not in control of your precious data.
add a comment |
I like the other answers, but just an (obvious) alternative approach in case you are worried about making a mistake in your pattern and want an easy way to rollback:
grep 'bar' file1 > file2
grep -v 'bar' file1 > file3
Once you're happy with your results (hint: tee
instead of >
will allow you to see what's getting written to your files):
mv file3 file1
Generally I prefer perl to do substitution like in the above answer, but when the regex idiom gets too complicated and you are semi-blindly copying and pasting commands you don't understand, you're not in control of your precious data.
add a comment |
I like the other answers, but just an (obvious) alternative approach in case you are worried about making a mistake in your pattern and want an easy way to rollback:
grep 'bar' file1 > file2
grep -v 'bar' file1 > file3
Once you're happy with your results (hint: tee
instead of >
will allow you to see what's getting written to your files):
mv file3 file1
Generally I prefer perl to do substitution like in the above answer, but when the regex idiom gets too complicated and you are semi-blindly copying and pasting commands you don't understand, you're not in control of your precious data.
I like the other answers, but just an (obvious) alternative approach in case you are worried about making a mistake in your pattern and want an easy way to rollback:
grep 'bar' file1 > file2
grep -v 'bar' file1 > file3
Once you're happy with your results (hint: tee
instead of >
will allow you to see what's getting written to your files):
mv file3 file1
Generally I prefer perl to do substitution like in the above answer, but when the regex idiom gets too complicated and you are semi-blindly copying and pasting commands you don't understand, you're not in control of your precious data.
edited Nov 14 '16 at 20:44
answered Nov 14 '16 at 20:37
Sridhar-SarnobatSridhar-Sarnobat
8,34585560
8,34585560
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2fstackoverflow.com%2fquestions%2f19465127%2fmove-lines-matching-a-pattern-from-one-file-to-another%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
Why
-1
? No comment? One who did that should show how to solve this in single line command before down voting!– jkshah
Oct 19 '13 at 11:35
Vote to close without providing any reason or comment?! That's too much.
– jkshah
Oct 19 '13 at 13:09