Showing the first 4 lines of a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Showing the first 4 lines of a file?
# 1  
Old 10-14-2010
Showing the first 4 lines of a file?

Is there a way to show the first 4 lines of a file without using head -4?
In sed would it be sed '1,4d' ?

What if I just wanted to display the 2nd line ONLY?

How could this be done with AWK?...correctly with SED?

Last edited by puttster; 10-14-2010 at 10:10 PM..
# 2  
Old 10-14-2010
Code:
$ ruby -ne '(print and exit) if $.==2 ' file

This User Gave Thanks to kurumi For This Post:
# 3  
Old 10-14-2010
Code:
awk 'NR<=4' infile

awk 'NR==2' infile

This User Gave Thanks to rdcwayx For This Post:
# 4  
Old 10-14-2010
Thanks problem solved
# 5  
Old 10-15-2010
Using sed

to print 2nd line alone
sed -n 2p filename

to print first four line

sed -n 1,4p filename
# 6  
Old 10-15-2010
Quote:
Originally Posted by puttster
Is there a way to show the first 4 lines of a file without using head -4?
In sed would it be sed '1,4d' ?

What if I just wanted to display the 2nd line ONLY?

How could this be done with AWK?...correctly with SED?

It can be done in the shell without using any external command:
Code:
{
 IFS= read -r line1
 IFS= read -r line2
 IFS= read -r line3
 IFS= read -r line4
} < "$file"

Then you can print whichever lines you want, e.g., lines 1 and 3:
Code:
printf "%s\n" "$line1" "$line3"

# 7  
Old 10-15-2010
If you have a large file we can get sed to exit as soon as the job is done rather than trawl the whole file:

Code:
First four lines:
sed '4q' largefile

Second line only:
sed '2q;d' largefile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Binary file for showing last reboot

Does anyone know what is the binary file for seeing the last reboot since my wtmpx file has been dumped as of now and i am not able to see the last reboot with the command "last reboot". Please don't say uptime, since i want to see the last 4-5 reboots for this server. Thanks in advance (4 Replies)
Discussion started by: aksijain
4 Replies

2. UNIX for Dummies Questions & Answers

Why Linux is not showing file in use ?

Hi i have written a infinite loop which writes to a file log.txt while do echo " file in use " >> log.txt done i have started this process in one terminal , from another terminal i issued cp command cp log.txt log2.txt i was expecting a File in use message but didnt ? i have... (6 Replies)
Discussion started by: rakeshkumar
6 Replies

3. Solaris

My /etc/mnttab file is showing ro permission for /usr

Hi All, My current /usr utilization is 100%.As i don't have another harddisk i decided to delete some unnecessary files under /usr.But,while deleteing it is giving error "it is a readonly filesystem".I checked /etc/mnttab file it is showing the following entry /dev/lofi/1 /usr hsfs ... (3 Replies)
Discussion started by: navjotmannan
3 Replies

4. AIX

not showing the year of file

Hi I am facing strange issue in one direcotry it is not showing year of file, can you please suggest me wheather there is any limitation on year, on some other file though it is showing the dates. Regards, Manoj. (1 Reply)
Discussion started by: manoj.solaris
1 Replies

5. UNIX for Dummies Questions & Answers

Showing a file's symbolic links

ls -l shows the number of links for each file. Is there a command that will show the link sources for a specific file? Running find on the entire filesystem and doing a little Perl "magic" is the only method I'm aware of. I'm running SunOS 5.8. Thanks. (6 Replies)
Discussion started by: effigy
6 Replies

6. UNIX for Advanced & Expert Users

fuser not showing file open by vi

hi, I opened a simple text file by vi. I then started another shell, and did fuser myFile. I expected it to show the pid of the vi session that had that file open, but it just returned a blank. why would this be? thanks (4 Replies)
Discussion started by: JamesByars
4 Replies

7. Shell Programming and Scripting

Help with showing the difference in two lines of input

I would like my script to be able to tell the difference between to lines of input, like: Input 1: 1 2 3 4 5 Input 2: 1 2 3 4 5 6 I want the script to tell me that the difference between the two lines is the 6. Is there anyway I can have it do this? Here's an example of what my script... (12 Replies)
Discussion started by: Kweekwom
12 Replies

8. Shell Programming and Scripting

Showing extra line/record in file

Hello everybody, My job is to load the data from Oracle table to flat file and from flat file to oracle table using ETL tool Informatica. My flat files are fixed width. In the first phase, it is loading 66351 records into data file through tool. When I checked through wc -l <data filename> it is... (1 Reply)
Discussion started by: srivsn
1 Replies

9. UNIX for Dummies Questions & Answers

counting lines and showing the output

First time poster - I have a huge file and i want to sort and compress it to something more readable Ex: FUTNCA01-SL1 DMT8a4 5 3 FUTNCA01-SL1 DMT8a4 5 9 FUTNCA01-SL1 DMT8a4 5 21 FUTNCA01-SL1 DMT8a4 5 22 FUTNCA01-SL1 DMT8a4 5 23 FUTNCA01-SL1 DMT8a4 5 24 FUTNCA01-SL1 DMT8a4 6 2... (13 Replies)
Discussion started by: jjoves
13 Replies

10. UNIX for Dummies Questions & Answers

word count showing wrong number of lines

Hi , I am using SUN OS Version 5.6. I have a file that contains records of length 270. when I do 'set nu' in vi editor, I get the count as 86. whereas when I do "wc -l" on the command prompt, it shows the count as only 85. this is very strange. why would the 'wc' show 1 record less. The job... (3 Replies)
Discussion started by: tselvanin
3 Replies
Login or Register to Ask a Question