loopless
first use a function
function sevenc
{
if [ ! -e "$1" ]; then #check if the file exist
echo "File $1 does not exist" #if not exist print echo output
else
sed -i -e 's/- /-/g' "$1" #remove space on the first 10 values
awk '{print $7}' "$1" > /tmp/$(basename $1.txt)_S.txt #print the column number 7 and copy the output in a file
rm "$1" #remove old file
fi
}
- when the shell recognize a function, it will pass argument (if any to $1 $2 ... and so on).
- by the way
's/- /-/g' "$1" #remove space on the first 10 values
NO, it turn all space- to - on the line, be there 1, 4, 10 or 255.
then no need for more var
sevenc /tmp/1wall_long.txt
sevenc /tmp/1wall_test1.txt
sevenc /tmp/1wall_test2.txt
sevenc /tmp/1wall_test3.txt
sevenc /tmp/3mt_long.txt
sevenc /tmp/3mt_OpenSpace_test1.txt
sevenc /tmp/3mt_OpenSpace_test2.txt
sevenc /tmp/3mt_OpenSpace_test3.txt
sevenc /tmp/3rooms_test1.txt
sevenc /tmp/3rooms_test2.txt
sevenc /tmp/3rooms_test3.txt
sevenc /tmp/20mt_OpenSpace_test1.txt
sevenc /tmp/20mt_OpenSpace_test2.txt
sevenc /tmp/20mt_OpenSpace_test3.txt
(provided you have no more use of fileXX var).
loopless (sol. 2)
should you want to pass more argument, and using Terdon's optimisation try
function eight
{
file=$1
destdir=${2-/tmp} # use second arg if defined, else /tmp
exten=${3-S}
if [ ! -e "$file" ]; then #check if the file exist
echo "File $file does not exist" #if not exist print echo output
else
sed -e 's/- /-/g' "$file" \
awk '{print $7}' "$1" > /"$destdir"/$(basename $1.txt)_"$exten".txt #print the column number 7 and copy the output in a file
rm "$file" #remove old file
fi
}
to be called with
eight /tmp/1wall_test3.txt /my/projec/dir T ## will use /my/project/dir as dit, T as extension
eight /tmp/1wall_test1.txt /my/project ## will use /my/project as dir
eignt /tmp/1wall_test2.txt ## will use default value
those function can be defined in .bashrc and be use interactively.
with loop
while read f
do
if [ ! -e "$f" ]; then #check if the file exist
echo "File $1 does not exist" #if not exist print echo output
else
sed -i -e 's/- /-/g' "$f" #remove space on the first 10 values
awk '{print $7}' "$f" > "/tmp/$(basename $f .txt)_S.txt" #print the column number 7 and copy the output in a file
rm "$f" #remove old file
fi
done <<EOF
/tmp/1wall_long.txt
/tmp/1wall_test1.txt
/tmp/1wall_test2.txt
/tmp/1wall_test3.txt
/tmp/3mt_long.txt
/tmp/3mt_OpenSpace_test1.txt
/tmp/3mt_OpenSpace_test2.txt
/tmp/3mt_OpenSpace_test3.txt
/tmp/3rooms_test1.txt
/tmp/3rooms_test2.txt
/tmp/3rooms_test3.txt
/tmp/20mt_OpenSpace_test1.txt
/tmp/20mt_OpenSpace_test2.txt
/tmp/20mt_OpenSpace_test3.txt
EOF