LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 05-26-2017, 02:11 PM   #1
kmkemp
LQ Newbie
 
Registered: Oct 2016
Posts: 5

Rep: Reputation: Disabled
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
 
Old 05-26-2017, 02:15 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,459

Rep: Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765
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?
 
Old 05-26-2017, 02:57 PM   #3
kmkemp
LQ Newbie
 
Registered: Oct 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Maybe something like:

sed -i -e 's/</[^.]*$/_/g' file_1 > file_2

but I get the error bad flag in substitute command: '_'
 
Old 05-26-2017, 03:06 PM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,459

Rep: Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765Reputation: 7765
yes, because you have an unprotected / in your sed (or at least you have 4 instead of 3)
 
Old 05-26-2017, 03:18 PM   #5
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: Rocky 9.5
Posts: 5,844

Rep: Reputation: 2263Reputation: 2263Reputation: 2263Reputation: 2263Reputation: 2263Reputation: 2263Reputation: 2263Reputation: 2263Reputation: 2263Reputation: 2263Reputation: 2263
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.
 
Old 05-26-2017, 08:48 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,314

Rep: Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172
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.
Old 06-04-2017, 09:53 AM   #7
kmkemp
LQ Newbie
 
Registered: Oct 2016
Posts: 5

Original Poster
Rep: Reputation: Disabled
Many thanks! That worked! Keri
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed search for a string, duplicate original and replace the string Jykke Programming 17 06-29-2016 11:14 AM
using grep and sed to find and replace a string with a pattern (HELP) bengerman Linux - Newbie 5 07-03-2013 01:53 PM
[SOLVED] search and replace string with special character perl or sed. Noobux Programming 4 05-21-2012 04:16 PM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 12:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration