Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

How get only required lines & delete the rest of the lines in file

UNIX for Dummies Questions & Answers


 
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 06-01-2010
reva reva is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 2 June 2010, 7:15 AM EDT
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
Unix or Linux Question How get only required lines & delete the rest of the lines in file

Hiiii
I have a file which contains huge data as
a.dat:
HTML Code:
 PDE 1990  1  9 18 51 28.90  24.7500   95.2800 118.0 6.1 0.0 BURMA
event name:     010990D
time shift:      7.3000
half duration:   5.0000
latitude:       24.4200
longitude:      94.9500
depth:         129.6000
Mrr:       2.016000e+25
Mtt:      -1.576000e+25
Mpp:      -4.390000e+24
Mrt:      -2.089000e+25
Mrp:      -1.011000e+25
Mtp:       1.012000e+25

 PDE 1990  1 10 11 53 21.20  11.6000   95.1900  33.0 5.3 5.4 ANDAMAN ISLANDS REGION
event name:     011090D
time shift:      1.6000
half duration:   2.5000
latitude:       11.7100
longitude:      95.0300
depth:          61.0000
Mrr:      -2.770000e+23
Mtt:      -2.288000e+24
Mpp:       2.565000e+24
Mrt:       6.300000e+22
Mrp:      -1.190000e+24
Mtp:       6.836000e+24

 PDE 1990  1 22 17 26 12.10   3.8500   96.1000  51.0 6.0 0.0 NORTHERN SUMATERA
event name:     012290D
time shift:      7.1000
half duration:   5.0000
latitude:        3.5500
longitude:      95.9300
depth:          50.1000
Mrr:       1.837000e+25
Mtt:      -1.010000e+25
Mpp:      -8.270000e+24
Mrt:       1.084000e+25
Mrp:      -1.030000e+25
Mtp:       1.330000e+25

 PDE 1990  2  5  5 16 45.10  37.0700   71.2700 102.0 6.1 0.0 AFGHANISTAN-USSR BORDER
event name:     020590A
time shift:      8.4000
half duration:   4.9000
latitude:       36.5600
longitude:      70.8400
depth:         113.5000
Mrr:       1.749000e+25
Mtt:      -2.940000e+25
Mpp:       1.191000e+25
Mrt:      -7.030000e+24
Mrp:      -1.093000e+25
Mtp:      -5.500000e+23
I just want to retain first line & delete rest of the lines in the file
THe output file should be like
b.dat

Code:
 PDE 1990  1  9 18 51 28.90  24.7500   95.2800 118.0 6.1 0.0 BURMA
 PDE 1990  1 10 11 53 21.20  11.6000   95.1900  33.0 5.3 5.4 ANDAMAN ISLANDS
 PDE 1990  1 22 17 26 12.10   3.8500   96.1000  51.0 6.0 0.0 NORTHERN SUMATERA
 PDE 1990  2  5  5 16 45.10  37.0700   71.2700 102.0 6.1 0.0 AFGHANISTAN-USSR BORDER

Help me out.....
Sponsored Links
    #2  
Old Unix and Linux 06-01-2010
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 14 September 2017, 8:59 PM EDT
Posts: 3,148
Thanks: 45
Thanked 698 Times in 674 Posts

Code:
grep "^PDE" a.dat > output.txt

Sponsored Links
    #3  
Old Unix and Linux 06-01-2010
reva reva is offline
Registered User
 
Join Date: Jul 2009
Last Activity: 2 June 2010, 7:15 AM EDT
Posts: 41
Thanks: 0
Thanked 0 Times in 0 Posts
It works only if the line starts from PDE..But there are few lines which start from differetn charcters
for example:
a.dat:
HTML Code:
 PDE 2010  5  9  5 59 42.00   3.7800   96.0500  45.0 7.2 7.2 NORTHERN SUMATRA, INDONE
event name:     201005090559A
time shift:      9.6600
half duration:  10.0000
latitude:        3.3800
longitude:      95.7900
depth:          37.6200
Mrr:       4.410000e+26
Mtt:      -2.590000e+26
Mpp:      -1.810000e+26
Mrt:       5.750000e+26
Mrp:      -5.280000e+26
Mtp:       2.250000e+26

Previous search examined first 31195 events in catalog

Results
Output in CMTSOLUTION format

 PDEW2009  7 24  3 11 56.80  31.1600   85.9000  13.4 5.7 5.8 XIZANG
event name:     200907240311A
time shift:      5.9100
half duration:   1.9000
latitude:       31.0500
longitude:      86.1000
depth:          28.0700
Mrr:      -6.450000e+23
Mtt:      -5.720000e+24
Mpp:       6.370000e+24
Mrt:       1.710000e+24
Mrp:       6.700000e+23
Mtp:       4.520000e+23

 HSW 2010  5 29  2 30  0.00  33.2500   96.2500  33.0 0.0 5.9 QINGHAI, CHINA
event name:     201005290230A
time shift:     -5.0000
half duration:   1.9000
latitude:       33.2500
longitude:      96.3000
depth:          20.8300
Mrr:      -5.500000e+23
Mtt:      -3.130000e+24
Mpp:       3.680000e+24
Mrt:      -1.500000e+23
Mrp:       2.860000e+23
Mtp:       4.750000e+24

 PDEW2009 12 13 14 41 55.80  22.0000   91.8200  10.0 5.4 0.0 BAY OF BENGAL
event name:     200912131441A
time shift:      0.7500
half duration:   0.7000
latitude:       21.8700
longitude:      91.7400
depth:          12.0000
Mrr:       2.660000e+23
Mtt:      -9.730000e+21
Mpp:      -2.570000e+23
Mrt:       2.720000e+22
Mrp:      -4.690000e+22
Mtp:       8.550000e+22
The output file should be
b.dat:
HTML Code:
 PDE 2010  5  9  5 59 42.00   3.7800   96.0500  45.0 7.2 7.2 NORTHERN SUMATRA, INDONE
 PDEW2009  7 24  3 11 56.80  31.1600   85.9000  13.4 5.7 5.8 XIZANG
 HSW 2010  5 29  2 30  0.00  33.2500   96.2500  33.0 0.0 5.9 QINGHAI, CHINA
 PDEW2009 12 13 14 41 55.80  22.0000   91.8200  10.0 5.4 0.0 BAY OF BENGAL
    #4  
Old Unix and Linux 06-01-2010
dennis.jacob's Unix or Linux Image
dennis.jacob dennis.jacob is offline Forum Advisor  
Registered User
 
Join Date: Feb 2007
Last Activity: 28 July 2017, 12:24 AM EDT
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 12 Times in 11 Posts
Try:


Code:
sed '/ *PDE/!d' a.out

Sponsored Links
    #5  
Old Unix and Linux 06-01-2010
itkamaraj's Unix or Linux Image
itkamaraj itkamaraj is offline Forum Advisor  
Registered User
 
Join Date: Apr 2010
Last Activity: 14 September 2017, 8:59 PM EDT
Posts: 3,148
Thanks: 45
Thanked 698 Times in 674 Posts
try this


Code:
sed -n '1,${p;n;n;n;n;n;n;n;n;n;n;n;n;n;}' file_name

Sponsored Links
    #6  
Old Unix and Linux 06-01-2010
posix's Unix or Linux Image
posix posix is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 21 April 2017, 10:24 PM EDT
Location: grep "Bhubaneswar" "india"
Posts: 192
Thanks: 8
Thanked 13 Times in 13 Posts
try

Code:
awk 'NF > 5' file_name

Sponsored Links
    #7  
Old Unix and Linux 06-01-2010
dennis.jacob's Unix or Linux Image
dennis.jacob dennis.jacob is offline Forum Advisor  
Registered User
 
Join Date: Feb 2007
Last Activity: 28 July 2017, 12:24 AM EDT
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 12 Times in 11 Posts
Try:

Assuming that you need lines starting with PDE/HSE/..



Code:
sed '/^[A-Z0-9]* /!d' a.out

Sponsored Links
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to delete lines in a file that have duplicates or derive the lines that aper once necroman08 Shell Programming and Scripting 3 07-17-2009 05:07 AM
Grep and delete lines except the lines with strings vj8436 Shell Programming and Scripting 14 04-17-2009 11:25 AM
To extract the required lines from a file sureshg Shell Programming and Scripting 1 01-26-2009 05:03 AM
Help required regarding Extracting lines from a file google_ever Shell Programming and Scripting 1 10-12-2005 07:02 AM
delete blank lines or lines with spaces only vascobrito UNIX for Dummies Questions & Answers 3 01-13-2004 06:36 AM



All times are GMT -4. The time now is 11:51 PM.