Use output of sed for a while read loop [duplicate]Reading lines in a file and avoiding lines with # with BashLoop through results of sedEmulate a do-while loop in Python?Syntax for a single-line Bash infinite while loopHow to count all the lines of code in a directory recursively?Looping through the content of a file in BashHow does “cat << EOF” work in bash?echo that outputs to stderrHow do I compare two string variables in an 'if' statement in Bash?How do I set a variable to the output of a command in Bash?How to change the output color of echo in LinuxLoop through an array of strings in Bash?

Multi tool use
Social leper versus social leopard
How to make interviewee comfortable interviewing in lounge chairs
Can Northern Ireland's border issue be solved by repartition?
To what extent is it worthwhile to report check fraud / refund scams?
Resolving moral conflict
What are these pixel-level discolored specks? How can I fix it?
A drug that allows people to survive on less food
Are there non JavaScript ways to hide HTML source code?
Has my MacBook been hacked?
How to manage expenditure when billing cycles and paycheck cycles are not aligned?
How to deal with my team leader who keeps calling me about project updates even though I am on leave for personal reasons?
What is the meaning of "heutig" in this sentence?
Does Diablo III have a loot filter?
A high quality contribution but an annoying error is present in my published article
Is it impolite to ask for halal food when traveling to and in Thailand?
Writing a letter of recommendation for a mediocre student
What is the need of methods like GET and POST in the HTTP protocol?
In a folk jam session, when asked which key my non-transposing chromatic instrument (like a violin) is in, what do I answer?
Why are there two fundamental laws of logic?
reverse a list of generic type
Do we know the situation in Britain before Sealion (summer 1940)?
How can an attacker use robots.txt?
Meaning of 'ran' in German?
How use custom order in folder on Windows 7 and 10
Use output of sed for a while read loop [duplicate]
Reading lines in a file and avoiding lines with # with BashLoop through results of sedEmulate a do-while loop in Python?Syntax for a single-line Bash infinite while loopHow to count all the lines of code in a directory recursively?Looping through the content of a file in BashHow does “cat << EOF” work in bash?echo that outputs to stderrHow do I compare two string variables in an 'if' statement in Bash?How do I set a variable to the output of a command in Bash?How to change the output color of echo in LinuxLoop through an array of strings in Bash?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
This question already has an answer here:
Loop through results of sed
2 answers
Reading lines in a file and avoiding lines with # with Bash
10 answers
I want the while loop to ignore blank (empty) lines and lines that contain #
I tried including sed -e 's/#.*$//' -e '/^$/d' on the input file and piping this into the while loop. This did not work.
file=$(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
while IFS=: read -r f1 f2 f3 f4; do
Command
done <"$file"
linux bash while-loop
marked as duplicate by tripleee
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 28 at 16:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment
|
This question already has an answer here:
Loop through results of sed
2 answers
Reading lines in a file and avoiding lines with # with Bash
10 answers
I want the while loop to ignore blank (empty) lines and lines that contain #
I tried including sed -e 's/#.*$//' -e '/^$/d' on the input file and piping this into the while loop. This did not work.
file=$(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
while IFS=: read -r f1 f2 f3 f4; do
Command
done <"$file"
linux bash while-loop
marked as duplicate by tripleee
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 28 at 16:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment
|
This question already has an answer here:
Loop through results of sed
2 answers
Reading lines in a file and avoiding lines with # with Bash
10 answers
I want the while loop to ignore blank (empty) lines and lines that contain #
I tried including sed -e 's/#.*$//' -e '/^$/d' on the input file and piping this into the while loop. This did not work.
file=$(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
while IFS=: read -r f1 f2 f3 f4; do
Command
done <"$file"
linux bash while-loop
This question already has an answer here:
Loop through results of sed
2 answers
Reading lines in a file and avoiding lines with # with Bash
10 answers
I want the while loop to ignore blank (empty) lines and lines that contain #
I tried including sed -e 's/#.*$//' -e '/^$/d' on the input file and piping this into the while loop. This did not work.
file=$(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
while IFS=: read -r f1 f2 f3 f4; do
Command
done <"$file"
This question already has an answer here:
Loop through results of sed
2 answers
Reading lines in a file and avoiding lines with # with Bash
10 answers
linux bash while-loop
linux bash while-loop
edited Mar 28 at 16:30
Charles Duffy
193k29 gold badges222 silver badges279 bronze badges
193k29 gold badges222 silver badges279 bronze badges
asked Mar 28 at 16:05
KevinKevin
185 bronze badges
185 bronze badges
marked as duplicate by tripleee
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 28 at 16:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by tripleee
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 28 at 16:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by tripleee
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 28 at 16:23
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment
|
add a comment
|
3 Answers
3
active
oldest
votes
You're trying to open that output as a filename, which it almost certainly isn't.
Run sed in a subshell and redirect its output to while loop instead.
while IFS=: read -r f1 f2 f3 f4; do
# do something
done < <(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
For anyone interested in how the syntax used in this answer works, see the bash-hackers' wiki on process substitution, and for why this syntax is better than sed ... | while read ...
, see BashFAQ #24.
add a comment
|
You can use grep
:
grep -Ev '^s*$|^s*#' foo.txt | while IFS=: read -r f1 f2 f3 f4; do
Command
done
Evengrep -E
isn't guaranteed to supports
; only the regex syntax documented in the POSIX ERE standard is available across all platforms, ands
-- like other backslash-sequence shorthand -- isn't there. To have guaranteed support on all POSIX platforms, use[[:space:]]
instead.
– Charles Duffy
Mar 28 at 16:21
...so, the use ofgrep
doesn't fix anything (the oldsed
command worked fine), and (with the use ofs
) introduces portability issues; the use of a pipe does fix the OP's problem, but it fixes it in a way that introduces different bugs.
– Charles Duffy
Mar 28 at 16:22
add a comment
|
use grep -v:
-v, --invert-match select non-matching lines
That certainly works, but the problem isn't with thesed
code, it's with how they're getting its result into thewhile read
loop. Thus, this answer replaces one piece of working code with a different piece of working code, but doesn't actually fix the bug the OP is asking about.
– Charles Duffy
Mar 28 at 16:13
If you read the title, then yes. But this is the first line of the post: "I want the while loop to ignore blank (empty) lines and lines that contain #" OP tried with sed, I'm suggesting to use grep instead.
– Dimas
Apr 4 at 17:08
Here's the thing, though -- the thing the OP tried with sed works fine. You're suggesting that they change the part that already works, not the part that's broken. The bug isn't caused byfile=$(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
doing something other than what the OP expects it to, so changing that line tofile=$(grep -v '#|^$')
or whatever doesn't fix it.
– Charles Duffy
Apr 4 at 17:12
...instead, the problem is caused by<"$file"
iterating over a file named by the output of theirsed
command, or yourgrep
command; switching which of those two interchangable commands is used is irrelevant to whether they hit that issue unless the other, later code is fixed.
– Charles Duffy
Apr 4 at 17:15
add a comment
|
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You're trying to open that output as a filename, which it almost certainly isn't.
Run sed in a subshell and redirect its output to while loop instead.
while IFS=: read -r f1 f2 f3 f4; do
# do something
done < <(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
For anyone interested in how the syntax used in this answer works, see the bash-hackers' wiki on process substitution, and for why this syntax is better than sed ... | while read ...
, see BashFAQ #24.
add a comment
|
You're trying to open that output as a filename, which it almost certainly isn't.
Run sed in a subshell and redirect its output to while loop instead.
while IFS=: read -r f1 f2 f3 f4; do
# do something
done < <(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
For anyone interested in how the syntax used in this answer works, see the bash-hackers' wiki on process substitution, and for why this syntax is better than sed ... | while read ...
, see BashFAQ #24.
add a comment
|
You're trying to open that output as a filename, which it almost certainly isn't.
Run sed in a subshell and redirect its output to while loop instead.
while IFS=: read -r f1 f2 f3 f4; do
# do something
done < <(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
For anyone interested in how the syntax used in this answer works, see the bash-hackers' wiki on process substitution, and for why this syntax is better than sed ... | while read ...
, see BashFAQ #24.
You're trying to open that output as a filename, which it almost certainly isn't.
Run sed in a subshell and redirect its output to while loop instead.
while IFS=: read -r f1 f2 f3 f4; do
# do something
done < <(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
For anyone interested in how the syntax used in this answer works, see the bash-hackers' wiki on process substitution, and for why this syntax is better than sed ... | while read ...
, see BashFAQ #24.
edited Mar 28 at 16:23
Charles Duffy
193k29 gold badges222 silver badges279 bronze badges
193k29 gold badges222 silver badges279 bronze badges
answered Mar 28 at 16:07
oguz ismailoguz ismail
12.8k7 gold badges19 silver badges37 bronze badges
12.8k7 gold badges19 silver badges37 bronze badges
add a comment
|
add a comment
|
You can use grep
:
grep -Ev '^s*$|^s*#' foo.txt | while IFS=: read -r f1 f2 f3 f4; do
Command
done
Evengrep -E
isn't guaranteed to supports
; only the regex syntax documented in the POSIX ERE standard is available across all platforms, ands
-- like other backslash-sequence shorthand -- isn't there. To have guaranteed support on all POSIX platforms, use[[:space:]]
instead.
– Charles Duffy
Mar 28 at 16:21
...so, the use ofgrep
doesn't fix anything (the oldsed
command worked fine), and (with the use ofs
) introduces portability issues; the use of a pipe does fix the OP's problem, but it fixes it in a way that introduces different bugs.
– Charles Duffy
Mar 28 at 16:22
add a comment
|
You can use grep
:
grep -Ev '^s*$|^s*#' foo.txt | while IFS=: read -r f1 f2 f3 f4; do
Command
done
Evengrep -E
isn't guaranteed to supports
; only the regex syntax documented in the POSIX ERE standard is available across all platforms, ands
-- like other backslash-sequence shorthand -- isn't there. To have guaranteed support on all POSIX platforms, use[[:space:]]
instead.
– Charles Duffy
Mar 28 at 16:21
...so, the use ofgrep
doesn't fix anything (the oldsed
command worked fine), and (with the use ofs
) introduces portability issues; the use of a pipe does fix the OP's problem, but it fixes it in a way that introduces different bugs.
– Charles Duffy
Mar 28 at 16:22
add a comment
|
You can use grep
:
grep -Ev '^s*$|^s*#' foo.txt | while IFS=: read -r f1 f2 f3 f4; do
Command
done
You can use grep
:
grep -Ev '^s*$|^s*#' foo.txt | while IFS=: read -r f1 f2 f3 f4; do
Command
done
answered Mar 28 at 16:18


accdiasaccdias
1,1079 silver badges14 bronze badges
1,1079 silver badges14 bronze badges
Evengrep -E
isn't guaranteed to supports
; only the regex syntax documented in the POSIX ERE standard is available across all platforms, ands
-- like other backslash-sequence shorthand -- isn't there. To have guaranteed support on all POSIX platforms, use[[:space:]]
instead.
– Charles Duffy
Mar 28 at 16:21
...so, the use ofgrep
doesn't fix anything (the oldsed
command worked fine), and (with the use ofs
) introduces portability issues; the use of a pipe does fix the OP's problem, but it fixes it in a way that introduces different bugs.
– Charles Duffy
Mar 28 at 16:22
add a comment
|
Evengrep -E
isn't guaranteed to supports
; only the regex syntax documented in the POSIX ERE standard is available across all platforms, ands
-- like other backslash-sequence shorthand -- isn't there. To have guaranteed support on all POSIX platforms, use[[:space:]]
instead.
– Charles Duffy
Mar 28 at 16:21
...so, the use ofgrep
doesn't fix anything (the oldsed
command worked fine), and (with the use ofs
) introduces portability issues; the use of a pipe does fix the OP's problem, but it fixes it in a way that introduces different bugs.
– Charles Duffy
Mar 28 at 16:22
Even
grep -E
isn't guaranteed to support s
; only the regex syntax documented in the POSIX ERE standard is available across all platforms, and s
-- like other backslash-sequence shorthand -- isn't there. To have guaranteed support on all POSIX platforms, use [[:space:]]
instead.– Charles Duffy
Mar 28 at 16:21
Even
grep -E
isn't guaranteed to support s
; only the regex syntax documented in the POSIX ERE standard is available across all platforms, and s
-- like other backslash-sequence shorthand -- isn't there. To have guaranteed support on all POSIX platforms, use [[:space:]]
instead.– Charles Duffy
Mar 28 at 16:21
...so, the use of
grep
doesn't fix anything (the old sed
command worked fine), and (with the use of s
) introduces portability issues; the use of a pipe does fix the OP's problem, but it fixes it in a way that introduces different bugs.– Charles Duffy
Mar 28 at 16:22
...so, the use of
grep
doesn't fix anything (the old sed
command worked fine), and (with the use of s
) introduces portability issues; the use of a pipe does fix the OP's problem, but it fixes it in a way that introduces different bugs.– Charles Duffy
Mar 28 at 16:22
add a comment
|
use grep -v:
-v, --invert-match select non-matching lines
That certainly works, but the problem isn't with thesed
code, it's with how they're getting its result into thewhile read
loop. Thus, this answer replaces one piece of working code with a different piece of working code, but doesn't actually fix the bug the OP is asking about.
– Charles Duffy
Mar 28 at 16:13
If you read the title, then yes. But this is the first line of the post: "I want the while loop to ignore blank (empty) lines and lines that contain #" OP tried with sed, I'm suggesting to use grep instead.
– Dimas
Apr 4 at 17:08
Here's the thing, though -- the thing the OP tried with sed works fine. You're suggesting that they change the part that already works, not the part that's broken. The bug isn't caused byfile=$(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
doing something other than what the OP expects it to, so changing that line tofile=$(grep -v '#|^$')
or whatever doesn't fix it.
– Charles Duffy
Apr 4 at 17:12
...instead, the problem is caused by<"$file"
iterating over a file named by the output of theirsed
command, or yourgrep
command; switching which of those two interchangable commands is used is irrelevant to whether they hit that issue unless the other, later code is fixed.
– Charles Duffy
Apr 4 at 17:15
add a comment
|
use grep -v:
-v, --invert-match select non-matching lines
That certainly works, but the problem isn't with thesed
code, it's with how they're getting its result into thewhile read
loop. Thus, this answer replaces one piece of working code with a different piece of working code, but doesn't actually fix the bug the OP is asking about.
– Charles Duffy
Mar 28 at 16:13
If you read the title, then yes. But this is the first line of the post: "I want the while loop to ignore blank (empty) lines and lines that contain #" OP tried with sed, I'm suggesting to use grep instead.
– Dimas
Apr 4 at 17:08
Here's the thing, though -- the thing the OP tried with sed works fine. You're suggesting that they change the part that already works, not the part that's broken. The bug isn't caused byfile=$(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
doing something other than what the OP expects it to, so changing that line tofile=$(grep -v '#|^$')
or whatever doesn't fix it.
– Charles Duffy
Apr 4 at 17:12
...instead, the problem is caused by<"$file"
iterating over a file named by the output of theirsed
command, or yourgrep
command; switching which of those two interchangable commands is used is irrelevant to whether they hit that issue unless the other, later code is fixed.
– Charles Duffy
Apr 4 at 17:15
add a comment
|
use grep -v:
-v, --invert-match select non-matching lines
use grep -v:
-v, --invert-match select non-matching lines
answered Mar 28 at 16:10
DimasDimas
787 bronze badges
787 bronze badges
That certainly works, but the problem isn't with thesed
code, it's with how they're getting its result into thewhile read
loop. Thus, this answer replaces one piece of working code with a different piece of working code, but doesn't actually fix the bug the OP is asking about.
– Charles Duffy
Mar 28 at 16:13
If you read the title, then yes. But this is the first line of the post: "I want the while loop to ignore blank (empty) lines and lines that contain #" OP tried with sed, I'm suggesting to use grep instead.
– Dimas
Apr 4 at 17:08
Here's the thing, though -- the thing the OP tried with sed works fine. You're suggesting that they change the part that already works, not the part that's broken. The bug isn't caused byfile=$(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
doing something other than what the OP expects it to, so changing that line tofile=$(grep -v '#|^$')
or whatever doesn't fix it.
– Charles Duffy
Apr 4 at 17:12
...instead, the problem is caused by<"$file"
iterating over a file named by the output of theirsed
command, or yourgrep
command; switching which of those two interchangable commands is used is irrelevant to whether they hit that issue unless the other, later code is fixed.
– Charles Duffy
Apr 4 at 17:15
add a comment
|
That certainly works, but the problem isn't with thesed
code, it's with how they're getting its result into thewhile read
loop. Thus, this answer replaces one piece of working code with a different piece of working code, but doesn't actually fix the bug the OP is asking about.
– Charles Duffy
Mar 28 at 16:13
If you read the title, then yes. But this is the first line of the post: "I want the while loop to ignore blank (empty) lines and lines that contain #" OP tried with sed, I'm suggesting to use grep instead.
– Dimas
Apr 4 at 17:08
Here's the thing, though -- the thing the OP tried with sed works fine. You're suggesting that they change the part that already works, not the part that's broken. The bug isn't caused byfile=$(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
doing something other than what the OP expects it to, so changing that line tofile=$(grep -v '#|^$')
or whatever doesn't fix it.
– Charles Duffy
Apr 4 at 17:12
...instead, the problem is caused by<"$file"
iterating over a file named by the output of theirsed
command, or yourgrep
command; switching which of those two interchangable commands is used is irrelevant to whether they hit that issue unless the other, later code is fixed.
– Charles Duffy
Apr 4 at 17:15
That certainly works, but the problem isn't with the
sed
code, it's with how they're getting its result into the while read
loop. Thus, this answer replaces one piece of working code with a different piece of working code, but doesn't actually fix the bug the OP is asking about.– Charles Duffy
Mar 28 at 16:13
That certainly works, but the problem isn't with the
sed
code, it's with how they're getting its result into the while read
loop. Thus, this answer replaces one piece of working code with a different piece of working code, but doesn't actually fix the bug the OP is asking about.– Charles Duffy
Mar 28 at 16:13
If you read the title, then yes. But this is the first line of the post: "I want the while loop to ignore blank (empty) lines and lines that contain #" OP tried with sed, I'm suggesting to use grep instead.
– Dimas
Apr 4 at 17:08
If you read the title, then yes. But this is the first line of the post: "I want the while loop to ignore blank (empty) lines and lines that contain #" OP tried with sed, I'm suggesting to use grep instead.
– Dimas
Apr 4 at 17:08
Here's the thing, though -- the thing the OP tried with sed works fine. You're suggesting that they change the part that already works, not the part that's broken. The bug isn't caused by
file=$(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
doing something other than what the OP expects it to, so changing that line to file=$(grep -v '#|^$')
or whatever doesn't fix it.– Charles Duffy
Apr 4 at 17:12
Here's the thing, though -- the thing the OP tried with sed works fine. You're suggesting that they change the part that already works, not the part that's broken. The bug isn't caused by
file=$(sed -e 's/#.*$//' -e '/^$/d' foo.txt)
doing something other than what the OP expects it to, so changing that line to file=$(grep -v '#|^$')
or whatever doesn't fix it.– Charles Duffy
Apr 4 at 17:12
...instead, the problem is caused by
<"$file"
iterating over a file named by the output of their sed
command, or your grep
command; switching which of those two interchangable commands is used is irrelevant to whether they hit that issue unless the other, later code is fixed.– Charles Duffy
Apr 4 at 17:15
...instead, the problem is caused by
<"$file"
iterating over a file named by the output of their sed
command, or your grep
command; switching which of those two interchangable commands is used is irrelevant to whether they hit that issue unless the other, later code is fixed.– Charles Duffy
Apr 4 at 17:15
add a comment
|
pvaKi,ADGCdpLrzWmwW,SVf4Ux ukS