Remove lines between "###"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove lines between "###"
# 1  
Old 05-28-2012
Remove lines between "###"

i have a file as below:
Code:
12 23 25 
12 22 
12 33 32 
###
12 23 56 45 77 45 56
12 54 6
###
###
1254
###

i named anything between two tandem "###" as a section, now i want to remove any section that has less than 2 lines. i wrote somthing like this code but i dont know how can i remove that section
Code:
awk '/###/ NR=x {while($1!~/###/getline;next NR=y} in > out

i think this code can compute the number of line of each section so i must write a code if y-x<4 for each section, remove that.
# 2  
Old 05-28-2012
Hi

Is this what you expected?


Code:
$ awk '/###/{if(i>=2)for(j=0;j<i;j++)print a[j];print;i=0;next}{a[i++]=$0;}' a
12 23 25
12 22
12 33 32
###
12 23 56 45 77 45 56
12 54 6
###
###
###

Guru
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 05-28-2012
yes thank you but there is a little problem can we remove extra "###" . for example here at the end there is 3 of that.it'll be more convenient if it can be removed when the data is very long in a file.
# 4  
Old 05-28-2012
Alternatively:
Code:
awk '/###/{print (n>1?p:x) $0; p=n=x; next}{p=p $0 RS; n++}' infile

or
Code:
awk '/###/{if (n>1) print p $0; p=n=x; next}{p=p $0 RS; n++}' infile

Provided input files end with a ### line, otherwise there has to be an END section too.


--
This might work too:
Code:
awk -F'\n' 'END{printf "\n"} NF>3' RS=# ORS=### infile


Last edited by Scrutinizer; 05-28-2012 at 11:36 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 05-28-2012
Thank you very much. it works perfect!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

4. Shell Programming and Scripting

Cant get awk 1liner to remove duplicate lines from Delimited file, get "event not found" error..help

Hi, I am on a Solaris8 machine If someone can help me with adjusting this awk 1 liner (turning it into a real awkscript) to get by this "event not found error" ...or Present Perl solution code that works for Perl5.8 in the csh shell ...that would be great. ****************** ... (3 Replies)
Discussion started by: andy b
3 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. UNIX for Advanced & Expert Users

Remove lines if the first character is "|"

Hi. I have a huge file (350 million lines). I need to delete all lines in it that: 1. Begin with a pipe character -- '|' 2. Or have less than 5 pipe characters in the line Have been searching for some SED/AWK help for this (which is faster btw on such a ginormous file?) but only... (7 Replies)
Discussion started by: pkiula
7 Replies

7. UNIX for Dummies Questions & Answers

Remove all lines that contain ".rar"

How do I use bash to remove all lines that contain the string ".rar" from a file? (3 Replies)
Discussion started by: locoroco
3 Replies

8. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

9. Shell Programming and Scripting

Remove ":" and join lines in outline file

I have a vim outliner file like this: Title title 2 :Testing now :testing 2 :testing 3 title 3 :testing :ttt :ttg Is there a way to use a script or command to remove... (7 Replies)
Discussion started by: jostber
7 Replies

10. Shell Programming and Scripting

How to remove "New line characters" and "spaces" at a time

Dear friends, following is the output of a script from which I want to remove spaces and new-line characters. Example:- Line1 abcdefghijklmnopqrstuvwxyz Line2 mnopqrstuvwxyzabcdefghijkl Line3 opqrstuvwxyzabcdefdefg Here in above example, at every starting line there is a “tab” &... (4 Replies)
Discussion started by: anushree.a
4 Replies
Login or Register to Ask a Question