Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a text file, whose content is like:

body
    font-size: 12px
    color: blue

td
    font-size: 14px

...

I want to append ; to the lines which contain :, so the content will be:

body
    font-size: 12px;
    color: blue;

td
    font-size: 14px;

...

What's the easiest way to do this in linux?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Use regular expression substitution. Many editors support regular expressions, including Vim.

Here's how to do it from the command line, using sed (Stream EDitor):

sed -i -e "s/.*:.*/&;/" INPUT_FILE.css

Some versions of sed don't support in-place editing (writing the output file to the input file):

sed -e "s/.*:.*/&;/" INPUT_FILE.css > OUTPUT_FILE.css

Explanation:

sed             invoke Stream EDitor commmand line tool
-i              edit in-place
-e              the next string will be the regular expression: s/.*:.*/&;/
INPUT_FILE.css  the name of your text file

The regular expression (RegEx) explained, in detail:

s   RegEx command indicates substitution
/   RegEx delimiter: separates command and match expression
.*  any string followed by...
:   a colon character followed by...
.*  any string
/   RegEx delimiter: separates match expression and replacement expression
&   RegEx back reference, entire string that was matched by match expression
;   the semicolon you wish to add
/   RegEx delimiter: ends replacement expression
share|improve this answer
1  
This is useful for converting many files at one time –  Freewind Jul 20 '11 at 5:03
add comment

In Vim, or any other editor with decent support for regular expressions

:%s/\(:.*\)$/\1;/
share|improve this answer
add comment

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.