Conditionally delete last X lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditionally delete last X lines
# 1  
Old 02-13-2011
Conditionally delete last X lines

delete last X lines,which start with [0-9]+

example file:
Code:
test1
test2
[23] remove1 
[24] remove2

one liner shell is preferred.
# 2  
Old 02-13-2011
so what did you try?
# 3  
Old 02-13-2011
Also, I want a cup of coffee and a croissant!

Why don't you tell us what you have tried so far?
# 4  
Old 02-13-2011
I figured it out, But I don't like the trick of tac.
[Help still needed, Solaris doesn't have tac command.]

Code:
$cat t2
test1
test2
[23] remove1 
test3
[24] remove2
[1] remove

$tac t2 | awk '{ if (($0 ~ /^\[[0-9]+\]/ )&&( flag!=1 )) { } else {print $0; flag=1 } } ' | tac
test1
test2
[23] remove1 
test3


Last edited by honglus; 02-14-2011 at 12:18 AM..
# 5  
Old 02-14-2011
from above sample, what do you need removed? All the lines within [ ] ?

Code:
test1
test2
[23] remove1 
test3
[24] remove2
[1] remove

# 6  
Old 02-14-2011
Could this help you.

Code:
awk '{a[++i]=$0} END{for(j=i;j>=1;j--){print a[j]}}' inputfile | awk '{if (($0 ~ /^\[[0-9]+\]/ )&&( flag!=1 )) { } else {a[++i]=$0;flag=1 } } END{for(j=i;j>=1;j--){print a[j]}}'

# 7  
Old 02-14-2011
Code:
awk '/^\[[0-9]+\]/{s=s (s""?RS:x) $0;next}s""{print s; s=x}1' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop to get rid of first 2 lines(conditionally)

hello all, I get data from different vendors and need to clean it up. Usually it pretty straight forward when i have files that only have headers....but in my case i have files that have a starting line of file name(and some junk info) and 2nd line is headers and the 3rd line is were the actual... (11 Replies)
Discussion started by: crazy_max
11 Replies

2. Shell Programming and Scripting

Sed/awk to delete single lines that aren't touching other lines

Hello, I'm trying to figure out how to use sed or awk to delete single lines in a file. By single, I mean lines that are not touching any other lines (just one line with white space above and below). Example: one two three four five six seven eight I want it to look like: (6 Replies)
Discussion started by: slimjbe
6 Replies

3. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 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. Shell Programming and Scripting

sed to conditionally delete multiple lines

Hello I'd like to remove any line in an output file that is preceded by one or more warning messages (each warning is on a separate line). Example : WARNING: Estimation of lower confidence limit of \rho failed; setting it to 0. 822 28447 ... (4 Replies)
Discussion started by: jossojjos
4 Replies

6. Shell Programming and Scripting

need to delete all lines from a group of files except the 1st 2 lines

Hello, I have a group of text files with many lines in each file. I need to delete all the lines in each and only leave 2 lines in each file. (3 Replies)
Discussion started by: script_op2a
3 Replies

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

8. Shell Programming and Scripting

sed problem - delete all lines until a match on 2 lines

First of all, I know this can be more eassily done with perl or other scripting languages but, that's not the issue. I need this in sed. (or wander if it's possible ) I got a file (trace file to recreate the control file from oracle for the dba boys) which contains some lines another line... (11 Replies)
Discussion started by: plelie2
11 Replies

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

10. UNIX for Dummies Questions & Answers

Conditionally joining lines in vi

I've done this before but I can't remember how. Too long away from vi. I want to do a search are replace, but I want the replace to be a join. Example see spot run see spot walk see spot run fast see spot hop %s/run$/<somehow perform a join with the next line>/g so the results... (0 Replies)
Discussion started by: ifermon
0 Replies
Login or Register to Ask a Question