Vijay06
September 25, 2008, 4:27am
1
I have a for loop in my script as shown below.
for file_path in $file_list ; do
........my code
..........
......
done
Can i restrict the number of files parsing to the variable file_path as 50?
That is, even if I have some 100 files in file_list, I need to take only 50 files for the loop every time.
Is that possible? Can someone help me with this?
Thanks,
Vijay
for file_path in `cat filelist.txt|head -50`
do
echo $file_path
--
---
YOUR_CODE_HERE
----
done
head -50 file | while read file_path
do
.......
done
Vijay06
September 25, 2008, 5:06am
4
But.. this doesn't solve my request.
Because.. i read it from a variable $file_list.
This contains the input as following:
echo $file_list
aaa.txt bbb.txt ccc.txt ddd.txt .......and so on.
So all file names will be in a single line. so head -50 will return all the files.. right?
any other way to get the first 50 names in the variable?
era
September 25, 2008, 5:37am
5
Just maintain a counter in the loop, and exit when it reaches 50.
n=0
for file_path in $file_list; do
: : : stuff
n=`expr $n + 1`
case $n in 50) break;; esac
done
filelist="1 2 3 4 5 6 7 8 9 10"
for filepath in `echo $filelist|cut -d" " -f1-5`
do
echo $filepath
done
OUTPUT
1
2
3
4
5
Instead of 5 use 50.
era
September 25, 2008, 7:23am
7
Assuming that spacing in the variable is regular. If it might contain tabs or newlines, awk might be a better solution. Or maybe
perl -le '$,=" "; print @ARGV[0..49]' "$@"
rubin
September 25, 2008, 8:13am
8
Maybe this will help,
echo "$file_list" | awk '{ for(i=1;i<51;i++) print $i }' | while read file
do
# actions with "$file"
done