head usage


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting head usage
# 1  
Old 02-17-2008
head usage

$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
# 2  
Old 02-17-2008
for variable declaratoin, its c=1, not $c=1
# 3  
Old 02-17-2008
Quote:
Originally Posted by vasuarjula
$ct=1
head -n $ct file.
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'
Code:
bash$ cat yourscript                          # see the script
$ct=1
head -n $ct file

and then execute with -x:
Code:
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')
Code:
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

# 4  
Old 02-18-2008
head usage

Quote:
Originally Posted by qneill
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'
Code:
bash$ cat yourscript                          # see the script
$ct=1
head -n $ct file

and then execute with -x:
Code:
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')
Code:
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
# 5  
Old 02-18-2008
then show the whole code you have.
# 6  
Old 02-18-2008
Try this ..

## 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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Memory usage per user,percent usage,sytem time in ksh

Let's say i have 20 users logged on Server. How can I know how much memory percent used each of them is using with system time in each user? (2 Replies)
Discussion started by: roy1912
2 Replies

2. UNIX for Dummies Questions & Answers

Command to display the space usage (memory usage) of a specific directory.

Hi all, Can you please tell me the command, with which one can know the amount of space a specific directory has used. df -k . ---> Displays, the amount of space allocated, and used for a directory. du -k <dir name> - gives me the memory used of all the files inside <dir> But i... (2 Replies)
Discussion started by: abhisheksunkari
2 Replies

3. UNIX for Dummies Questions & Answers

find and head -1

i have lots of files in /law/prod and /law/dev, such as AP20PD, AP20WS, AP20.scr, AP20.rpt if i am in /law DIR find . -name AP20PD, found in /law/prod and /law/dev i want to head -1 AP20PD from both location and >> /tmp/test.log can i use find and head in one line ? ----------... (1 Reply)
Discussion started by: tjmannonline
1 Replies

4. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

5. Shell Programming and Scripting

head followed by variable??

I know that the common use of head is for example head -3 etc.Is there any possibility that,if i have a variable that equals to an integer(i=5),i can write head -i?? If not,what syntax or commands should i write down in order to have the same result? //maybe something lik head -"$variable" ? (2 Replies)
Discussion started by: bashuser2
2 Replies

6. Solaris

current CPU usage, memory usage, disk I/O oid(snmp)

Hi, I want to monitor the current cpu usage, monitor usage , disk I/o and network utlization for solaris using SNMP. I want the oids for above tasks. can you please tell me that Thank you (2 Replies)
Discussion started by: S_venkatesh
2 Replies

7. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

8. Shell Programming and Scripting

head command

Hi All, How can the head command be used to extract only a particular line. By default head -n filename displays the first n lines. I want only the nth line. I couldn't get it from forum search. Thanks, Sumesh (6 Replies)
Discussion started by: sumesh.abraham
6 Replies

9. Programming

Monitor CPU usage and Memory Usage

how can i monitor usages of CPU, Memory, Hard disk etc. under SUN Solaries through a c program or java program i want to store that data into database so i can show it graphically thanks in advance (2 Replies)
Discussion started by: Gajanad Bihani
2 Replies

10. UNIX for Dummies Questions & Answers

help.. I am in way over my head !!!!

my boss has done it again I have been sent to fix a unix issue and I ma hoping you can help three issues 1st. I have a printer that when you try to print to it the print job comes out on a diffrent printer. If I take the printer ( dot matrix thourgh a serail connection) to a diffrent local the... (3 Replies)
Discussion started by: oberon42
3 Replies
Login or Register to Ask a Question