Running a loop from second line of a file in a bash script
I want to use the below file content as an input of for loop my script. As from second line we have id's which will be used to fetch content from db.
cat abc.txt
id
27
27
27
27
23
linux bash scripting
add a comment |
I want to use the below file content as an input of for loop my script. As from second line we have id's which will be used to fetch content from db.
cat abc.txt
id
27
27
27
27
23
linux bash scripting
Is the loop doing something with these numbers that could be done in a singleawk
script?
– Kusalananda
Mar 4 at 18:20
Related: Why is using a shell loop to process text considered bad practice?
– Kusalananda
Mar 4 at 18:45
add a comment |
I want to use the below file content as an input of for loop my script. As from second line we have id's which will be used to fetch content from db.
cat abc.txt
id
27
27
27
27
23
linux bash scripting
I want to use the below file content as an input of for loop my script. As from second line we have id's which will be used to fetch content from db.
cat abc.txt
id
27
27
27
27
23
linux bash scripting
linux bash scripting
edited Feb 28 at 8:55
Inian
5,1701429
5,1701429
asked Feb 28 at 8:47
user339272user339272
61
61
Is the loop doing something with these numbers that could be done in a singleawk
script?
– Kusalananda
Mar 4 at 18:20
Related: Why is using a shell loop to process text considered bad practice?
– Kusalananda
Mar 4 at 18:45
add a comment |
Is the loop doing something with these numbers that could be done in a singleawk
script?
– Kusalananda
Mar 4 at 18:20
Related: Why is using a shell loop to process text considered bad practice?
– Kusalananda
Mar 4 at 18:45
Is the loop doing something with these numbers that could be done in a single
awk
script?– Kusalananda
Mar 4 at 18:20
Is the loop doing something with these numbers that could be done in a single
awk
script?– Kusalananda
Mar 4 at 18:20
Related: Why is using a shell loop to process text considered bad practice?
– Kusalananda
Mar 4 at 18:45
Related: Why is using a shell loop to process text considered bad practice?
– Kusalananda
Mar 4 at 18:45
add a comment |
5 Answers
5
active
oldest
votes
This can get as simple as the following:
{ read -r; while read -r line; do echo "$line"; done; } < abc.txt
The {
here means to group the command. Using read -r
we'll skip the first line!
add a comment |
You can filter it with sed
before the proc.
for example, instead of cat abc.txt
you can sed 1d abc.txt
to remove the first line.
add a comment |
This works - even if the headline is not in the first row or there is an empty line:
awk '!/^(id| *)$/{print$0}' file | while read line; do
something $line
done
I used while
in this example, because it is often preferable over for
.
That said - here is your for
loop:
for line in $(awk '!/^id$/{print$0}' file); do
something $line
done
add a comment |
Assuming the files containing your IDs is in the same directory as this script, this should work:
#!/bin/bash
content=$(tail -n +2 abc.txt)
for line in $content; do
echo "$line"
done
Output:
27
27
27
27
23
I think for large file both answers will break.
– Prvt_Yadv
Feb 28 at 9:08
2
Don't read lines withfor
-- use awhile read
loop instead.
– Gordon Davisson
Feb 28 at 9:48
add a comment |
Not recommended if your file is large
You can use like:
for i in $(sed -e '1d' abc.txt)
do
something
done
1
Don't read lines withfor
-- use awhile read
loop instead.
– Gordon Davisson
Feb 28 at 9:48
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%2f503509%2frunning-a-loop-from-second-line-of-a-file-in-a-bash-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
This can get as simple as the following:
{ read -r; while read -r line; do echo "$line"; done; } < abc.txt
The {
here means to group the command. Using read -r
we'll skip the first line!
add a comment |
This can get as simple as the following:
{ read -r; while read -r line; do echo "$line"; done; } < abc.txt
The {
here means to group the command. Using read -r
we'll skip the first line!
add a comment |
This can get as simple as the following:
{ read -r; while read -r line; do echo "$line"; done; } < abc.txt
The {
here means to group the command. Using read -r
we'll skip the first line!
This can get as simple as the following:
{ read -r; while read -r line; do echo "$line"; done; } < abc.txt
The {
here means to group the command. Using read -r
we'll skip the first line!
answered Feb 28 at 9:15
Valentin BajramiValentin Bajrami
6,13611728
6,13611728
add a comment |
add a comment |
You can filter it with sed
before the proc.
for example, instead of cat abc.txt
you can sed 1d abc.txt
to remove the first line.
add a comment |
You can filter it with sed
before the proc.
for example, instead of cat abc.txt
you can sed 1d abc.txt
to remove the first line.
add a comment |
You can filter it with sed
before the proc.
for example, instead of cat abc.txt
you can sed 1d abc.txt
to remove the first line.
You can filter it with sed
before the proc.
for example, instead of cat abc.txt
you can sed 1d abc.txt
to remove the first line.
answered Mar 4 at 16:14
JuanJuan
1869
1869
add a comment |
add a comment |
This works - even if the headline is not in the first row or there is an empty line:
awk '!/^(id| *)$/{print$0}' file | while read line; do
something $line
done
I used while
in this example, because it is often preferable over for
.
That said - here is your for
loop:
for line in $(awk '!/^id$/{print$0}' file); do
something $line
done
add a comment |
This works - even if the headline is not in the first row or there is an empty line:
awk '!/^(id| *)$/{print$0}' file | while read line; do
something $line
done
I used while
in this example, because it is often preferable over for
.
That said - here is your for
loop:
for line in $(awk '!/^id$/{print$0}' file); do
something $line
done
add a comment |
This works - even if the headline is not in the first row or there is an empty line:
awk '!/^(id| *)$/{print$0}' file | while read line; do
something $line
done
I used while
in this example, because it is often preferable over for
.
That said - here is your for
loop:
for line in $(awk '!/^id$/{print$0}' file); do
something $line
done
This works - even if the headline is not in the first row or there is an empty line:
awk '!/^(id| *)$/{print$0}' file | while read line; do
something $line
done
I used while
in this example, because it is often preferable over for
.
That said - here is your for
loop:
for line in $(awk '!/^id$/{print$0}' file); do
something $line
done
answered Mar 4 at 17:52
sborskysborsky
811611
811611
add a comment |
add a comment |
Assuming the files containing your IDs is in the same directory as this script, this should work:
#!/bin/bash
content=$(tail -n +2 abc.txt)
for line in $content; do
echo "$line"
done
Output:
27
27
27
27
23
I think for large file both answers will break.
– Prvt_Yadv
Feb 28 at 9:08
2
Don't read lines withfor
-- use awhile read
loop instead.
– Gordon Davisson
Feb 28 at 9:48
add a comment |
Assuming the files containing your IDs is in the same directory as this script, this should work:
#!/bin/bash
content=$(tail -n +2 abc.txt)
for line in $content; do
echo "$line"
done
Output:
27
27
27
27
23
I think for large file both answers will break.
– Prvt_Yadv
Feb 28 at 9:08
2
Don't read lines withfor
-- use awhile read
loop instead.
– Gordon Davisson
Feb 28 at 9:48
add a comment |
Assuming the files containing your IDs is in the same directory as this script, this should work:
#!/bin/bash
content=$(tail -n +2 abc.txt)
for line in $content; do
echo "$line"
done
Output:
27
27
27
27
23
Assuming the files containing your IDs is in the same directory as this script, this should work:
#!/bin/bash
content=$(tail -n +2 abc.txt)
for line in $content; do
echo "$line"
done
Output:
27
27
27
27
23
answered Feb 28 at 9:02
PankiPanki
778412
778412
I think for large file both answers will break.
– Prvt_Yadv
Feb 28 at 9:08
2
Don't read lines withfor
-- use awhile read
loop instead.
– Gordon Davisson
Feb 28 at 9:48
add a comment |
I think for large file both answers will break.
– Prvt_Yadv
Feb 28 at 9:08
2
Don't read lines withfor
-- use awhile read
loop instead.
– Gordon Davisson
Feb 28 at 9:48
I think for large file both answers will break.
– Prvt_Yadv
Feb 28 at 9:08
I think for large file both answers will break.
– Prvt_Yadv
Feb 28 at 9:08
2
2
Don't read lines with
for
-- use a while read
loop instead.– Gordon Davisson
Feb 28 at 9:48
Don't read lines with
for
-- use a while read
loop instead.– Gordon Davisson
Feb 28 at 9:48
add a comment |
Not recommended if your file is large
You can use like:
for i in $(sed -e '1d' abc.txt)
do
something
done
1
Don't read lines withfor
-- use awhile read
loop instead.
– Gordon Davisson
Feb 28 at 9:48
add a comment |
Not recommended if your file is large
You can use like:
for i in $(sed -e '1d' abc.txt)
do
something
done
1
Don't read lines withfor
-- use awhile read
loop instead.
– Gordon Davisson
Feb 28 at 9:48
add a comment |
Not recommended if your file is large
You can use like:
for i in $(sed -e '1d' abc.txt)
do
something
done
Not recommended if your file is large
You can use like:
for i in $(sed -e '1d' abc.txt)
do
something
done
edited Feb 28 at 9:08
answered Feb 28 at 9:03
Prvt_YadvPrvt_Yadv
2,88031227
2,88031227
1
Don't read lines withfor
-- use awhile read
loop instead.
– Gordon Davisson
Feb 28 at 9:48
add a comment |
1
Don't read lines withfor
-- use awhile read
loop instead.
– Gordon Davisson
Feb 28 at 9:48
1
1
Don't read lines with
for
-- use a while read
loop instead.– Gordon Davisson
Feb 28 at 9:48
Don't read lines with
for
-- use a while read
loop instead.– Gordon Davisson
Feb 28 at 9:48
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.
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%2f503509%2frunning-a-loop-from-second-line-of-a-file-in-a-bash-script%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
Is the loop doing something with these numbers that could be done in a single
awk
script?– Kusalananda
Mar 4 at 18:20
Related: Why is using a shell loop to process text considered bad practice?
– Kusalananda
Mar 4 at 18:45