Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting


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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-09-2013
Registered User
 
Join Date: Sep 2010
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
Moderator
 
Join Date: Nov 2008
Location: Amsterdam
Posts: 7,352
Thanks: 144
Thanked 1,756 Times in 1,593 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
ɹoʇɐɹǝpoɯ
 
Join Date: Feb 2005
Location: Foxborough, MA
Posts: 7,384
Thanks: 112
Thanked 486 Times in 458 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
Reply

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 11:11 AM.