Replace a string after n semicolon every line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a string after n semicolon every line
# 1  
Old 07-29-2010
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 12:23 PM..
# 2  
Old 07-29-2010
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 12:38 PM.. Reason: regex fix
This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 07-29-2010
Code:
a="a1;b1;c1;d11;e1"; echo $1 | sed 's/[a-z0-9]*;/;/4'

This User Gave Thanks to Ikon For This Post:
# 4  
Old 07-30-2010
MySQL

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace semicolon within double quotes in a file with semicolon delimiter

Hello Team, Could you please help me with the below question? I have a file with the following properties 1) File Delimiter is ; 2) Text columns are within double quotes 3) Numeric columns will not have double quotes 4) File has total 6 columns Please see a sample record from file ... (3 Replies)
Discussion started by: sam99
3 Replies

2. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

3. Shell Programming and Scripting

Read line by line and replace string.

Hi, I currently have a problem that I need to read a file line by line. After I read it line by line there are some commands in which I have to change a specific string.(In my case, I have to make a script that changes all the passwords into hash value) Here is a sample input... (3 Replies)
Discussion started by: thebennnn
3 Replies

4. Shell Programming and Scripting

How to have semicolon at the end of every line?

Hi, I wanted to create an automated script that will output a create table statement in unix. Below is the input and the desired output: INPUT: desc ZZ_APL_TIDDATELIST ( TID NUMBER AEX_DATE TIMESTAMP(6) ) desc ZZ_APL_TIDLIST ( TID NUMBER ) desc... (5 Replies)
Discussion started by: reignangel2003
5 Replies

5. Shell Programming and Scripting

Replace dot with semicolon in PERL

Hi, I have a file in PERL in the following pattern filename| 06-Dec-11 03.04.14.000000 PM filename1| 06-Dec-11 05.05.14.000000 PM I need to replace .(dot) with :(semicolon) in the timestamp value of the file How can this be done. Any help will be appreciated Thanks in advance (5 Replies)
Discussion started by: irudayaraj
5 Replies

6. Emergency UNIX and Linux Support

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (3 Replies)
Discussion started by: nithins007
3 Replies

7. Shell Programming and Scripting

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (6 Replies)
Discussion started by: nithins007
6 Replies

8. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

9. Shell Programming and Scripting

Replace blank spaces with semicolon - text file

Hi all, Been trying to find a solution to this, I'm sure its a sed 1 liner, but I don't know sed well enough to work it out... I have a text file in the following format: 431 666 1332 2665 0.24395 432 670 ... (3 Replies)
Discussion started by: mpcengineering
3 Replies

10. Shell Programming and Scripting

search for a string ,replace the whole line with new line

hai i am very new to unix. i am having two files like this. first.properties cache.ZA.TL_CCY=SELECT trim(CCY_CODE)||trim(COUNTRY_CODE)||trim(CITY_CODE) AS... (4 Replies)
Discussion started by: kkraja
4 Replies
Login or Register to Ask a Question