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
# 8  
Old 07-08-2010
Yes I am on solaris.
I tried your command,it does not give error but the o/p is different than what
i am expecting.It is removing all the occurances of "@".Seems it is not able to interpret "4" alongwith "g".

Code:
$/usr/xpg4/bin/sed 's/@/ /4g' test.dat
fdgfgddfg sfsdfdsf dfdfd  vfdfsdfd
sfsdfs sdfd gfhfgh  ddffg sdf
srfgtfsdfs sdfertrd eergfhfgh  ddffg sdf
wttrtrt sdfd sdfdf  sdfdfds



---------- Post updated at 02:29 PM ---------- Previous update was at 02:25 PM ----------

Yes I am on solaris.
I tried your command,it does not give error but the o/p is different than what
i am expecting.It is removing all the occurances of "@".Seems it is not able to interpret "4" alongwith "g".

Code:
$/usr/xpg4/bin/sed 's/@/ /4g' test.dat
fdgfgddfg sfsdfdsf dfdfd  vfdfsdfd
sfsdfs sdfd gfhfgh  ddffg sdf
srfgtfsdfs sdfertrd eergfhfgh  ddffg sdf
wttrtrt sdfd sdfdf  sdfdfds



---------- Post updated at 04:49 PM ---------- Previous update was at 02:29 PM ----------

The perl command works fine but when i try to create a perl file with contents
Code:
#!/usr/local/bin/perl
perl \-pe "$c=0; s/(\t)/ ++$c<=23 ? $1 : ""/eg" "src.dat" > "dest.dat"

it gives me an error.

Do i need to change anything in the perl file

Last edited by Franklin52; 07-08-2010 at 08:21 AM.. Reason: corrected code tags
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