Operating System - HP-UX
1755738 Members
2843 Online
108837 Solutions
New Discussion юеВ

Scripting help: Print first and last line of a file ?

 
SOLVED
Go to solution
rveri
Super Advisor

Scripting help: Print first and last line of a file ?

Hi Experts,
I want to get first and last line from a file with a command by one time parsing the file:



Example:
# cat datafile
1. This is the first Line.
2. AAA BBB CCC
3. DDD EEE FFF
---- Many lines
---- Many Lines
LastLine. This is the LastLine of the File.



Output should be:
1. This is the first Line.
LastLine. This is the LastLine of the File.



Thanks in adv.
3 REPLIES 3
Raj D.
Honored Contributor
Solution

Re: Scripting help: Print first and last line of a file ?

F=datafile ;t=`wc -l $F|awk '{print $1}'`; sed -n "1p; $t p" $F
" If u think u can , If u think u cannot , - You are always Right . "
James R. Ferguson
Acclaimed Contributor

Re: Scripting help: Print first and last line of a file ?

Hi Rveri:

Simply:

# sed -ne '1p;$p' file

Regards!

...JRF...
Raj D.
Honored Contributor

Re: Scripting help: Print first and last line of a file ?

F=file ;l=`sed -n '$=' $F`;awk -v v=${l} 'NR==1||NR==v' $F
" If u think u can , If u think u cannot , - You are always Right . "