get some field of text with sed command - sed

I have these line:
"CN=Michael George\, Jou,OU=External,OU=User,DC=Company,DC=en"
I would like to extract these:
Michael George\, Jou
I'm trying with sed:
grep -i "CN" file | sed "s/CN=\(.*\),\(.*\)/\1/g"
But I can't have these result. Could you help me with sed command? Thanks

str="CN=Michael George\, Jou,OU=External,OU=User,DC=Company,DC=en"
echo "${str}" | sed 's/.*CN=\([^=]*\),.*/\1/'
Output:
Michael George\, Jou

Related

SED command to the following

Using SED I would like to transform several hundred lines in a text file from:
Input example:
https://mysite.demo.com/topics/en-gb/3
https://mysite.demo.com/topics/en-gb/436
https://mysite.demo.com/topics/en-gb/9167
into
Output:
https://mysite.demo.com/topics/en-gb/3/pdf/3.pdf
https://mysite.demo.com/topics/en-gb/436/pdf/436.pdf
https://mysite.demo.com/topics/en-gb/9167/pdf/9167.pdf
I was wondering what SED command I would use to do this?
Many thanks
Run: echo "https://mysite.demo.com/topics/en-gb/3" |\
sed "s|\(https:\/\/mysite.demo.com\/topics\/en-gb\)\/\([0-9]\+\)|\1/\2/pdf/\2.pdf|g"
Output:
https://mysite.demo.com/topics/en-gb/3/pdf/3.pdf
Here I use sed "s|||" instead of sed "s///".
As per your sample input and expected output, this sed command would work:
sed -E 's,(.*\/)([0-9]+$),\1\2\/pdf\/\2\.pdf,g' text_file
Output:
https://mysite.demo.com/topics/en-gb/3/pdf/3.pdf
https://mysite.demo.com/topics/en-gb/436/pdf/436.pdf
https://mysite.demo.com/topics/en-gb/9167/pdf/9167.pdf

manipulation of text by sed command

I a file containing the genome ids following NZ_FLAT01000030.1_173 I need to manipulate those ids like this one: NZ_FLAT01000030.1
I tried some but didn't give me the exact thing.
sed 's/_/\t/' output : NZ FLAT01000030.1_173
sed -r 's/_//' output: NZFLAT01000030.1_173
sed -r 's/_//g' output: NZFLAT01000030.1173
How can I do that by using sed command?
Are you trying to remove the undesrscore and the digits following it?
echo 'NZ_FLAT01000030.1_173' | sed -E 's/_[0-9]+//g'
NZ_FLAT01000030.1
$ echo 'NZ_FLAT01000030.1_173' | sed 's/_[^_]*$//'
NZ_FLAT01000030.1

How to replace a string using Sed?

Suppose I have a string like this
<start><a></a><a></a><a></a></start>
I want to replace values inside <start></start> like this
<start><ab></ab><ab></ab><ab></ab><more></more><vale></value></start>
How do I do this using Sed?
Try this :
sed 's#<start>.*</start>#<start><ab></ab><ab></ab><ab></ab></start>#' file
I get this line with gnu sed :
sed -r 's#(<start>)(.*)(</start>)#echo "\1"$(echo "\2"\|sed "s:a>:ab>:g")"\3"#ge'
see example:
kent$ echo "<start><a></a><a></a><a></a><foo></foo><bar></bar></start>"|sed -r 's#(<start>)(.*)(</start>)#echo "\1"$(echo "\2"\|sed "s:a>:ab>:g")"\3"#ge'
<start><ab></ab><ab></ab><ab></ab><foo></foo><bar></bar></start>
note
this will replace the tags between <start>s which ending with a . which worked for your example. but if you have <aaa></aaa>:
you could do: (I break it into lines for better reading)
sed -r 's#(<start>)(.*)(</start>)
#echo "\1"$(echo "\2"\|sed "s:<a>:<ab>:g;s:</a>:</ab>:g")"\3"
#ge'
e.g.
kent$ echo "<start><a></a><a></a><a></a><aaa></aaa><aba></aba></start>" \
|sed -r 's#(<start>)(.*)(</start>)#echo "\1"$(echo "\2"\|sed "s:<a>:<ab>:g;s:</a>:</ab>:g")"\3"#ge'
<start><ab></ab><ab></ab><ab></ab><aaa></aaa><aba></aba></start>
sed 's/(\<\/?)a\>/\1ab\>/g' yourfile, though that would get <a></a> that was outside <start> as well...
grep -rl 'abc' a.txt | xargs sed -i 's/abc/def/g'

modify a line using sed

I need to used sed for following requirment using sed
I have one string as $str and I need to replace blow line in a file
abh{1..$abh} cdf_$ghu,xyz * abh{}.$xy
New modified line should be as below
abh{1..$abh} cdf_$ghu,$str * abh{}.$xy
Note "xyz" can be any arbitrary value. Could you please tell me how to do using sed in one liner.
sed 's/\(^\s*abh{1..$abh}\s*\)\(.*xyz\)/\1/' file.txt
but still does not work. Any help would be appreciated.
Try this:
$ sed 's|\(\S\+\s\+[^,]\+,\)\S\+\(\s\+.*\)|\1$str\2|' file.txt
abh{1..$abh} cdf_$ghu,$str * abh{}.$xy
Or even more simple:
$ sed 's|,\S\+|,$str|' example.txt
echo 'abh{1..$abh} cdf_$ghu,xyz * abh{}.$xy' | sed 's/\(.*\$ghu,\)\(.*\)\( .*\)/\1\$str\3/g'

sed and special char

im trying the following sed command, but i have no luck with special chars:
echo "x#asdf" | sed "s/\([^-]\)#/\1\n/g"
x
asdf
but if i use some special char in test.txt
echo "ä#asdf" | sed "s/\([^-]\)#/\1\n/g"
ä#asdf
why ?
this works:
echo "ü#asdf" | sed "s/ü/-/g"
-#asdf
but this doesnt:
echo "ü#asdf" | sed "s/[ü]/-/g"
ü#asdf
I'm not sure about this, because your sed commands work ok for me (gnu sed 4.1.5), but try invoking sed this way:
$ LANG=de_DE.UTF-8 sed ...
See this post for more information: Why does sed fail with International characters and how to fix?.
If this doesn't work, it may help to upgrade to gnu sed 4.2, if you can. The NEWS file says "multibyte processing fixed" for 4.2 but does not go into further detail.