delete last X lines,which start with [0-9]+
example file:
test1
test2
[23] remove1
[24] remove2
one liner shell is preferred.
delete last X lines,which start with [0-9]+
example file:
test1
test2
[23] remove1
[24] remove2
one liner shell is preferred.
so what did you try?
Also, I want a cup of coffee and a croissant!
Why don't you tell us what you have tried so far?
I figured it out, But I don't like the trick of tac.
[Help still needed, Solaris doesn't have tac command.]
$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
from above sample, what do you need removed? All the lines within [ ] ?
test1
test2
[23] remove1
test3
[24] remove2
[1] remove
Could this help you.
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]}}'
awk '/^\[[0-9]+\]/{s=s (s""?RS:x) $0;next}s""{print s; s=x}1' infile
Hi Scrutinizer , Could you please explain the code ?
Hi Pravin, if the line starts with one or more numbers in square brackets then append the line to string s. If the string is not empty then insert a newline character in between and process the next line..
Else (if the line start with something else), if the string s is not empty then print it and set it to an empty string. Print the current line (1).