I tried to extract 11 characters (default) enclosed with in tag from a file using cut command. If the characters comprises a double space, the cut command gives the output with a single spacing.
The file I am working on looks like :
blablaFirst lastblabla
The command I have used is :
cut -c7-18 file.txt
The non desired result is (only one space instead of two between first and last) :
First last
Here is the main code :
c_line="file.txt"
targetfile="result.txt"
var1=0
while read c_line
do
var1=`grep -c "IIT Chennai" $c_line`
echo $var1 >> $targetfile
if [ $var1 -ge 1 ]
then
val=`cut -c7-18 $c_line`
echo $var >> $targetfile
fi
done<${c_line}
I rather keep using cut
do you know what is the problem with cut ?
I'm assuming that this part of your script is a typo:
val=`cut -c7-18 $c_line`
echo $var >> $targetfile
# ^ should be $val
So the problem is that you're using $val
, when you should be using "$val"
.
When you expand a variable without quotes, word-splitting occurs, so echo
sees two arguments:
echo 'First' 'last'
These arguments are printed, separated by a single space.