With cut -c 8-13 myfile,
I am getting some numeric value.
In my shell script I am trying to assign something like this,
var=cut -c 8-13 myfile
But at the time of execution I am getting -c is not found.
If I dont assign, then script executes well.
Can we not simply use the value from one command output into the
variable in shell script?
RTM
December 14, 2004, 10:43am
2
Instead of
var=cut -c 8-13 myfile
try
var=`cut -c 8-13 myfile`
Meanwhile I started with awk. So on the similar lines I am trying to evaluate
awk o/p to var.
start=2
len=10
var=`awk <"$1""myfile" '{print substr($0,$start,$len)}'`
echo $var
After executing, I am getting awk error.
If at the place of $start & $len, I put 2 & 10 resp, it works.
Where am I making a mistake?
What exactly is this line
var=`awk <"$1""myfile" '{print substr($0,$start,$len)}'`
supposed to be achieving?
Without knowing exactly what your problem is, something like
var=`awk -vs=$start -vl=$length '{print substr($0,s,l)}' filename`
may be more appropriate.
Cheers
ZB
Zazzybob
Yes,
that works. But let me explain what was my thought process behind prev command.
awk < filename - I understand as, file 'filename' is an input to awk. $1 param consists
dir loc where the file is residing.
In my fixed width file, I have these numeric values. Those I am retrieving with
substring.
I know that that kind of redirection will work, but it's unneccessary - you can just take the filename onto the end of the command.
Cheers
ZB
Also I would like to get rid of leading zeros, after I read a number from fixed width
file.
Say, 00000015 should be read as 15.
How would I achieve that?
Ygor
December 15, 2004, 11:14pm
8
$ echo "aaaa0000000015bbbb" > file1
$ num=$(awk '{print int(substr($0,s,l))}' s=5 l=10 file1)
$ echo $num
15