Unix Programming - add a character to a line...

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2006 > add a character to a line...





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author add a character to a line...
onlineviewer

2006-04-27, 1:28 pm

Hello,

How do you add a word or a character to the beginning of a line in a
file? I want to add a comment '#' where the string 'var' appears in a
file, globally. Like:

#### before:
bla
Var
bla

#### after:
bla
#Var
bla

Thanks,,,

Maxim Yegorushkin

2006-04-27, 1:28 pm


onlineviewer wrote:
> Hello,
>
> How do you add a word or a character to the beginning of a line in a
> file? I want to add a comment '#' where the string 'var' appears in a
> file, globally. Like:
>
> #### before:
> bla
> Var
> bla
>
> #### after:
> bla
> #Var
> bla
>
> Thanks,,,


in emacs

or using awk:

awk '/Var/{print #Var}' <file>

Chris F.A. Johnson

2006-04-27, 7:25 pm

On 2006-04-27, onlineviewer wrote:
> Hello,
>
> How do you add a word or a character to the beginning of a line in a
> file? I want to add a comment '#' where the string 'var' appears in a
> file, globally. Like:
>
> #### before:
> bla
> Var
> bla
>
> #### after:
> bla
> #Var
> bla


Do you want to add it before (a) 'var' or before (b) 'Var' or
before (c) both?

sed '/var/ s/^/#/' ## a
sed '/Var/ s/^/#/' ## b
sed '/[Vv]ar/ s/^/#/' ## c

Does var have to be a complete word? If so, you'll have to add
that.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, is written for the POSIX locale
===== and is released under the GNU General Public Licence
onlineviewer

2006-04-27, 7:25 pm

awesome,,,

thanks,

Jim Cochrane

2006-04-27, 7:25 pm

On 2006-04-27, onlineviewer <lancerset@gmail.com> wrote:
> Hello,
>
> How do you add a word or a character to the beginning of a line in a
> file? I want to add a comment '#' where the string 'var' appears in a
> file, globally. Like:
>
> #### before:
> bla
> Var
> bla
>
> #### after:
> bla
> #Var
> bla
>
> Thanks,,,
>


To change the file directly, you can use ed (or ex):

# On every line where var or Var occurs as a word (e.g., exclude
# "variety"), add '#' to the beginning of the line:
echo 'g/\<[vV]ar\>/s/^/#/
wq'|ed file

It's easy to put this in a for loop to process several files.

--

*** Posted via a free Usenet account from http://www.teranews.com ***
onlineviewer

2006-04-28, 1:16 pm

thank you

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com