1

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
3

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!

0

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.

0

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
-1

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
-1

Not recommended if your file is large

You can use like:

 for i in $(sed -e '1d' abc.txt)
 do
   something
 done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.