I want to write a bash script than prints only odd lines of file, including the first line without used sed
or awk
, also without &&, || and ;
-
2The requirements seem awfully arbitrary. What is the point? Is this a homework? The question (like any question) should show reasonable research effort. What is your attempt so far? Where are you stuck?– Kamil MaciorowskiMar 15 at 8:02
-
1Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.– Community BotMar 15 at 8:44
-
Voted to close as too broad - it's not clear what was attempted here, and where the problem that the asker has lie. As such, a good answer would have to explain far too much– Marcus MüllerMar 15 at 9:15
-
When asking questions about text processing, please be sure to always include example input along with the desired output. Also, please show what you tried and where you faced difficulties, so that contributors trying to help don't propose solutions you already know won't work.– AdminBee8 hours ago
2 Answers
With perl
:
$ seq 10 | perl -ne 'print if $.%2'
1
3
5
7
9
If the input doesn't contain TAB characters, you can also do:
$ seq 10 | paste - - | cut -f1
1
3
5
7
9
With GNU grep
:
$ seq 10 | grep -n '^' | LC_ALL=C grep -Po '^\d*[13579]:\K.*'
1
3
5
7
9
In any case, you shouldn't use a shell loop to process text. If that's what your teacher expects with those weird requirements, get another teacher as they'd be teaching you bad practice. A shell's role is before all to run other commands. And as a shell scripter, you want to pick the best command for the job (here perl
, sed
or awk
) or a set of commands that collaborate together as intended by the Unix authors when they invented those pipelines some 50 years ago.
-
This
perl
solution is quite elegant, but it does not satisfy the requirements. From the question: "I want to write a bash script". Theperl
command is standalone, it's not a bash script. Your other solutions use|
, so they really require a shell like bash, this makes them little closer to "bash scripts". One can wrapperl
in a bash script though. It would make the answer better (the requirement would be met) and worse at the same time (objectively such wrapping is not the Right Thing). Mar 15 at 10:01 -
The
#bash@irc.libera.chat
(previously onfreenode
) team that is owning shellcheck, bash-hackers, bashFAQ, woodledge and so on, says thatseq(1) is nonstandard, inefficient and useless
Dixitgreybot
Mar 15 at 10:03 -
1@KamilMaciorowski, that's shell code that invokes
perl
. Also works in the bash shell. A shell is a tool to run commands. Mar 15 at 10:06 -
@GillesQuénot,
seq
here is not part of the answer, it's a tool used to provide sample data to demonstrate the answer. Mar 15 at 10:07 -
2@KamilMaciorowski, but IMO, giving a solution using a shell loop to process text, even one that only uses builtin commands of one particular shell implementation would be a disservice to the OP. If that's what their teacher want, they should get another teacher. Mar 15 at 11:06
You could do:
while IFS= read -r line
do
printf "%s\n" "$line"
IFS= read -r _
done <"file"
Trying with seq
:
while IFS= read -r line
do
printf "%s\n" "$line"
IFS= read -r _
done < <(seq 10)
1
3
5
7
9
-
-
-
1