How to extract string between IDs in shell [duplicate] - bash

This question already has answers here:
How to use sed/grep to extract text between two words?
(14 answers)
Closed 2 years ago.
I have a string that looks like this:
RG="#RG\tID:HS2000-1015_160.7\tDS:ADNI_1380^LP6005117-DNA_G04^ADNI_WGS\tLB:LP6005117-DNA_G04\tPL:illumina\tPU:HS2000-1015_160.7\tSM:ADNI_1380"
I want to extract everything after ID: and before first the \t
and get HS2000-1015_160.7 as a result. I would like a one-liner if possible.
code I tried:
echo ${RG} | grep -oP "(?<=ID:)[^"\t"]*"
which gives me HS2000-1015_160.7\

Try:
sed 's/.*ID://;s/\\t.*//'
s/.*ID:// remove everything in front and including ID:
s/\\t.*//' remove everything after and including \t characters.

Related

Replace a string with special characters using sed [duplicate]

This question already has answers here:
Is it possible to escape regex metacharacters reliably with sed
(4 answers)
Closed 7 months ago.
I have the below string
sec.val.hos.patn=.*app\.com$|localhost$|127\.0\.0\.1$
I want replace .*app\.com$|localhost$|127\.0\.0\.1$ with * so that final string looks like below
sec.val.hos.patn=*
I am trying to solve this problem using below sed command on Mac OS
sed -i ' ' 's~\.\*app\\\.com\$\|localhost\$\|127\\\.0\\\.0\\\.1\$~\*~g' file.txt
but unable to get the desired replacement. Can someone please help me to get this working.
I don't know if the pattern is actually more complex than you sample, but seems this should do the trick:
sed 's/\(^.*=\).*$/\1*/' <<< "sec.val.hos.patn=.*app\.com$|localhost$|127\.0\.0\.1$"
Here we capture everything from the start to =, then we replace the whole thing with capture group 1 plus *.

How can I capitalize a single letter after a certain character? [duplicate]

This question already has answers here:
Using Sed to capitalize the first letter of each word
(4 answers)
Closed 1 year ago.
I have some strings
some-string
some-other-string
yet-another-string-to-handle
I want to convert those strings into
someString
someOtherString
yetAnotherStringToHandle
I'm trying to do the following
echo yet-another-string-to-handle | sed -r 's/\-(.*)/\U\1\E/g'
But that results in
yetANOTHER-STRING-TO-HANDLE
Needless to say, I'm a bit lost. Any suggestions on how I can achieve my goal?
With GNU sed:
sed -E 's/-(.)/\u\1/g' file
\u: Turn the next character to uppercase (GNU 'sed' extension).
Output:
someString
someOtherString
yetAnotherStringToHandle
See: info sed

Replace comma with new line in MacOS terminal bash shell script [duplicate]

This question already has answers here:
How to add new line using sed on MacOS?
(3 answers)
Closed 2 years ago.
I'm trying to replace each comma with a new line.
e.g. when I do the following
echo abc,wer | sed 's/\,/\n/g'
I hope to get
abc
wer
However, I got
abcnwer
What did I do wrong?
Based on #MarkSetchell answer above, the below works.
echo abc,wer | tr , '\n'

How can I grep through multiple files and print any file names that have the string present? [duplicate]

This question already has answers here:
How can I use grep to show just filenames on Linux? [closed]
(3 answers)
Closed 3 years ago.
I want to know which files in a folder contain a certain string. There are many answers on here showing how to grep through many files. However, is there a way to print out the file names that have the string present?
I want to use something like:
grep -E '275322' *.txt
Can I use echo to print the file currently being looked at if the string is present?
Any help is appreciated.
If I understand the question correctly, you only want the filenames?
grep -l '275322' *
Check the manpage -
-l, --files-with-matches print only names of FILEs containing matches

How can I use a regex pattern in `tr` to replace only the final character of the match? [duplicate]

This question already has answers here:
Replacing first occurence in every line
(6 answers)
unix tr find and replace
(4 answers)
Closed 3 years ago.
I'm trying to use tr in bash to replace only the final character of a match. The string will have a substring with 5 digits followed by a dash, but I want to replace that dash with a slash.
I want to use something like this:
echo "xyzvb12345-Ab-C5678-dEf" | tr "#####-" "#####/"
To get an output like this:
xyzvb12345/Ab-C5678-dEf
Is there a way to do this with tr? Or maybe sed?
EDIT:
This is not a duplicate of the many tickets out there that merely find and replace text. Please read carefully before marking as a duplicate.
echo "xyzvb12345-Ab-C5678-dEf" | sed 's/\([0-9]\{5\}\)-/\1\//g'
[0-9] matches numbers
\{5\} matches five of the previous group (numers)
\(...\) set the matching group so as to be referred in replacement (as \1)
g at the end tells sed to replace all matches in the input
echo "xyzvb12345-Ab-C5678-dEf" | sed '0,/-/s//\//'
Thanks to this other answer
.

Resources