delete lines in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers delete lines in a file
# 1  
delete lines in a file

I've got a file like this:

Grid-ref= 443, 229
167 169 204 233 290 309 308 326 300 251 194 161
148 189 228 251 296 329 331 338 308 263 219 179
178 203 215 252 277 319 327 335 312 264 196 149
120 172 226 253 297 329 323 322 305 242 203 136
158 191 224 237 301 326 321 339 321 263 212 170
181 168 209 256 312 335 331 346 325 274 214 175
157 167 193 241 297 323 329 328 322 263 201 165
160 154 217 221 263 318 326 327 312 268 218 184
155 178 246 255 302 332 323 326 314 279 214 172
155 191 215 270 301 327 314 331 311 277 232 161
Grid-ref= 443, 230
159 163 199 231 291 311 308 328 299 251 192 155
142 182 223 248 297 330 331 340 309 263 215 172
172 196 209 250 276 320 326 337 313 263 194 142
113 166 222 251 297 329 323 322 304 241 200 131
151 185 219 234 301 326 321 340 321 260 208 164
174 164 204 254 312 335 330 347 325 271 212 169
149 160 189 239 297 323 330 326 322 263 195 157
154 149 212 221 264 320 326 329 312 265 214 178
149 173 243 252 302 332 323 328 314 276 209 167
150 185 211 270 302 328 315 331 310 276 229 154
Grid-ref= 443, 231
154 160 197 232 292 315 311 332 300 254 191 153
140 177 222 248 299 332 333 343 311 264 212 169
170 192 206 251 276 323 329 341 315 266 193 140
109 163 220 250 298 332 325 324 305 242 199 129
146 180 216 234 302 329 323 343 322 260 206 161
170 162 202 255 312 337 331 349 325 271 212 166
145 155 187 239 296 325 333 325 322 266 192 154
151 146 211 222 265 324 330 331 314 266 212 174
146 170 244 251 302 335 325 331 317 276 205 167
147 180 210 271 303 332 318 332 311 279 228 150
Grid-ref= 443, 232
147 156 191 229 290 312 311 332 297 252 188 149
135 172 217 243 297 329 333 343 309 263 209 164
165 187 200 247 273 320 328 341 315 263 191 135
102 159 216 246 295 329 325 323 303 239 196 126
139 175 211 230 299 325 322 343 320 256 202 157
164 158 198 251 310 333 330 348 322 266 210 161
139 149 183 232 292 322 328 322 317 263 188 149
145 142 207 219 265 321 330 331 312 264 210 160
143 167 242 244 300 334 322 331 315 273 198 166
142 175 205 268 301 331 317 329 307 277 222 143

I wanna keep the two first lines of each metrix like this:

Grid-ref= 443, 229
167 169 204 233 290 309 308 326 300 251 194 161
Grid-ref= 443, 230
159 163 199 231 291 311 308 328 299 251 192 155
Grid-ref= 443, 231
154 160 197 232 292 315 311 332 300 254 191 153
Grid-ref= 443, 232
147 156 191 229 290 312 311 332 297 252 188 149

Any help you could give me would be greatly appreciated.
su_in99
# 2  
Code:
sed -n "/^Grid-ref=/{N;p;}" file

# 3  
Many thanks for anbu23, indeed.

Do you have any idea if I wanna save it to another file.

Thanks
su_in99
# 4  
Code:
sed -n "/^Grid-ref=/{N;p;}" file >newfile

or
Code:
sed -n "/^Grid-ref=/N;/^Grid-ref=/w newfile" file

# 5  
Code:
awk ' $0 ~ "^Grid" { print > "n1"; getline; print > "n1" }' filename

# 6  
sed -n '/^Grid/{p;n;p;}' filename >newfile
# 7  
Thanks a lot for every answer.
su_in99
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #594
Difficulty: Easy
In the C programming language, variables are scoped to the code block in which they are declared and are only valid within that code block.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete Some Lines from File

Hi, I have a txt document having a format like this: DATA1 | DATA2 | DATA3 | 23-JAN-20 23:41:34 DATA1 | DATA2 | DATA3 | 23-JAN-20 23:41:32 DATA1 | DATA2 | DATA3 | 23-JAN-20 23:41:30 ... DATA1 | DATA2 | DATA3 | 23-JAN-20 22:35:31 DATA1 | DATA2 | DATA3 | 23-JAN-20 22:30:34 DATA1 | DATA2 |... (1 Reply)
Discussion started by: gc_sw
1 Replies

2. Shell Programming and Scripting

Delete 40 lines after every 24 lines from a file

Hello, I have file of more than 10000 lines. I want to delete 40 lines after every 20 lines. e.g from a huge file, i want to delete line no from 34 - 74, then 94 - 134 etc and so on. Please let me know how i can do it. Best regards, (11 Replies)
Discussion started by: nehashine
11 Replies

3. UNIX for Dummies Questions & Answers

Delete lines in a file

Hi This is a sample of my data file. ##field PH01000000 1 4869017 #PH01000000G0240 WWW278545G0240 P.he_model_v1.0 erine 119238 121805 . - . ID=PH01000000G0240;Description="zinc finger, C3HC4 type domain containing protein, expressed"... (7 Replies)
Discussion started by: sonia102
7 Replies

4. UNIX for Advanced & Expert Users

In a huge file, Delete duplicate lines leaving unique lines

Hi All, I have a very huge file (4GB) which has duplicate lines. I want to delete duplicate lines leaving unique lines. Sort, uniq, awk '!x++' are not working as its running out of buffer space. I dont know if this works : I want to read each line of the File in a For Loop, and want to... (16 Replies)
Discussion started by: krishnix
16 Replies

5. UNIX for Dummies Questions & Answers

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: 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: ... (7 Replies)
Discussion started by: reva
7 Replies

6. Shell Programming and Scripting

How to delete lines in a file that have duplicates or derive the lines that aper once

Input: a b b c d d I need: a c I know how to get this (the lines that have duplicates) : b d sort file | uniq -d But i need opossite of this. I have searched the forum and other places as well, but have found solution for everything except this variant of the problem. (3 Replies)
Discussion started by: necroman08
3 Replies

7. Shell Programming and Scripting

delete n last lines of a file

Hello!!! how can I delete the last n lines of a file??? Thanks (7 Replies)
Discussion started by: ncatdesigner
7 Replies

8. Shell Programming and Scripting

delete the lines from file

i have two files & want to delete the lines from 2nd file which matches with 1st file (2 Replies)
Discussion started by: sameersam
2 Replies

9. Shell Programming and Scripting

delete lines from a file.

I have a file which has about 500K records and I need to delete about 50 records from the file. I know line numbers and am using sed '13456,13457,......d' filename > new file. It does not seem to be working. Any help will greatly appreciated. (5 Replies)
Discussion started by: oracle8
5 Replies

10. UNIX for Dummies Questions & Answers

delete all lines in file

how can i delete all lines in file by using "vi" ? (6 Replies)
Discussion started by: strok
6 Replies

Featured Tech Videos