File's order got destroyed while grepping and echoing into for loop
I want to write the result of grep inside for loop into a file, keeping original file structure.
Original file:
$cat newtest1
some_text
/homes/myself/joz
ivan petrov - 20/10/19
new customer:
/homes/myself/silvia
john smith - 30/11/19
old customer:
some_text
I am using grep in for loop and getting the following:
$for i in `cat newtest1 | grep customer -B2`; do echo $i; done
/homes/myself/joz
ivan
petrov
-
etc.
File's structure got changed.
I tried printf - the same result.
Question: how to keep file's structure using grep in for loop?
command-line grep echo
add a comment |
I want to write the result of grep inside for loop into a file, keeping original file structure.
Original file:
$cat newtest1
some_text
/homes/myself/joz
ivan petrov - 20/10/19
new customer:
/homes/myself/silvia
john smith - 30/11/19
old customer:
some_text
I am using grep in for loop and getting the following:
$for i in `cat newtest1 | grep customer -B2`; do echo $i; done
/homes/myself/joz
ivan
petrov
-
etc.
File's structure got changed.
I tried printf - the same result.
Question: how to keep file's structure using grep in for loop?
command-line grep echo
What's your intent with thefor
loop? Why not just thegrep
?
– egmont
Mar 12 at 8:58
For I want to use some ifs inside for loop.
– Josef Klimuk
Mar 12 at 9:06
This sounds like an XY problem - for example how to process 3-line records that are delimited by the string "customer:"
– steeldriver
Mar 12 at 12:00
The question was asked exactly about what I needed and the answer helped. It was meaningless for me to include if statements, since I had no question about if statements.
– Josef Klimuk
Mar 12 at 13:15
add a comment |
I want to write the result of grep inside for loop into a file, keeping original file structure.
Original file:
$cat newtest1
some_text
/homes/myself/joz
ivan petrov - 20/10/19
new customer:
/homes/myself/silvia
john smith - 30/11/19
old customer:
some_text
I am using grep in for loop and getting the following:
$for i in `cat newtest1 | grep customer -B2`; do echo $i; done
/homes/myself/joz
ivan
petrov
-
etc.
File's structure got changed.
I tried printf - the same result.
Question: how to keep file's structure using grep in for loop?
command-line grep echo
I want to write the result of grep inside for loop into a file, keeping original file structure.
Original file:
$cat newtest1
some_text
/homes/myself/joz
ivan petrov - 20/10/19
new customer:
/homes/myself/silvia
john smith - 30/11/19
old customer:
some_text
I am using grep in for loop and getting the following:
$for i in `cat newtest1 | grep customer -B2`; do echo $i; done
/homes/myself/joz
ivan
petrov
-
etc.
File's structure got changed.
I tried printf - the same result.
Question: how to keep file's structure using grep in for loop?
command-line grep echo
command-line grep echo
asked Mar 12 at 8:52
Josef KlimukJosef Klimuk
555117
555117
What's your intent with thefor
loop? Why not just thegrep
?
– egmont
Mar 12 at 8:58
For I want to use some ifs inside for loop.
– Josef Klimuk
Mar 12 at 9:06
This sounds like an XY problem - for example how to process 3-line records that are delimited by the string "customer:"
– steeldriver
Mar 12 at 12:00
The question was asked exactly about what I needed and the answer helped. It was meaningless for me to include if statements, since I had no question about if statements.
– Josef Klimuk
Mar 12 at 13:15
add a comment |
What's your intent with thefor
loop? Why not just thegrep
?
– egmont
Mar 12 at 8:58
For I want to use some ifs inside for loop.
– Josef Klimuk
Mar 12 at 9:06
This sounds like an XY problem - for example how to process 3-line records that are delimited by the string "customer:"
– steeldriver
Mar 12 at 12:00
The question was asked exactly about what I needed and the answer helped. It was meaningless for me to include if statements, since I had no question about if statements.
– Josef Klimuk
Mar 12 at 13:15
What's your intent with the
for
loop? Why not just the grep
?– egmont
Mar 12 at 8:58
What's your intent with the
for
loop? Why not just the grep
?– egmont
Mar 12 at 8:58
For I want to use some ifs inside for loop.
– Josef Klimuk
Mar 12 at 9:06
For I want to use some ifs inside for loop.
– Josef Klimuk
Mar 12 at 9:06
This sounds like an XY problem - for example how to process 3-line records that are delimited by the string "customer:"
– steeldriver
Mar 12 at 12:00
This sounds like an XY problem - for example how to process 3-line records that are delimited by the string "customer:"
– steeldriver
Mar 12 at 12:00
The question was asked exactly about what I needed and the answer helped. It was meaningless for me to include if statements, since I had no question about if statements.
– Josef Klimuk
Mar 12 at 13:15
The question was asked exactly about what I needed and the answer helped. It was meaningless for me to include if statements, since I had no question about if statements.
– Josef Klimuk
Mar 12 at 13:15
add a comment |
1 Answer
1
active
oldest
votes
You need to quote variables and the result of command substitution to prevent word splitting:
for i in "$(<newtest1 grep customer -B2)"; do
echo "$i"
done
Of course to just output the matching lines you don’t need the for
loop, but I suppose you want to do other things with the lines. In any case you don’t need the cat
, you can let grep
open the file with grep … newtest1
or let the shell open it and assign its content to grep
’s stdin as I did above. The latter has a number of advantages explained in this great answer: When should I use input redirection?
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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%2faskubuntu.com%2fquestions%2f1124978%2ffiles-order-got-destroyed-while-grepping-and-echoing-into-for-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to quote variables and the result of command substitution to prevent word splitting:
for i in "$(<newtest1 grep customer -B2)"; do
echo "$i"
done
Of course to just output the matching lines you don’t need the for
loop, but I suppose you want to do other things with the lines. In any case you don’t need the cat
, you can let grep
open the file with grep … newtest1
or let the shell open it and assign its content to grep
’s stdin as I did above. The latter has a number of advantages explained in this great answer: When should I use input redirection?
add a comment |
You need to quote variables and the result of command substitution to prevent word splitting:
for i in "$(<newtest1 grep customer -B2)"; do
echo "$i"
done
Of course to just output the matching lines you don’t need the for
loop, but I suppose you want to do other things with the lines. In any case you don’t need the cat
, you can let grep
open the file with grep … newtest1
or let the shell open it and assign its content to grep
’s stdin as I did above. The latter has a number of advantages explained in this great answer: When should I use input redirection?
add a comment |
You need to quote variables and the result of command substitution to prevent word splitting:
for i in "$(<newtest1 grep customer -B2)"; do
echo "$i"
done
Of course to just output the matching lines you don’t need the for
loop, but I suppose you want to do other things with the lines. In any case you don’t need the cat
, you can let grep
open the file with grep … newtest1
or let the shell open it and assign its content to grep
’s stdin as I did above. The latter has a number of advantages explained in this great answer: When should I use input redirection?
You need to quote variables and the result of command substitution to prevent word splitting:
for i in "$(<newtest1 grep customer -B2)"; do
echo "$i"
done
Of course to just output the matching lines you don’t need the for
loop, but I suppose you want to do other things with the lines. In any case you don’t need the cat
, you can let grep
open the file with grep … newtest1
or let the shell open it and assign its content to grep
’s stdin as I did above. The latter has a number of advantages explained in this great answer: When should I use input redirection?
edited Mar 12 at 9:06
answered Mar 12 at 8:59
dessertdessert
24.6k672105
24.6k672105
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1124978%2ffiles-order-got-destroyed-while-grepping-and-echoing-into-for-loop%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
What's your intent with the
for
loop? Why not just thegrep
?– egmont
Mar 12 at 8:58
For I want to use some ifs inside for loop.
– Josef Klimuk
Mar 12 at 9:06
This sounds like an XY problem - for example how to process 3-line records that are delimited by the string "customer:"
– steeldriver
Mar 12 at 12:00
The question was asked exactly about what I needed and the answer helped. It was meaningless for me to include if statements, since I had no question about if statements.
– Josef Klimuk
Mar 12 at 13:15