How to use sed to replace u'sometext' with 'sometext'
Clash Royale CLAN TAG#URR8PPP
How to use sed to replace u'sometext' with 'sometext'
I have a file with text in it I simply want to strip off the leading u from all instances of u'sometext'
so that it leaves 'sometext'
. I haven't been able to figure out how to get sed to match on u'
and replace with '
.
u'sometext'
'sometext'
u'
'
Sed command I though would work:
echo ['a', u'update for microsoft office 2013 (kb4022166) 32-bit edition', 'unknown', 'null'] | sed "s/u'/'/g"
output:
[a, uupdate for microsoft office 2013 (kb4022166) 32-bit edition, unknown, null]
what I wanted:
['a', 'update for microsoft office 2013 (kb4022166) 32-bit edition', 'unknown', 'null']
More examples of what is in the file:
"[u'cpe:/o:microsoft:windows_7::sp1:x64-enterprise', u'cpe:/a:adobe:acrobat:11.0.19']"
What I would like to have:
"['cpe:/o:microsoft:windows_7::sp1:x64-enterprise', 'cpe:/a:adobe:acrobat:11.0.19']"
Thanks @sundeep
– JediKid
Aug 10 at 15:30
3 Answers
3
You will need to use word boundaries, denoted with the special character b
which goes immediately before the first thing to be matched on a boundary
b
$ echo "[u'a', u'hello']" | sed "s/bu'/'/g"
['a', 'hello']
So one issue, in this case: "[ u'hello', u'hulu' ]" -> "[ 'hello', 'hul' ]"
– JediKid
Aug 10 at 15:44
@JediKid, I realized I mixed up where the
b
goes. Put it in front and the hulu problem will go away– Edward Minnix
Aug 10 at 15:51
b
Awesome! Thank you!
– JediKid
Aug 10 at 15:53
That's not very robust, e.g.
echo "[u'version 7-u']" | sed "s/bu'/'/g"
-> ['version 7-']
– Ed Morton
Aug 12 at 14:53
echo "[u'version 7-u']" | sed "s/bu'/'/g"
['version 7-']
Try, if possible, with something like this:
OUTPUT:
It seems that it is not taking well the complete string but assuming it as several ones.
It was my test string that was the issue, in the file the string is wrapped in double quotes, but my echo string wasn't, this explains why it wasn't working as I expected. Thank you.
– JediKid
Aug 10 at 15:41
That wasn't the only issue. Your sed command was wrong too.
– Ed Morton
Aug 12 at 15:01
$ echo "[u'a', u'hello', u'version 7-u']" | sed "s/u('[^']*')/1/g"
['a', 'hello', 'version 7-u']
$ echo "['a', u'update for microsoft office 2013 (kb4022166) 32-bit edition', 'unknown', 'null']" | sed "s/u('[^']*')/1/g"
['a', 'update for microsoft office 2013 (kb4022166) 32-bit edition', 'unknown', 'null']
$ echo "[u'cpe:/o:microsoft:windows_7::sp1:x64-enterprise', u'cpe:/a:adobe:acrobat:11.0.19']" | sed "s/u('[^']*')/1/g"
['cpe:/o:microsoft:windows_7::sp1:x64-enterprise', 'cpe:/a:adobe:acrobat:11.0.19']
Note though that both the above and the currently accepted answer would fail if you can have a u
at the end of a single-quote-delimited string earlier in the line. e.g.:
u
$ echo "['u', 'a']" | sed "s/u('[^']*')/1/g"
['', 'a']
$ echo "['u', 'a']" | sed "s/bu'/'/g"
['', 'a']
so, assuming that is an issue, we can use a more robust approach with awk (in this case using GNU awk for multi-char RS and RT):
$ echo "['u', 'a']" | awk -v RS="'[^']*'" -v ORS= 'RTsub(/u$/,"") print $0 RT'
['u', 'a']
$ echo "[u'a', u'hello', u'version 7-u']" | awk -v RS="'[^']*'" -v ORS= 'RTsub(/u$/,"") print $0 RT'
['a', 'hello', 'version 7-u']
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
please click edit and add the code you tried.. also, adding few sample lines (say 2-5 with made up data) along with complete expected output would help add clarity as well as help in testing solutions... the downvotes are probably because of these missing information.. see stackoverflow.com/help/mcve for details
– Sundeep
Aug 10 at 15:08