Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Delete until Nth occurence (sed, awk)

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-09-2013
drbiloukos drbiloukos is offline
Registered User
 
Join Date: Sep 2010
Last Activity: 1 April 2015, 5:11 AM EDT
Posts: 101
Thanks: 70
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 Unix and Linux 01-09-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 19 April 2015, 4:13 PM EDT
Location: Amsterdam
Posts: 10,105
Thanks: 326
Thanked 2,652 Times in 2,373 Posts
Try:

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


Last edited by Scrutinizer; 01-09-2013 at 08:14 AM..
Sponsored Links
    #3  
Old Unix and Linux 01-09-2013
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 19 April 2015, 4:57 PM EDT
Location: Foxborough, MA
Posts: 7,804
Thanks: 172
Thanked 647 Times in 605 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image 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 08:48 PM.