• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

using grep to insert a character

I have a list of 500 words like this: (1 word per line)

word 1
word 2
word 3

etc..

I would like to ad an * before and after every word so it will look  like this:

*word 1*
*word 2*
*word 3*

is there a way to do this with grep?
0
danomatic
Asked:
danomatic
  • 4
  • 4
  • 2
  • +3
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi danomatic,

Not with grep, but sed or awk will do it quite easily

  sed 's/^/*/; s/$/*/' myfile > newfile


Good Luck,
Kent
0
 
danomaticAuthor Commented:
kdo, that works to put an * in front of the word but does not put it on the end.  Can that code be adjusted to put one at the end too?
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi danomatic,

Hmmm.....  

  s/$/*/

That should put an asterisk before the end-of-line.

The file is one word per line, isn't it?


Kent
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Kent OlsenData Warehouse Architect / DBACommented:
Hi danomatic,

What flavor of unix are you using?


Kent
0
 
TintinCommented:
This will work.
 sed 's/\(.*\)/*\1*/' file >newfile

Open in new window

0
 
danomaticAuthor Commented:
I am using terminal in OS X
0
 
danomaticAuthor Commented:
ok, now for some reason it does this:

*word1
*
*word2
*
word3
*

so an extra line break is being put in
0
 
omarfaridCommented:
Hi,

Try

ed myfile <<END
1,$ s/^/*
1,$ s/$/*
w
q
END
0
 
danomaticAuthor Commented:
well that still puts the trailing * on a new line of its own...
0
 
omarfaridCommented:
Try

Modified version :)

ed myfile <<END
1,$ s/^/\*
1,$ s/$/\*
w
q
END
0
 
ozoCommented:
I'm testing
sed 's/^/*/; s/$/*/'
using terminal in OS X
and getting
*word 1*
*word 2*
*word 3*
Could there be something else in the file?
can you show
cat -vet file
and
cat -vet newfile
0
 
TintinCommented:
In addition to ozo's request, could you also post a few lines of the output from

od -c file
0
 
ghostdog74Commented:
try this
awk '{ gsub(/\r\n|\n/,"");print "*"$0"*"}' file

Open in new window

0
 
Kent OlsenData Warehouse Architect / DBACommented:

I suspect that the file is DOS formatted, with a silly carriage return beside the newline character.

I don't know OS X well enough to know if you have dos2unix (sometimes d2u).  If so, convert the file first.

dos2unix {oldfile} > newfile
d2u {oldfile} > newfile

If not, one of these sed scripts should work for you:

 sed 's/.$//' {oldfile} > newfile
 sed 's/^M$//' {oldfile} > newfile
 sed 's/\x0D$//' {oldfile} > newfile

Note that the first script will remove the last character of the line so if the file is not DOS formatted, all of the text will be 1 character short.  (Since the file is being written elsewhere, this is just a debugging phenomenon and nothing is really damaged.)

Then follow up with the originally posted sed script

  sed 's/^/*/; s/$/*/' {newfile} > newerfile


Good Luck,
Kent
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 4
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now