- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- replacing the first instance of a character in eac...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2003 03:29 AM
11-21-2003 03:29 AM
- 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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2003 03:36 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2003 03:47 AM
11-21-2003 03:47 AM
Re: replacing the first instance of a character in each line
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2003 03:47 AM
11-21-2003 03:47 AM
Re: replacing the first instance of a character in each line
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2003 03:57 AM
11-21-2003 03:57 AM
Re: replacing the first instance of a character in each line
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2003 04:10 AM
11-21-2003 04:10 AM
Re: replacing the first instance of a character in each line
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2003 05:04 AM
11-21-2003 05:04 AM
Re: replacing the first instance of a character in each line
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2003 05:47 AM
11-21-2003 05:47 AM
Re: replacing the first instance of a character in each line
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;i
if ( i == o ) printf("%s",s);
else printf("%s",f);
}
printf("\n");
}}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2003 05:57 AM
11-21-2003 05:57 AM
Re: replacing the first instance of a character in each line
here is an awk version:
awk '{sub(/,/,"|");print}' file
it changes only the first , in each line.
greetings,
Michael
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2003 08:54 AM
11-21-2003 08:54 AM
Re: replacing the first instance of a character in each line
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.