Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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

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

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

You can use Vim in Ex mode:

ex -sc 'g/:/s/$/;/' -cx file
  1. g global search

  2. s substitute

  3. $ end of line

  4. x save and close

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .