LinuxQuestions.org
Visit Jeremy's Blog.
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, 01: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, 01:15 PM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,621

Rep: Reputation: 5608Reputation: 5608Reputation: 5608Reputation: 5608Reputation: 5608Reputation: 5608Reputation: 5608Reputation: 5608Reputation: 5608Reputation: 5608Reputation: 5608
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, 01: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, 02:06 PM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,621

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

Rep: Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069Reputation: 2069
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, 07:48 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,781

Rep: Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573Reputation: 3573
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, 08: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 10:14 AM
using grep and sed to find and replace a string with a pattern (HELP) bengerman Linux - Newbie 5 07-03-2013 12:53 PM
[SOLVED] search and replace string with special character perl or sed. Noobux Programming 4 05-21-2012 03: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 11:05 AM

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

All times are GMT -5. The time now is 03:41 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