- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Editing n-1th occurrence of a character in a f...
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
03-29-2004 09:07 AM
03-29-2004 09:07 AM
In a nutshell, every record in my pipe-delimited file can have one or more slashes, the first one(s)one occurring in the very first field, and the last slash occuring in the last field. I need to preserve the last slash, but convert the next-to-last slash to a pipe.
Ex:
(spaces added for emphasis)
aa/bb/cc /dd| ee|ff|gg|/hh|
I need to change this to:
aa/bb/cc |dd| ee|ff|gg|/hh|
The solution I got from this wonderful community works for the first example but not the second:
while
read input_record
do
start=${input_record%/*}
last=${input_record##*/}
print "$start|$last" >> ${output_file}
done < ${input_file}
Could this code be easily changed to accomodate my needs?
Regards,
Cathy
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2004 09:34 AM
03-29-2004 09:34 AM
Solutionwhile
read input_record
do
Last=${input_record#*|} #everything after the first |
Start=${input_record%%|*} #everything before the first |
start=${Start%/*} # everything before the last / in $Start
last=${Start##*/} # everything after the last / in $Start
print "$start|$last|$Last" >> ${output_file}
done < ${input_file}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2004 09:49 AM
03-29-2004 09:49 AM
Re: Editing n-1th occurrence of a character in a file
sed 's!\([^|]*\)/\(.*|.*\)/\1|\2!'
replaces the last / before the first | with |
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2004 10:05 AM
03-29-2004 10:05 AM
Re: Editing n-1th occurrence of a character in a file
Sometimes the solution is SO simple it's easy to miss! I like your first solution simply because it's more readable for the developer who may need to modify this in the future.
Superb work! Many thanks (and points!!).
Cathy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-29-2004 10:38 AM
03-29-2004 10:38 AM
Re: Editing n-1th occurrence of a character in a file
I wasn't getting anything out of $Last or $Start. I neutralized the pipe and got results:
Last=${input_record#*\|}
Start=${tactic_state_record%%\|*}
Thanks again!
Cathy