Unix script terminates after reading first line in file
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am trying to count the number of tabs per line in file and when condition matches print the line to another file. But the script reads only the first line and terminates.
Please let me know on what's wrong in the below,
#!/bin/bash
set -e
set -o pipefail
filename="0101.tsv"
while IFS= read -r line;do
s=$(awk 'print gsub(/t/,"")')
echo $s
if [[ $s -eq 995 ]]; then
printf "%sn" "$line"
continue
fi
done < $filename > abc.tsv
Thanks!.
bash shell-script
add a comment |
up vote
1
down vote
favorite
I am trying to count the number of tabs per line in file and when condition matches print the line to another file. But the script reads only the first line and terminates.
Please let me know on what's wrong in the below,
#!/bin/bash
set -e
set -o pipefail
filename="0101.tsv"
while IFS= read -r line;do
s=$(awk 'print gsub(/t/,"")')
echo $s
if [[ $s -eq 995 ]]; then
printf "%sn" "$line"
continue
fi
done < $filename > abc.tsv
Thanks!.
bash shell-script
5
You are not providing any input toawk
. However the whole shell loop is superfluous - you can conditionally print lines with awk alone e.g.awk 'gsub(/t/,"") == 995 print' 0101.tsv
or (assuming you want to count tab separated fields) simplyawk -F't' 'NF == 996' 0101.tsv
– steeldriver
Dec 10 at 7:37
@steeldriver You should probably make this an answer instead.
– Graipher
Dec 10 at 9:37
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to count the number of tabs per line in file and when condition matches print the line to another file. But the script reads only the first line and terminates.
Please let me know on what's wrong in the below,
#!/bin/bash
set -e
set -o pipefail
filename="0101.tsv"
while IFS= read -r line;do
s=$(awk 'print gsub(/t/,"")')
echo $s
if [[ $s -eq 995 ]]; then
printf "%sn" "$line"
continue
fi
done < $filename > abc.tsv
Thanks!.
bash shell-script
I am trying to count the number of tabs per line in file and when condition matches print the line to another file. But the script reads only the first line and terminates.
Please let me know on what's wrong in the below,
#!/bin/bash
set -e
set -o pipefail
filename="0101.tsv"
while IFS= read -r line;do
s=$(awk 'print gsub(/t/,"")')
echo $s
if [[ $s -eq 995 ]]; then
printf "%sn" "$line"
continue
fi
done < $filename > abc.tsv
Thanks!.
bash shell-script
bash shell-script
asked Dec 10 at 7:12
v.rajan
93
93
5
You are not providing any input toawk
. However the whole shell loop is superfluous - you can conditionally print lines with awk alone e.g.awk 'gsub(/t/,"") == 995 print' 0101.tsv
or (assuming you want to count tab separated fields) simplyawk -F't' 'NF == 996' 0101.tsv
– steeldriver
Dec 10 at 7:37
@steeldriver You should probably make this an answer instead.
– Graipher
Dec 10 at 9:37
add a comment |
5
You are not providing any input toawk
. However the whole shell loop is superfluous - you can conditionally print lines with awk alone e.g.awk 'gsub(/t/,"") == 995 print' 0101.tsv
or (assuming you want to count tab separated fields) simplyawk -F't' 'NF == 996' 0101.tsv
– steeldriver
Dec 10 at 7:37
@steeldriver You should probably make this an answer instead.
– Graipher
Dec 10 at 9:37
5
5
You are not providing any input to
awk
. However the whole shell loop is superfluous - you can conditionally print lines with awk alone e.g. awk 'gsub(/t/,"") == 995 print' 0101.tsv
or (assuming you want to count tab separated fields) simply awk -F't' 'NF == 996' 0101.tsv
– steeldriver
Dec 10 at 7:37
You are not providing any input to
awk
. However the whole shell loop is superfluous - you can conditionally print lines with awk alone e.g. awk 'gsub(/t/,"") == 995 print' 0101.tsv
or (assuming you want to count tab separated fields) simply awk -F't' 'NF == 996' 0101.tsv
– steeldriver
Dec 10 at 7:37
@steeldriver You should probably make this an answer instead.
– Graipher
Dec 10 at 9:37
@steeldriver You should probably make this an answer instead.
– Graipher
Dec 10 at 9:37
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
You appear to want to count the number of tab-delimited fields in a file. To do this, you would have to split the input line on tabs and count them. awk
can do this automatically, and it also has a special variable for the resulting number of fields, NF
.
If you want to print all lines that has 996 fields (995 tabs):
awk -F 't' 'NF == 996' <file
This is a shorthand way of writing
awk 'BEGIN FS = "t" NF == 996 print ' <file
where print
means print $0
, i.e. print the input record (line), and FS
is the input field separator.
Whenever you find yourself extracting lines of text from a file and passing them to awk
or sed
or a similar tool in a loop, then there is always a more efficient way of doing the same operation. Note that the above commands only calls awk
a single time, whereas your solution (had it passed the data correctly to awk
) would have called awk
for each and every line in the file.
add a comment |
up vote
0
down vote
The oneliners with awk
by @steeldriver will do the job, but if you want a bash shellscript reading lines, you can do it like this,
#!/bin/bash
set -e
set -o pipefail
filename="0101.tsv"
while IFS= read -r line
do
s=0
len=$#line
# echo "line=$line"
# echo "len=$len"
for (( i=0; i<$len; i++ ))
do
if [ "$line:i:1" == $'t' ]
then
s=$((s +1))
fi
done
echo $s
if [[ "$s" == "995" ]]; then
printf "%sn" "$line"
continue
fi
done < "$filename" > abc.tsv
2
Related: Why is using a shell loop to process text considered bad practice?
– Kusalananda
Dec 10 at 8:52
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f487059%2funix-script-terminates-after-reading-first-line-in-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
You appear to want to count the number of tab-delimited fields in a file. To do this, you would have to split the input line on tabs and count them. awk
can do this automatically, and it also has a special variable for the resulting number of fields, NF
.
If you want to print all lines that has 996 fields (995 tabs):
awk -F 't' 'NF == 996' <file
This is a shorthand way of writing
awk 'BEGIN FS = "t" NF == 996 print ' <file
where print
means print $0
, i.e. print the input record (line), and FS
is the input field separator.
Whenever you find yourself extracting lines of text from a file and passing them to awk
or sed
or a similar tool in a loop, then there is always a more efficient way of doing the same operation. Note that the above commands only calls awk
a single time, whereas your solution (had it passed the data correctly to awk
) would have called awk
for each and every line in the file.
add a comment |
up vote
2
down vote
You appear to want to count the number of tab-delimited fields in a file. To do this, you would have to split the input line on tabs and count them. awk
can do this automatically, and it also has a special variable for the resulting number of fields, NF
.
If you want to print all lines that has 996 fields (995 tabs):
awk -F 't' 'NF == 996' <file
This is a shorthand way of writing
awk 'BEGIN FS = "t" NF == 996 print ' <file
where print
means print $0
, i.e. print the input record (line), and FS
is the input field separator.
Whenever you find yourself extracting lines of text from a file and passing them to awk
or sed
or a similar tool in a loop, then there is always a more efficient way of doing the same operation. Note that the above commands only calls awk
a single time, whereas your solution (had it passed the data correctly to awk
) would have called awk
for each and every line in the file.
add a comment |
up vote
2
down vote
up vote
2
down vote
You appear to want to count the number of tab-delimited fields in a file. To do this, you would have to split the input line on tabs and count them. awk
can do this automatically, and it also has a special variable for the resulting number of fields, NF
.
If you want to print all lines that has 996 fields (995 tabs):
awk -F 't' 'NF == 996' <file
This is a shorthand way of writing
awk 'BEGIN FS = "t" NF == 996 print ' <file
where print
means print $0
, i.e. print the input record (line), and FS
is the input field separator.
Whenever you find yourself extracting lines of text from a file and passing them to awk
or sed
or a similar tool in a loop, then there is always a more efficient way of doing the same operation. Note that the above commands only calls awk
a single time, whereas your solution (had it passed the data correctly to awk
) would have called awk
for each and every line in the file.
You appear to want to count the number of tab-delimited fields in a file. To do this, you would have to split the input line on tabs and count them. awk
can do this automatically, and it also has a special variable for the resulting number of fields, NF
.
If you want to print all lines that has 996 fields (995 tabs):
awk -F 't' 'NF == 996' <file
This is a shorthand way of writing
awk 'BEGIN FS = "t" NF == 996 print ' <file
where print
means print $0
, i.e. print the input record (line), and FS
is the input field separator.
Whenever you find yourself extracting lines of text from a file and passing them to awk
or sed
or a similar tool in a loop, then there is always a more efficient way of doing the same operation. Note that the above commands only calls awk
a single time, whereas your solution (had it passed the data correctly to awk
) would have called awk
for each and every line in the file.
edited Dec 10 at 10:09
answered Dec 10 at 10:02
Kusalananda
121k16226370
121k16226370
add a comment |
add a comment |
up vote
0
down vote
The oneliners with awk
by @steeldriver will do the job, but if you want a bash shellscript reading lines, you can do it like this,
#!/bin/bash
set -e
set -o pipefail
filename="0101.tsv"
while IFS= read -r line
do
s=0
len=$#line
# echo "line=$line"
# echo "len=$len"
for (( i=0; i<$len; i++ ))
do
if [ "$line:i:1" == $'t' ]
then
s=$((s +1))
fi
done
echo $s
if [[ "$s" == "995" ]]; then
printf "%sn" "$line"
continue
fi
done < "$filename" > abc.tsv
2
Related: Why is using a shell loop to process text considered bad practice?
– Kusalananda
Dec 10 at 8:52
add a comment |
up vote
0
down vote
The oneliners with awk
by @steeldriver will do the job, but if you want a bash shellscript reading lines, you can do it like this,
#!/bin/bash
set -e
set -o pipefail
filename="0101.tsv"
while IFS= read -r line
do
s=0
len=$#line
# echo "line=$line"
# echo "len=$len"
for (( i=0; i<$len; i++ ))
do
if [ "$line:i:1" == $'t' ]
then
s=$((s +1))
fi
done
echo $s
if [[ "$s" == "995" ]]; then
printf "%sn" "$line"
continue
fi
done < "$filename" > abc.tsv
2
Related: Why is using a shell loop to process text considered bad practice?
– Kusalananda
Dec 10 at 8:52
add a comment |
up vote
0
down vote
up vote
0
down vote
The oneliners with awk
by @steeldriver will do the job, but if you want a bash shellscript reading lines, you can do it like this,
#!/bin/bash
set -e
set -o pipefail
filename="0101.tsv"
while IFS= read -r line
do
s=0
len=$#line
# echo "line=$line"
# echo "len=$len"
for (( i=0; i<$len; i++ ))
do
if [ "$line:i:1" == $'t' ]
then
s=$((s +1))
fi
done
echo $s
if [[ "$s" == "995" ]]; then
printf "%sn" "$line"
continue
fi
done < "$filename" > abc.tsv
The oneliners with awk
by @steeldriver will do the job, but if you want a bash shellscript reading lines, you can do it like this,
#!/bin/bash
set -e
set -o pipefail
filename="0101.tsv"
while IFS= read -r line
do
s=0
len=$#line
# echo "line=$line"
# echo "len=$len"
for (( i=0; i<$len; i++ ))
do
if [ "$line:i:1" == $'t' ]
then
s=$((s +1))
fi
done
echo $s
if [[ "$s" == "995" ]]; then
printf "%sn" "$line"
continue
fi
done < "$filename" > abc.tsv
answered Dec 10 at 8:36
sudodus
82616
82616
2
Related: Why is using a shell loop to process text considered bad practice?
– Kusalananda
Dec 10 at 8:52
add a comment |
2
Related: Why is using a shell loop to process text considered bad practice?
– Kusalananda
Dec 10 at 8:52
2
2
Related: Why is using a shell loop to process text considered bad practice?
– Kusalananda
Dec 10 at 8:52
Related: Why is using a shell loop to process text considered bad practice?
– Kusalananda
Dec 10 at 8:52
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2funix.stackexchange.com%2fquestions%2f487059%2funix-script-terminates-after-reading-first-line-in-file%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
5
You are not providing any input to
awk
. However the whole shell loop is superfluous - you can conditionally print lines with awk alone e.g.awk 'gsub(/t/,"") == 995 print' 0101.tsv
or (assuming you want to count tab separated fields) simplyawk -F't' 'NF == 996' 0101.tsv
– steeldriver
Dec 10 at 7:37
@steeldriver You should probably make this an answer instead.
– Graipher
Dec 10 at 9:37