Operating System - HP-UX
1833871 Members
1656 Online
110063 Solutions
New Discussion

replacing the first instance of a character in each line

 
SOLVED
Go to solution
MAD_2
Super Advisor

replacing the first instance of a character in each line

I would like to know if someone can give me ideas on how to replace the first, and or second, or third, or fourth instance (whatever may be the case) of a charachter in a file by any methods, such as:

- awk & sed
- Perl
- vi substitution

Or some other way. Example:

"Doe, John"|"1234 Imagine St., Apt. 1530, Weston FL 33327"|"954-123-4567"|"954-987-6543"

I would like to separate the "Doe, John" in the first field and make it two fields by subsituting the comma with "|" to obtain results like "Doe"|"John"| (but I have about 500-1000 records to do this with so forget the manual change. Of course, each of these characters to be changed are located each in different positions and not a fixed column through the file.

Then I would have to separate full addresses into address1, address2, city, state, zip, etc. maybe using a similar method.

Thanks.
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
9 REPLIES 9
H.Merijn Brand (procura
Honored Contributor
Solution

Re: replacing the first instance of a character in each line

# perl -pe's/\s*,\s*/"|"/' file >newfile

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
MAD_2
Super Advisor

Re: replacing the first instance of a character in each line

Great procura... What in the code is telling it to substitute only the first instance in this case? (Is it the \s*, as in look at the beginning of the string to the first comma?) How about if I want it to replace the second, or third, or whatever instance of the character in each line?

Thanks!
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
Stefan Farrelly
Honored Contributor

Re: replacing the first instance of a character in each line

cat file | sed 's/,/"|"/1' >newfile

By default sed only replaces the first occurance of the comma in each line, the 1 at the end means the same thing, only replace the first comma.


Im from Palmerston North, New Zealand, but somehow ended up in London...
MAD_2
Super Advisor

Re: replacing the first instance of a character in each line

So basically I can tell it to replace whatever occurrence in each line, by replacing the 1 with a 2, 3, so on or so forth?
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
Stefan Farrelly
Honored Contributor

Re: replacing the first instance of a character in each line

no, if you change the 1 in sed to 2 or 3 it will change the first 2 or 3 occurances (inclusive), not just the 2nd or 3rd. Change it to g to change all (g=global)
Im from Palmerston North, New Zealand, but somehow ended up in London...
MAD_2
Super Advisor

Re: replacing the first instance of a character in each line

Ok, I am getting a little confused about your answer. If I change the 1, to let's say 2, will it only change the 2nd occurrence or will it change the 1st and 2nd? How can I make it:

1. Change the 1s, 2nd, and 3rd. Or just
2. Change only the 2nd or the 3rd

For example.

I don't want to do any global changes. Thanks!
Contrary to popular belief, Unix is user friendly. It's just very particular about who it makes friends with
curt larson_1
Honored Contributor

Re: replacing the first instance of a character in each line

#!/usr/bin/ksh

file=yourfile
f="," # character to find
s="|" # character to substitute
o=1 # occurance of character to make substitution on

cat $file |
awk -v f="$f" -v s="$s" -v o="$o" '{
num=split($0,a,f);
if ( o >= num ) {print $0;}
else {
for ( i=1;iprintf("%s",a[i]);
if ( i == o ) printf("%s",s);
else printf("%s",f);
}
printf("\n");
}}'
Michael Schulte zur Sur
Honored Contributor

Re: replacing the first instance of a character in each line

Hi,

here is an awk version:
awk '{sub(/,/,"|");print}' file

it changes only the first , in each line.

greetings,

Michael
Ralph Haefner
Frequent Advisor

Re: replacing the first instance of a character in each line

Ok, here's my goofy idea. Say you want to change the 3rd comma to a pipe. Couldn't you take Stefan's sed command and run it for the value of 3, which changes all of the first 3 commas to pipes. But then run it again with a value of 2 and reverse the pipe and comma, which changes the first 2 back?

That way you get only the 3rd one to change as you want, and you do it in 2 lines instead of needing a much longer script.

Primitive I know, but seems like it should work. I have no idea how doing that would compare to the scripts other people have suggested performance-wise, but with only 500-1000 records it probably doesn't matter.