How to filter and redirect output
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have a command which will produce lots of output to STDOUT, which I know can be redirected into a file this way:
./myCMD 1>tmp
How can the output be filtered before redirecting it into the file. For example, I would want to redirect only those lines of output which contain some key word.
io-redirection
add a comment |Â
up vote
1
down vote
favorite
I have a command which will produce lots of output to STDOUT, which I know can be redirected into a file this way:
./myCMD 1>tmp
How can the output be filtered before redirecting it into the file. For example, I would want to redirect only those lines of output which contain some key word.
io-redirection
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a command which will produce lots of output to STDOUT, which I know can be redirected into a file this way:
./myCMD 1>tmp
How can the output be filtered before redirecting it into the file. For example, I would want to redirect only those lines of output which contain some key word.
io-redirection
I have a command which will produce lots of output to STDOUT, which I know can be redirected into a file this way:
./myCMD 1>tmp
How can the output be filtered before redirecting it into the file. For example, I would want to redirect only those lines of output which contain some key word.
io-redirection
edited Mar 7 at 10:01
user1404316
2,314520
2,314520
asked Mar 7 at 9:06
Yves
705414
705414
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
If you mean that you only want some lines to go to the tmp
file, while the rest are left untouched:
cmd | awk '/pattern/ print > "tmp"; next
print'
Or:
cmd | sed '/pattern/!b
w tmp
d'
(on one line: cmd | sed -e '/pattern/!b' -e 'w tmp' -e d
)
Or:
cmd | sed '/pattern/
w tmp
d
'
(on one line: cmd | sed -e '/pattern/w tmp' -e 'd;'
)
Note that for sed
, pattern
is a basic regular expression, while for awk
, it's an extended regular expression.
add a comment |Â
up vote
2
down vote
You can do this with pipes:
./myCMD | grep keyword > tmp
This will only write lines containing âÂÂkeywordâ to the tmp
file.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
If you mean that you only want some lines to go to the tmp
file, while the rest are left untouched:
cmd | awk '/pattern/ print > "tmp"; next
print'
Or:
cmd | sed '/pattern/!b
w tmp
d'
(on one line: cmd | sed -e '/pattern/!b' -e 'w tmp' -e d
)
Or:
cmd | sed '/pattern/
w tmp
d
'
(on one line: cmd | sed -e '/pattern/w tmp' -e 'd;'
)
Note that for sed
, pattern
is a basic regular expression, while for awk
, it's an extended regular expression.
add a comment |Â
up vote
1
down vote
accepted
If you mean that you only want some lines to go to the tmp
file, while the rest are left untouched:
cmd | awk '/pattern/ print > "tmp"; next
print'
Or:
cmd | sed '/pattern/!b
w tmp
d'
(on one line: cmd | sed -e '/pattern/!b' -e 'w tmp' -e d
)
Or:
cmd | sed '/pattern/
w tmp
d
'
(on one line: cmd | sed -e '/pattern/w tmp' -e 'd;'
)
Note that for sed
, pattern
is a basic regular expression, while for awk
, it's an extended regular expression.
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
If you mean that you only want some lines to go to the tmp
file, while the rest are left untouched:
cmd | awk '/pattern/ print > "tmp"; next
print'
Or:
cmd | sed '/pattern/!b
w tmp
d'
(on one line: cmd | sed -e '/pattern/!b' -e 'w tmp' -e d
)
Or:
cmd | sed '/pattern/
w tmp
d
'
(on one line: cmd | sed -e '/pattern/w tmp' -e 'd;'
)
Note that for sed
, pattern
is a basic regular expression, while for awk
, it's an extended regular expression.
If you mean that you only want some lines to go to the tmp
file, while the rest are left untouched:
cmd | awk '/pattern/ print > "tmp"; next
print'
Or:
cmd | sed '/pattern/!b
w tmp
d'
(on one line: cmd | sed -e '/pattern/!b' -e 'w tmp' -e d
)
Or:
cmd | sed '/pattern/
w tmp
d
'
(on one line: cmd | sed -e '/pattern/w tmp' -e 'd;'
)
Note that for sed
, pattern
is a basic regular expression, while for awk
, it's an extended regular expression.
answered Mar 7 at 10:34
Stéphane Chazelas
280k53515847
280k53515847
add a comment |Â
add a comment |Â
up vote
2
down vote
You can do this with pipes:
./myCMD | grep keyword > tmp
This will only write lines containing âÂÂkeywordâ to the tmp
file.
add a comment |Â
up vote
2
down vote
You can do this with pipes:
./myCMD | grep keyword > tmp
This will only write lines containing âÂÂkeywordâ to the tmp
file.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
You can do this with pipes:
./myCMD | grep keyword > tmp
This will only write lines containing âÂÂkeywordâ to the tmp
file.
You can do this with pipes:
./myCMD | grep keyword > tmp
This will only write lines containing âÂÂkeywordâ to the tmp
file.
answered Mar 7 at 9:08
Stephen Kitt
141k22307367
141k22307367
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%2f428695%2fhow-to-filter-and-redirect-output%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