Solved

using grep to insert a character

Posted on 2007-12-06
14
259 Views
Last Modified: 2010-04-21
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
Comment
Question by:danomatic
  • 4
  • 4
  • 2
  • +3
14 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 20422349
Hi danomatic,

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

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


Good Luck,
Kent
0
 
LVL 3

Author Comment

by:danomatic
ID: 20422449
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
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 20422583
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
 
LVL 45

Expert Comment

by:Kdo
ID: 20422588
Hi danomatic,

What flavor of unix are you using?


Kent
0
 
LVL 48

Expert Comment

by:Tintin
ID: 20422861
This will work.
 sed 's/\(.*\)/*\1*/' file >newfile

Open in new window

0
 
LVL 3

Author Comment

by:danomatic
ID: 20423343
I am using terminal in OS X
0
 
LVL 3

Author Comment

by:danomatic
ID: 20423384
ok, now for some reason it does this:

*word1
*
*word2
*
word3
*

so an extra line break is being put in
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:omarfarid
ID: 20423620
Hi,

Try

ed myfile <<END
1,$ s/^/*
1,$ s/$/*
w
q
END
0
 
LVL 3

Author Comment

by:danomatic
ID: 20423728
well that still puts the trailing * on a new line of its own...
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 20423771
Try

Modified version :)

ed myfile <<END
1,$ s/^/\*
1,$ s/$/\*
w
q
END
0
 
LVL 84

Expert Comment

by:ozo
ID: 20423900
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
 
LVL 48

Expert Comment

by:Tintin
ID: 20424134
In addition to ozo's request, could you also post a few lines of the output from

od -c file
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 20425360
try this
awk '{ gsub(/\r\n|\n/,"");print "*"$0"*"}' file

Open in new window

0
 
LVL 45

Expert Comment

by:Kdo
ID: 20427050

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now