6

I have some output from iconv, e.g.

$ iconv -l | grep ISO | head -5
CSISO4UNITEDKINGDOM//
CSISO10SWEDISH//
CSISO11SWEDISHFORNAMES//
CSISO14JISC6220RO//
CSISO15ITALIAN//
durrantm:~

How can I change the //'s to, say --'s ?

I tried $ (iconv -l | grep ISO).gsub('\/\/','--') but no luck.

8

There are a few approaches using either tr, awk or sed

TR:

 iconv -l | grep ISO |head -5 |tr '/' '-'

AWK:

iconv -l | awk '/ISO/{gsub("//","--"); print $0}' |head -5

SED:

iconv -l | grep ISO |head -5 | sed 's/\//-/g'
# or, to avoid needing to escape the backslashes:
iconv -l | grep ISO |head -5 | sed 's#/#-#g'
  • 4
    For tr you can say tr '/' '-'. Global substitution is implied. – Joseph R. Sep 13 '13 at 17:31
  • Updated this answer for TR to reflect that and also added my own sed option that avoids needing to escape the backslash. – Michael Durrant Sep 14 '13 at 1:15
2

grep is more or less a subset of sed (grep stands for the g/re/p command in ed, and sed is like ed applied to [s]treams), so you generally don't need to pipe them together.

iconv -l | sed '/ISO/!d;s,//,--,g' | head -n 5
2

sed is an option:

$ iconv -l | grep ISO | sed 's#/#-#g' | head -5
CSISO4UNITEDKINGDOM--
CSISO10SWEDISH--
CSISO11SWEDISHFORNAMES--
CSISO14JISC6220RO--
CSISO15ITALIAN--
1

When using sed you don't have to use forward slashes to denote the arguments to search/replace. You can change them to anything you want. For example:

$ iconv -l|grep ISO | sed 's@//@--@g' | head -5
CSISO4UNITEDKINGDOM--
CSISO10SWEDISH--
CSISO11SWEDISHFORNAMES--
CSISO14JISC6220RO--
CSISO15ITALIAN--

This can be condensed since you're looking for the same character to this:

$ iconv -l|grep ISO|sed 's@/@-@g' | head -5
CSISO4UNITEDKINGDOM--
CSISO10SWEDISH--
CSISO11SWEDISHFORNAMES--
CSISO14JISC6220RO--
CSISO15ITALIAN--
  • No need for // and -- since you're using global substitution. – Joseph R. Sep 13 '13 at 18:01
  • @JosephR. - yeah I was getting to it. 8-) – slm Sep 13 '13 at 18:02
0

There are several ways to do it, but I'd use sed with the -n aka --quiet or --silent option to suppress output by default.

iconv -l | sed -n -e '/ISO/ s://:--:p'

That sed script ignores lines not containing "ISO", changes // to --, and prints the (modified) matching line. The final 'p' in the script is what tells sed to print that matching line.

To avoid lots of ugly backslash-escaping, I've used : as the regexp separator character rather than /

See the sed man page for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.