Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-26-2017, 02:11 PM
|
#1
|
LQ Newbie
Registered: Oct 2016
Posts: 5
Rep:
|
using sed to find and replace a character in a string
Hello, I have a fine containing many lines and I want to search for specific lines beginning with ">" and replace the third and final dot "." in the string with an underscore "_"
File looks like:
>LKTS.24.127.1
TACGGGAGTGGCAAGCGTTATCCGGAATTATTGGGCGTAAAGCGTCCGCAGGCGGCCCTTCAAGTCTGCTGTTAAAAAGT
>LKTS.24.127.3
TACGAGGGGTGCGAGCGTTGTCCGGAATCACTGGGCGTAAAGGGTCCGTAGGCGTTTCAACAAGTCATTTGTTAAAGACA
I want:
>LKTS.24.127_1
TACGGGAGTGGCAAGCGTTATCCGGAATTATTGGGCGTAAAGCGTCCGCAGGCGGCCCTTCAAGTCTGCTGTTAAAAAGT
>LKTS.24.127_3
TACGAGGGGTGCGAGCGTTGTCCGGAATCACTGGGCGTAAAGGGTCCGTAGGCGTTTCAACAAGTCATTTGTTAAAGACA
|
|
|
05-26-2017, 02:15 PM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,459
|
Oh, yes, that looks feasible. How did you try to solve it? sed is (probably) the right tool to do that. Where did you stuck?
|
|
|
05-26-2017, 02:57 PM
|
#3
|
LQ Newbie
Registered: Oct 2016
Posts: 5
Original Poster
Rep:
|
Maybe something like:
sed -i -e 's/</[^.]*$/_/g' file_1 > file_2
but I get the error bad flag in substitute command: '_'
|
|
|
05-26-2017, 03:06 PM
|
#4
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,459
|
yes, because you have an unprotected / in your sed (or at least you have 4 instead of 3)
|
|
|
05-26-2017, 03:18 PM
|
#5
|
LQ Veteran
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky 9.5
Posts: 5,844
|
Won't the dot need to be escaped as well to search for an actual dot? Otherwise it will mean "any single character" to sed.
|
|
|
05-26-2017, 08:48 PM
|
#6
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,314
|
Correct.
All-in-all, wayyyy too complicated for the task. Design your regex as you specified the requirement. Address only lines starting with ">", and replace the third "."
Code:
sed '/^>/ s/\./_/3' file
|
|
1 members found this post helpful.
|
06-04-2017, 09:53 AM
|
#7
|
LQ Newbie
Registered: Oct 2016
Posts: 5
Original Poster
Rep:
|
Many thanks! That worked! Keri
|
|
|
All times are GMT -5. The time now is 09:03 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|