-1

I am to delete all stings from starting of file to a certain point using anything, sed, Grep, awk in shell

INPUT

sCSISmart20 TRAP-TYPE                                  
    ENTERPRISE  cyclone                                                  
    DESCRIPTION                                          
    "Aspi: unable to read the file server hard disk might have problems"                           
    --#TYPE "Aspi: unable to read the database file"                                 
    --#SUMMARY "ASPI: unable to read the file, server hard disk may have problems"                                     
    --#ARGUMENTS {}                                 
    --#SEVERITY WARNING                                         
    --#TIMEINDEX 100                                                    
    --#STATE OPERATIONAL                                          
    --#HELP "scsismrt.hlp"                                                      
    --#HELPTAG 124                         
::=  124                                                                                            

sCSISmart21 TRAP-TYPE                                     
    ENTERPRISE  cyclone                                                
    DESCRIPTION                                                      
    "Aspi: database is corrupted"                                                  
    --#TYPE "Aspi: database is corrupted"                                          
    --#SUMMARY "ASPI: database file is corrupted"                                              
    --#ARGUMENTS {}                                                             
    --#SEVERITY WARNING                   
    --#TIMEINDEX 100                           
    --#STATE OPERATIONAL                              
    --#HELP "scsismrt.hlp"                        
    --#HELPTAG 125
::=  125                

sCSISmart12 TRAP-TYPE                                                
    ENTERPRISE  cyclone                                                        
    VARIABLES {cycHostAdapterNumber, cycScsiTargetID, cycLun, cycVendor, cycProduct, cycSenseInfo}                                                       
    DESCRIPTION                           
    "The HostAdapter# %d, TargetID %d, Lun# %d has Predictive Failure Condition on vendor %s product %s with sense info MSB(sense code), next  8 bits (sense code Qual) next 8 bits (Add sense code Qual) LSB (0000) %d"            
    --#TYPE "Device has SMART/Predicictive failure event"                   
    --#SUMMARY "The HostAdapter# %d , TargetID %d, Lun# %d has Predictive Failure Condition on vendor %s product %s with senseinfo %d"                  
    --#ARGUMENTS {0,1,2,3,4,5}                      
    --#SEVERITY INFORMATIONAL                
    --#TIMEINDEX 100                        
    --#STATE OPERATIONAL                       
    --#HELP "scsismrt.hlp"                     
    --#HELPTAG 116                                 
::=  116                       

CODE

sed/grep/awk (something that deletes from first line to ::= xxx(a random number))

OUTPUT

                                                  
sCSISmart21 TRAP-TYPE                                     
    ENTERPRISE  cyclone                                                
    DESCRIPTION                                                      
    "Aspi: database is corrupted"                                                  
    --#TYPE "Aspi: database is corrupted"                                          
    --#SUMMARY "ASPI: database file is corrupted"                                              
    --#ARGUMENTS {}                                                             
    --#SEVERITY WARNING                   
    --#TIMEINDEX 100                           
    --#STATE OPERATIONAL                              
    --#HELP "scsismrt.hlp"                        
    --#HELPTAG 125
::=  125                

sCSISmart12 TRAP-TYPE                                                
    ENTERPRISE  cyclone                                                        
    VARIABLES {cycHostAdapterNumber, cycScsiTargetID, cycLun, cycVendor, cycProduct, cycSenseInfo}                                                       
    DESCRIPTION                           
    "The HostAdapter# %d, TargetID %d, Lun# %d has Predictive Failure Condition on vendor %s product %s with sense info MSB(sense code), next  8 bits (sense code Qual) next 8 bits (Add sense code Qual) LSB (0000) %d"            
    --#TYPE "Device has SMART/Predicictive failure event"                   
    --#SUMMARY "The HostAdapter# %d , TargetID %d, Lun# %d has Predictive Failure Condition on vendor %s product %s with senseinfo %d"                  
    --#ARGUMENTS {0,1,2,3,4,5}                      
    --#SEVERITY INFORMATIONAL                
    --#TIMEINDEX 100                        
    --#STATE OPERATIONAL                       
    --#HELP "scsismrt.hlp"                     
    --#HELPTAG 116                                 
::=  116   

So you see only first part of the file has been deleted.Later I can put this in a LOOP so that I delete one paragraph at a time.
ALSO each file is separated by a blank link, So you could also tell me something that deletes from first line to the first blank line it encounters.

  • 2
    Learn to format your question properly first. Look at your last post and how I formatted it, and askubuntu.com/help/formatting – muru Jun 16 '15 at 6:52
  • okay sorry about this. I will be sure its done properly next time. – SamFlynn Jun 16 '15 at 6:57
  • And what is the "certain point"? – A.B. Jun 16 '15 at 9:51
1

Using sed:

< inputfile sed '1,/::=/d' > outputfile
  • < inputfile: redirects the content of inputfile to sed's stdin
  • > outputfile: redirects the content of sed's stdout to outputfile

sed command breakdown:

  • 1,/::=/d: deletes all the lines between the first one and the first one matching the ::= regex inclusive

Using awk:

< inputfile awk 'NR==1,/::=/ {next}; {print}' > outputfile
  • < inputfile: redirects the content of inputfile to sed's stdin
  • > outputfile: redirects the content of sed's stdout to outputfile

awk command breakdown:

  • NR==1,/::=/ {next}: skips the record if between the first one and the first one matching the ::= regex inclusive
  • {print}: prints the record

Using Perl:

 < inputfile perl -0777 -pe 's/^(.*\n)*?::=.*\n//' > outputfile
  • -0777: slurps the whole file at once instead of one line at the time
  • -p: places a while (<>) {[...]} loop around the script and prints the processed file
  • -e: reads the script from the arguments

Perl command breakdown:

  • s: asserts to perform a substitution
  • /: starts the pattern
  • ^(.*\n)*?::=.*\n: matches any character zero or more times at the start of the file greedily within the current line (i.e. the (.) pattern will be matched the most times as possible within the current line) and a newline, zero or more times lazily within the current file (i.e. the (.*\n) pattern will be matched the least times as possible within the current file) before a ::= string, matching then any character zero or more times greedily within the current line and a newline
  • /: stops the pattern / starts the replacement string
  • /: stops the replacement string / starts the modifiers
  • You're getting good at answering these questions;) +1 – Sergiy Kolodyazhnyy Jun 16 '15 at 9:23
  • Separator lines added =) – A.B. Jun 16 '15 at 10:05
1

Another awk version:

awk '/sCSISmart(12|20)\s+TRAP-TYPE/,/::=/ {print}' foo
  • /sCSISmart(12|20)\s+TRAP-TYPE/

    matches sCSISmart12 TRAP-TYPE and sCSISmart20 TRAP-TYPE

    specify what you need


Output

sCSISmart20 TRAP-TYPE                                  
    ENTERPRISE  cyclone                                                  
    DESCRIPTION                                          
    "Aspi: unable to read the file server hard disk might have problems"                           
    --#TYPE "Aspi: unable to read the database file"                                 
    --#SUMMARY "ASPI: unable to read the file, server hard disk may have problems"                                     
    --#ARGUMENTS {}                                 
    --#SEVERITY WARNING                                         
    --#TIMEINDEX 100                                                    
    --#STATE OPERATIONAL                                          
    --#HELP "scsismrt.hlp"                                                      
    --#HELPTAG 124                         
::=  124                                                                                            
sCSISmart12 TRAP-TYPE                                                
    ENTERPRISE  cyclone                                                        
    VARIABLES {cycHostAdapterNumber, cycScsiTargetID, cycLun, cycVendor, cycProduct, cycSenseInfo}                                                       
    DESCRIPTION                           
    "The HostAdapter# %d, TargetID %d, Lun# %d has Predictive Failure Condition on vendor %s product %s with sense info MSB(sense code), next  8 bits (sense code Qual) next 8 bits (Add sense code Qual) LSB (0000) %d"            
    --#TYPE "Device has SMART/Predicictive failure event"                   
    --#SUMMARY "The HostAdapter# %d , TargetID %d, Lun# %d has Predictive Failure Condition on vendor %s product %s with senseinfo %d"                  
    --#ARGUMENTS {0,1,2,3,4,5}                      
    --#SEVERITY INFORMATIONAL                
    --#TIMEINDEX 100                        
    --#STATE OPERATIONAL                       
    --#HELP "scsismrt.hlp"                     
    --#HELPTAG 116                                 
::=  116 
1

Pure BASH approach

The script below takes a file as command-line argument and prints all lines only after first ::= string is encountered.

#!/bin/bash
main(){
    local flag=false
    while IFS= read -r line;
    do 
        case "$line" in
            *"::="*) flag=true && continue ;;
        esac
        $flag && echo "$line"
    done  < "$1"
}
main "$@"

Test run (agrees with OP's desired output):

$ ./remove_paragraph.sh  input.txt                                                                    

sCSISmart21 TRAP-TYPE                                     
    ENTERPRISE  cyclone                                                
    DESCRIPTION                                                      
    "Aspi: database is corrupted"                                                  
    --#TYPE "Aspi: database is corrupted"                                          
    --#SUMMARY "ASPI: database file is corrupted"                                              
    --#ARGUMENTS {}                                                             
    --#SEVERITY WARNING                   
    --#TIMEINDEX 100                           
    --#STATE OPERATIONAL                              
    --#HELP "scsismrt.hlp"                        
    --#HELPTAG 125

sCSISmart12 TRAP-TYPE                                                
    ENTERPRISE  cyclone                                                        
    VARIABLES {cycHostAdapterNumber, cycScsiTargetID, cycLun, cycVendor, cycProduct, cycSenseInfo}                                                       
    DESCRIPTION                           
    "The HostAdapter# %d, TargetID %d, Lun# %d has Predictive Failure Condition on vendor %s product %s with sense info MSB(sense code), next  8 bits (sense code Qual) next 8 bits (Add sense code Qual) LSB (0000) %d"            
    --#TYPE "Device has SMART/Predicictive failure event"                   
    --#SUMMARY "The HostAdapter# %d , TargetID %d, Lun# %d has Predictive Failure Condition on vendor %s product %s with senseinfo %d"                  
    --#ARGUMENTS {0,1,2,3,4,5}                      
    --#SEVERITY INFORMATIONAL                
    --#TIMEINDEX 100                        
    --#STATE OPERATIONAL                       
    --#HELP "scsismrt.hlp"                     
    --#HELPTAG 116

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.