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



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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-29-2010
Registered User
 

Join Date: Jun 2010
Posts: 11
Thanks: 6
Thanked 0 Times in 0 Posts
Replace a string after n semicolon every line

I have a file that is formatted in this way.


Code:
a1;b2;c33;d4;e5;e;f;f;f;s
d;ds;d;a;v;b;g;gr;r;rt;fdf
s1;s2;s2;s3;s4;
 
b1;f2;g3;h4;a3c4e;xcsd;fds;
sd2;fs4;fs2;sdf3;

I want to replace the value just before the 4th semicolon to empty string, regardless the value, such that it looks like this for every block of data separated by a line


Code:
a1;b2;c33;;e5;e;f;f;f;s
d;ds;d;a;v;b;g;gr;r;rt;fdf
s1;s2;s2;s3;s4;
 
b1;f2;g3;;a3c4e;xcsd;fds;
sd2;fs4;fs2;sdf3;

I tried using
Code:
sed 's/[^*];//4'

but to no avail. Any ideas?

Last edited by alienated; 07-29-2010 at 11:23 AM..
Sponsored Links
    #2  
Old 07-29-2010
vgersh99's Avatar
ɹoʇɐɹǝpoɯ
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 6,959
Thanks: 69
Thanked 351 Times in 338 Posts

Code:
echo 'a1;b2;c33;d4;e5' | nawk -F';' '{$4=""}1' OFS=';'
OR
echo 'a1;b2;c33;d4;e5' | sed 's/[^;][^;]*;/;/4'


Last edited by vgersh99; 07-29-2010 at 11:38 AM.. Reason: regex fix
The Following User Says Thank You to vgersh99 For This Useful Post:
alienated (07-29-2010)
Sponsored Links
    #3  
Old 07-29-2010
Ikon's Avatar
Ikon Ikon is offline Forum Advisor  
Computer Geek
 

Join Date: Jul 2008
Location: Frederick, MD
Posts: 748
Thanks: 4
Thanked 10 Times in 9 Posts

Code:
a="a1;b1;c1;d11;e1"; echo $1 | sed 's/[a-z0-9]*;/;/4'

The Following User Says Thank You to Ikon For This Useful Post:
alienated (07-29-2010)
    #4  
Old 07-30-2010
ygemici ygemici is online now Forum Advisor  
sed_shell@LNU
 

Join Date: Feb 2010
Location: istanbul
Posts: 1,478
Thanks: 1
Thanked 247 Times in 240 Posts
Thumbs up


Code:
# echo 'a1;b2;c33;d4;e5' | sed 's/[^;]*;/;/4'
a1;b2;c33;;e5


Code:
 
# echo 'a1;b2;c33;d4;e5' | sed 's/\(.*\)[a-z][0-9]*;\([a-z0-9]*\)*$/\1;\2/'
a1;b2;c33;;e5

Sponsored Links
Closed Thread

Tags
nawk

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
Replace blank spaces with semicolon - text file mpcengineering Shell Programming and Scripting 3 11-30-2009 07:36 AM
How to replace a particular string in a line Raju Datla Red Hat 1 01-09-2009 04:02 AM
search for a string ,replace the whole line with new line kkraja Shell Programming and Scripting 4 08-07-2008 02:26 AM
sed conditional string replace for each line Nanu_Manju Shell Programming and Scripting 6 04-14-2008 02:16 PM
put a semicolon at the end of each line of a file surjyap Shell Programming and Scripting 1 02-13-2006 10:05 AM



All times are GMT -4. The time now is 03:55 AM.