Remove certain lines from file based on start of line except beginning and ending | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Remove certain lines from file based on start of line except beginning and ending

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-20-2013
nwalsh88 nwalsh88 is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 13 February 2014, 12:44 PM EST
Posts: 12
Thanks: 6
Thanked 0 Times in 0 Posts
Remove certain lines from file based on start of line except beginning and ending

Hi, I have multiple large files which consist of the below format:

Quote:
00..................
06..................
06..................
06..................
06..................
70..................
80..................
00..................
06..................
06..................
06..................
06..................
70..................
80..................
00..................
06..................
06..................
06..................
06..................
70..................
80..................
etc
I am trying to write an awk or sed script to remove all occurrences of the 00 record except the first and remove all of the 80 records except the last one.

Any help would be greatly appreciated.
Sponsored Links
    #2  
Old 02-20-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 1 July 2014, 5:40 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
Is this what you want...?


Code:
awk 'NR==1
s=0
NR>1 && !/^00/
/^00/{s=1}
END{if(s){print}}' file

The Following User Says Thank You to pamu For This Useful Post:
nwalsh88 (02-20-2013)
Sponsored Links
    #3  
Old 02-20-2013
nwalsh88 nwalsh88 is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 13 February 2014, 12:44 PM EST
Posts: 12
Thanks: 6
Thanked 0 Times in 0 Posts
Thanks for your reply.

Yes that is along the lines of what i need.

That script removes the 00 records except the 1st

I also need it to remove all the 80 records but leave the last one as it is.

Any ideas....?
    #4  
Old 02-20-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 1 July 2014, 5:40 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
Quote:
Originally Posted by nwalsh88 View Post
I also need it to remove all the 80 records but leave the last one as it is.

Any ideas....?
Forgot to add this condition..

try


Code:
awk 'NR==1
s=0
NR>1 && !/^00|^80/
/^80/{s=1}
END{if(s){print}}' file

Sponsored Links
    #5  
Old 02-20-2013
nwalsh88 nwalsh88 is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 13 February 2014, 12:44 PM EST
Posts: 12
Thanks: 6
Thanked 0 Times in 0 Posts
It's getting there now

That edit to the script removes all the 80 records including the last one.

How could i get it to leave the last 80 record there??
Sponsored Links
    #6  
Old 02-20-2013
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 1 July 2014, 5:40 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 Posts
Try now


Code:
awk '!s && /00/{s=1;print}
/80/ && a{K=$0;for(i=1;i<=a;i++){print X[i]};a=0}
!/^00|80/{X[++a]=$0}
END{if(K){print K}
for(i=1;i<=a;i++){print X[i]}
}' file

Sponsored Links
    #7  
Old 02-20-2013
nwalsh88 nwalsh88 is offline
Registered User
 
Join Date: Feb 2013
Last Activity: 13 February 2014, 12:44 PM EST
Posts: 12
Thanks: 6
Thanked 0 Times in 0 Posts
Same again pamu

Everything is perfect apart from the final line of the file which should be the 80 record but is the 70 record
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
join based on line number when one file is missing lines jackiev Shell Programming and Scripting 1 06-27-2011 07:08 PM
how to remove lines ending with '*' jdhahbi UNIX for Dummies Questions & Answers 7 11-09-2010 09:43 AM
How to fetch rows based on line numbers or based on the beginning of a word? Muthuraj K Shell Programming and Scripting 18 02-02-2010 04:27 AM
Remove lines based on contents of another file bashshadow1979 Shell Programming and Scripting 4 03-25-2009 12:32 PM
Remove white space at the beginning of lines tipi Shell Programming and Scripting 4 09-02-2008 03:35 PM



All times are GMT -4. The time now is 01:22 AM.