$ct=1
head -n $ct file.
When i used like this, i got an error , Bad usage of head
Cant we use variables in place of number in HEAD.
In my requirement for every iteration i should increase the number in Head and tail the last one.
HOw can i achieve this
for variable declaratoin, its c=1, not $c=1
A good debugging technique in most shells is to use "-x" which prints each command as it is executed. I put your commands in a script named 'yourscript'
bash$ cat yourscript # see the script
$ct=1
head -n $ct file
and then execute with -x:
bash$ bash -x yourscript # execute the script with -x
+ =1 # notice '$ct' is blank (you want 'ct')
script: line 1: =1: command not found # which causes problems
+ head -n file # '$ct' is again blank
head: file: invalid number of lines # more problems
Here's your corrected script (using 'ct=1')
bash$ cat yourscript
ct=1
head -n $ct file
bash$ cat file
blah blah one
blah blah two
bash$ bash -x yourscript
+ ct=1
+ head -n 1 file
blah blah one
sorry about that
my code is
ct=1
head -n $ct | tail -1
i will put this code in a while loop and increment ct.
so when ct =1 i will get the first record, when ct =2 then i will get the second record and so on....
But head -n $ct is not working. it says incorrect usage. Please let me know what is the problem
then show the whole code you have.
## HEAD command works with files....so try the below script..
#! /bin/ksh
# Give the file path below ..
INFILE=/home/infil1.dat
ct=1
cnt=`cat $INFILE | wc -l`
while [ $cnt -ge $ct ]
do
echo "Line no: $ct and line: "
head -n $ct $INFILE | tail -1
ct=`expr $ct + 1`
done