Hi All,
I have directory in which I have around 50 files with filename as:
abcs_1
afaa_2
asda_3
agfa_4
.
.
sada_50
I want to append all files in sada_50 i.e first ssdd_49 in sada_50. Then
append asda_48 in (ssdd_49 in sada_50).
As number of files are more I do not feel like going for "cat 48 49 > 50" and I want to write a generic script in which I mention the max file number (here 50) and I get all files in single.
I have tried to write but not I am new to shell and not able to find exact soln.
$ appendfiles.bash 50
maxcount=$1
count=maxcount
MY_DIR=/home/my/dir/
maxfile= 'ls -l' | grep $maxcount
echo $maxfile
while test $count -le 1
do
echo $count
count=`expr $count - 1`
echo $count
nextminfile= 'ls -l' | grep $count
cat MY_DIR/newDir/$nextminfile > MY_DIR/newDir/$maxfile
done
Please help.
Thanks !!
Hi,
This should get you going. You can compare the differences and take it from there:
maxcount=$1
count=$maxcount
MY_DIR=/home/my/dir/
if [ ! -d $MY_DIR/newDir ]; then
mkdir -p $MY_DIR/newDir
fi
cd $MY_DIR/newDir
# maxfile= 'ls -l' | grep $maxcount
maxfile=*_$maxcount
echo $maxfile
until test $count -le 1
do
echo $count
count=`expr $count - 1`
echo $count
# nextminfile= 'ls -l' | grep $count
nextminfile=*_$count
echo $nextminfile
cat $nextminfile >> $maxfile
done
Variable assignments do not undergo filename expansion. Those asterisks will be taken literally.
Regards,
Alister
---------- Post updated at 03:08 PM ---------- Previous update was at 03:07 PM ----------
A possible alternative or starting point:
#!/bin/sh
i=0 max=$1
while [ $((++i)) -lt $max ]; do
cat *_$i
done > "$(echo *_$max)" 2>/dev/null
Regards,
Alister
They do not need to. The expansion is done in this statement:
cat $nextminfile >> $maxfile
How much more? (How big?) - At least for me, the following worked, read: processed several hundred small plain text files without any problem (on a workstation, though):
cat $( ls *.txt ) > collection
This should work if i have understood your question properly
for i in `ls /path/to/dir`
do
`echo $i >> out.txt`
done
Hi
awk 'FNR==1{split(FILENAME,a,"_"); if (a[2]<m) y=system("cat " FILENAME);}' m=50 * > sada_50
Guru.