Remove 3rd and onwards occurances of a character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove 3rd and onwards occurances of a character
# 1  
Old 07-07-2010
Remove 3rd and onwards occurances of a character

I have a file with "@" in every line
Code:
fdgfgddfg@sfsdfdsf@dfdfd@@vfdfsdfd@
sfsdfs@sdfd@gfhfgh@@ddffg@sdf@
srfgtfsdfs@sdfertrd@eergfhfgh@@ddffg@sdf@
wttrtrt@sdfd@sdfdf@@sdfdfds@@@

I need to remove all the fourth and onwards occurences of "@".
so my final o/p would be
Code:
fdgfgddfg@sfsdfdsf@dfdfd@ vfdfsdfd
sfsdfs@sdfd@gfhfgh@ ddffg sdf
srfgtfsdfs@sdfertrd@eergfhfgh@ ddffg sdf
wttrtrt@sdfd@sdfdf@ sdfdfds

Can anyone please help

Last edited by Shivdatta; 07-07-2010 at 01:26 PM.. Reason: Please use code tags
# 2  
Old 07-07-2010
The posted desired output is different from what you ask.
# 3  
Old 07-07-2010
Perhaps you mean to replace them with a space?
Code:
sed 's/@/ /4g' infile

Code:
fdgfgddfg@sfsdfdsf@dfdfd@ vfdfsdfd
sfsdfs@sdfd@gfhfgh@ ddffg sdf
srfgtfsdfs@sdfertrd@eergfhfgh@ ddffg sdf
wttrtrt@sdfd@sdfdf@ sdfdfds

# 4  
Old 07-07-2010
Thanks for the reply but i'm getting an error
sed: command garbled: s/@/ /4g . Are you sure 4g works that way.
# 5  
Old 07-07-2010
Code:
perl -pe '$c=0; s/(@)/ ++$c<=3 ? $1 : " "/eg' file

# 6  
Old 07-07-2010
The perl script works splendid but i'm looking for a UNIX approach.
Wonder why sed 's/@/ /4g' gives an error Smilie
# 7  
Old 07-07-2010
Quote:
Originally Posted by Shivdatta
Thanks for the reply but i'm getting an error
sed: command garbled: s/@/ /4g . Are you sure 4g works that way.
Hi, I am sure it works on my system, as I posted the output. I think it should work with other proper seds too.
Are you on Solaris? See if this works:
Code:
/usr/xpg4/bin/sed 's/@/ /4g' infile


Last edited by Scrutinizer; 07-07-2010 at 03:01 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Shell Programming and Scripting

[Solved] Replace character in 3rd column and leave 1rst and last

Hello to all, I have the following text where columns are separated by spaces. I want to have the 3rd column separating 3 strings with 2 "_" in the format below: LeftSring_CentralString_RightString So, in 3rd column I want to replace all "_" with "-", except the first and last "_" The... (5 Replies)
Discussion started by: Ophiuchus
5 Replies

3. Shell Programming and Scripting

Replace first 3 occurances of a character in a file for each line

Hi all, I am very new to unix - ksh. I want to replace some characters in a file with some other, but only for first three occurances for each line. For eg: the first 3 occurances character ']' has to be replaced with ',' in each line. Please help. thanks Sreejith (1 Reply)
Discussion started by: rsreejithmenon
1 Replies

4. Shell Programming and Scripting

Remove 3rd character from the end of a random-length string

Hi, I hope someone can share there scripting fu on my problem, I would like to delete the 3rd character from a random length of string starting from the end Example Output Hope you can help me.. Thanks in advance.. (3 Replies)
Discussion started by: jao_madn
3 Replies

5. Shell Programming and Scripting

Replace multiple occurances of same character with a single character.

Hi all, Greetings, I have the following scenario, The contents of main file are like : Unix|||||forum|||||||||||||||is||||||the||best so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it And i need the output in the following form: Unix=forum=is=the=best so=be=on=it ... (3 Replies)
Discussion started by: dipanchandra
3 Replies

6. UNIX for Advanced & Expert Users

if 4th and 5th character of sting -ge 45 then add 1 to 3rd character

I want to know how to, given a string like W87151WR71C, if the 4th and 5th character (in this case 15) are greater than 45, then to add 1 to the 3rd character (in this case 7) and assign the revised string the variable name MODSTRING. Thanks in advance. This is ultimately to grab info from... (6 Replies)
Discussion started by: glev2005
6 Replies

7. Shell Programming and Scripting

Merging lines based on occurances of a particular character in a file

Hi, Is there any way to merge two lines based on specific occurance of a character in a file. I am having a flat file which contains multiple records. Each row in the file should contain specified number of delimiter. For a particular row , if the delimiter count is not matched with... (2 Replies)
Discussion started by: mohan_tuty
2 Replies

8. Shell Programming and Scripting

Count occurances of a character in a file

I want to find the number of occurences of a character in a file. How do i do it. Eg: $cat file1.txt Welcome to World of Unix. $ If i want to find the occurences of 'o' then I should be getting 3. Thanks. (6 Replies)
Discussion started by: Shivdatta
6 Replies

9. Shell Programming and Scripting

how to change every 3rd character

hey champs, i have a files, whose contents are as follows, abcdefghijk lmnopqrstuv .............. .............. i want to replace every other 3rd character to some specified character. let here in this file i want to replace each 3rd character to z. abzdezghzjk lmzopzrszuv... (2 Replies)
Discussion started by: manas_ranjan
2 Replies
Login or Register to Ask a Question