Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

5 Answers 5

up vote 5 down vote accepted

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'
share|improve this answer
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
1  
Accepting this answer. TR is the shortest and simplest to read imho. –  Michael Durrant Sep 14 '13 at 1:18

sed is an option:

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

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.

share|improve this answer

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
share|improve this answer

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--
share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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