How to extract first and last line of different record from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract first and last line of different record from a file
# 1  
Old 02-02-2009
How to extract first and last line of different record from a file

Hi all

I want to inquire that is there any unix command that can help me while extracting first and last line in a file ( TEST.dat)
for example in the below record i want to extract the line that are in BOLD text or in other words i want to extract line no 1,3,4 and 7

aa 1 2 3
aa 2 3 4
aa 3 4 5
bb 1 2 3
bb 2 3 4
bb 3 4 5
bb 4 5 6


Please help me, quick response will be highly appericiated

Regards,
BungashSmilie
# 2  
Old 02-02-2009
Please don't hijack existing threads,
start your own instead.

Thank you!

Post moved.

Last edited by radoulov; 02-02-2009 at 06:54 AM..
# 3  
Old 02-02-2009
Use GNU awk (gawk), New awk (nawk) or POSIX awk (/usr/xpg4/bin/awk):

Code:
awk 'END { print l }
!f[$1]++ { printf "%s\n", \
l ? l RS $0 : $0 }
{ l = $0 }' infile

# 4  
Old 02-02-2009
i have problem similar like that (almost sameSmilie i want only the last lines of records. The awk code is above working perfectly. but i cant convert it for my caseSmilie can you (or anybody Smilie) help me too?

i want these bold lines:

1233625770 416
1233625770 788
1233625771 912
1233625771 5252
1233625772 5376
# 5  
Old 02-03-2009
Code:
awk 'END { print l }
!f[$1]++ && l { print l }
{ l = $0 }' infile

# 6  
Old 02-03-2009
radoulov, thanks for your response. regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl to extract information from a file line by line

In the below perl code I am using tags within each line to extract certain information. The tags that are used are: STB >0.8 is STRAND BIAS otherwise GOOD FDP is the second number GO towards the end of the line is read into an array and the value returned is outputed, in the first line that... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Extract timestamp from first record in xml file and it checks if not it will replace first record

I have test.xml <emp><id>101</id><name>AAA</name><date>06/06/14 1811</date></emp> <Join><id>101</id><city>london</city><date>06/06/14 2011</date></join> <Join><id>101</id><city>new york</city><date>06/06/14 1811</date></join> <Join><id>101</id><city>sydney</city><date>06/06/14... (2 Replies)
Discussion started by: vsraju
2 Replies

3. Shell Programming and Scripting

Extract record from file based on section.

input file output file (1 Reply)
Discussion started by: lathigara
1 Replies

4. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

5. Shell Programming and Scripting

Reject the record if the record in the next line does not begin with 2.

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten 2eleven 2twelve 1thirteen 2fourteen The output should be: (5 Replies)
Discussion started by: supchand
5 Replies

6. Shell Programming and Scripting

Reject the record if the record in the next line does not satisfy the pattern

Hi, I have a input file with the following entries: 1one 2two 3three 1four 2five 3six 1seven 1eight 1nine 2ten The output should be 1one 2two 3three 1four 2five 3six (2 Replies)
Discussion started by: supchand
2 Replies

7. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

8. Shell Programming and Scripting

Extract a line from a file using the line number

I have a shell script and want to assign a value to a variable. The value is the line exctrated from a file using the line number. The line number it is not fix, and could change any time. I have tried sed, awk, head .. See my script # Get randome line number from the file #selectedline = `awk... (1 Reply)
Discussion started by: zambo
1 Replies

9. 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

10. Shell Programming and Scripting

how to extract last line in record

Hi all!! After experiencing great helpfulness the last time I posted a problem at this site, I once again turn to the forum for expert help. The problem: I have a file.dat containing x, y, and z coordinates, like: x y z 1 1 4 1 2 3 1 3 9 2 1 7 2 2 2 2 3 8 3 1 ... (7 Replies)
Discussion started by: bjorb
7 Replies
Login or Register to Ask a Question