Removing asterisks from a file

James Gray james at gray.net.au
Thu Oct 16 05:36:49 UTC 2008


On 16/10/2008, at 4:13 PM, Ranmadhu Wijayatilaka wrote:

> James Gray wrote:
>> sed "s#*##g" file.txt > new_file.txt
>>
>> That will remove the asterisks from "file.txt" and dump the result
>> into "new_file.txt"
> Fantastic! Thank you. That does what I need. For my benefit, how does
> that command work?

The basic syntax for sed is:

sed [expression] [file]

So [expression] is applied against [file] and the result dumped to  
standard out.  So by redirecting STDOUT using ">" it writes the result  
to the file "new_file.txt" instead of the screen.

Now the real magic in [expression] that I used is in putting the whole  
thing in double quotes to avoid the shell (bash etc) trying to expand  
the "*" and also avoids the need to escape characters.  However the  
"s" command I gave sed means substitute:

s/substitution pattern/replacement/  (although I used "#" instead of  
"/").  So in this case sed will replace the FIRST instance  
"substitution pattern" with "replacement" on each line it occurs.  If  
there is more than one occurrence on any line, you need to suffix the  
substitution command with "g" to make it globally replace ALL  
occurrences on each line.

There are plenty of really good sed tutorials online just google "sed  
primer".

Regards,

James 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2417 bytes
Desc: not available
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20081016/974762e3/attachment.bin>


More information about the ubuntu-users mailing list