Delete until Nth occurence (sed, awk) | 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.

Delete until Nth occurence (sed, awk)

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-09-2013
drbiloukos drbiloukos is offline
Registered User
 
Join Date: Sep 2010
Last Activity: 10 January 2013, 3:28 AM EST
Posts: 99
Thanks: 69
Thanked 0 Times in 0 Posts
Delete until Nth occurence (sed, awk)

Hello people,
Once more I need your help with SED/AWK
I need to delete up to the Nth occurence of a char (from the beggining) and until the Mth occurence of a char (from the end)

Example:
Input:

Code:
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z

Output:

Code:
i,j

Must delete up to the 8th comma from the beggining and upo to the 16th comma from the end.

For each line

Thank you in advance for your help.

Last edited by Scrutinizer; 01-09-2013 at 07:35 AM.. Reason: code tags
Sponsored Links
    #2  
Old 01-09-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 30 October 2014, 11:47 AM EDT
Location: Amsterdam
Posts: 9,558
Thanks: 286
Thanked 2,429 Times in 2,176 Posts
Try:

Code:
sed 's/\([^,]*,\)\{8\}//; s/\(,[^,]*\)\{16\}$//' file


Last edited by Scrutinizer; 01-09-2013 at 08:14 AM..
Sponsored Links
    #3  
Old 01-09-2013
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 30 October 2014, 12:23 PM EDT
Location: Foxborough, MA
Posts: 7,667
Thanks: 152
Thanked 586 Times in 551 Posts

Code:
awk '{for(i=s+1;i<=NF-e;i++) printf("%s%c", $i,(i==NF-e)?ORS:OFS)}' s=8 e=16 FS=, OFS=, myFile

The Following User Says Thank You to vgersh99 For This Useful Post:
drbiloukos (01-09-2013)
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
Using tr, sed or awk to delete text from nth column only hlwright Shell Programming and Scripting 6 09-02-2011 03:51 PM
To find the Nth Occurence of Search String mac4rfree UNIX for Dummies Questions & Answers 3 07-27-2011 02:16 AM
Replacing nth occurence raghav288 Shell Programming and Scripting 11 10-29-2009 11:39 AM
Replace matching nth occurence raghav288 Shell Programming and Scripting 1 10-29-2009 10:19 AM
delete line upto the nth occurence of a particular charachter. kashifv Shell Programming and Scripting 12 10-09-2009 06:26 AM



All times are GMT -4. The time now is 12:40 PM.