So, How Would You Do It?

A 5 star hangout for overworked and underpaid system admins.
Post Reply
User avatar
AlanBartlett
Forum Moderator
Posts: 9320
Joined: 2007/10/22 11:30:09
Location: ~/Earth/UK/England/Suffolk
Contact:

So, How Would You Do It?

Post by AlanBartlett » 2010/05/10 14:32:35

Earlier today, I was asked a question: How would I insert the string " * " at the beginning of every line of an ASCII text file?

If it were a one-off occurrence and if the file was only four lines long (or even 24 lines long), I would have proceeded with my brain in idling mode and used [b]vi[/b]. However, I suspect a file of 50,000 lines was involved and so, as my brain was still idling, quickly responded --

[code]
[b]awk '{ print " * "$0 }' text > temp && mv temp text[/b]
[/code]
That really isn't the best solution, so who can come up the the shorted and most sophisticated solution to this query?

It's not a competition but I'm willing to give a (pseudo) :pint: from [b]Ned[/b]'s (legendary) beer-cellar to the first person who's suggestion matches the requirements, above. ;-)

User avatar
jlehtone
Posts: 2212
Joined: 2007/12/11 08:17:33
Location: Finland

Re: So, How Would You Do It?

Post by jlehtone » 2010/05/10 16:00:22

The first that I can think of:
[code]sed -i -e 's/^/ * /' text[/code]
(Recent sed does have the '-i' inplace option. I do know of perl having '-p -i -e' options too, but I don't know perl.)

User avatar
AlanBartlett
Forum Moderator
Posts: 9320
Joined: 2007/10/22 11:30:09
Location: ~/Earth/UK/England/Suffolk
Contact:

Re: So, How Would You Do It?

Post by AlanBartlett » 2010/05/10 16:32:47

Indeed, a simple [i]sed[/i] substitution is all that is required. Taking your offering, I've mutated it into --

[code]
[b]sed -i.old 's/^/ * /' text[/b]
[/code]
Unless anyone else can come up with a more sophisticated suggestion, I do think we have the winner. :-D

JakeS
Posts: 84
Joined: 2009/02/01 08:12:33
Location: England, Lincolnshire

Re: So, How Would You Do It?

Post by JakeS » 2010/05/10 22:56:31

[quote]
jlehtone wrote:
The first that I can think of:
[code]sed -i -e 's/^/ * /' text[/code]
(Recent sed does have the '-i' inplace option. I do know of perl having '-p -i -e' options too, but I don't know perl.)[/quote]Oh shoot, I was gonna say that :|

Though if, the file starts with say, all the same beginning letter (I doubt it, but it's possible) you could just open up a text editor, with the ability to "Search and replace" and replace all instances of "A" to "*A" (Preferably by pressing a replace all button)

:-P

User avatar
jlehtone
Posts: 2212
Joined: 2007/12/11 08:17:33
Location: Finland

Re: So, How Would You Do It?

Post by jlehtone » 2010/05/11 07:17:06

1. sed [b]is[/b] a text editor. ;-) (Or would you shovel binary crap to it?)
2. You can never trust all lines to start the same, nor not finding the character later in line too, so you'd have to match the begin anyway. /^/.

[b]Alan[/b]: keeping backups makes your version superior (unless the user [b]git[/b]s all text).

User avatar
AlanBartlett
Forum Moderator
Posts: 9320
Joined: 2007/10/22 11:30:09
Location: ~/Earth/UK/England/Suffolk
Contact:

Re: So, How Would You Do It?

Post by AlanBartlett » 2010/05/11 14:32:37

Hmm, I think [b]Jake[/b], in his enthusiasm to obtain some of [b]Ned[/b]'s throat embrocation, has missed my comment --

[quote]
I suspect a file of 50,000 lines was involved
[/quote]
For such a case, that is exactly why [i]sed[/i] was invented.

I have a hard copy of:

[b]SED -- A Non-interactive Text Editor[/b] by [i]Lee E. McMahon[/i] of Bell Laboratories, Murray Hill, New Jersey 07974

Dated: August 15th 1978.

The Abstract reads:

[code]
[i]Sed[/i] is a non-interactive context editor that runs on the UNIX operating system.
[i]Sed[/i] is designed to be especially useful in three cases:

1) To edit files too large for comfortable interactive editing.
2) To edit any size file when the sequence of editing commands is too complicated to be typed in interactive mode.
3) To perform multiple 'global' editing functions efficiently in one pass thorough the input.

This memorandum constitutes a manual for users of [i]sed[/i].
[/code]
The example that I alluded to in my initial post is covered by cases (1) & (3) from the abstract, above.

So [b]Ned[/b]'s loss is [b]jlehtone[/b]'s gain, as a :pint: from the [i]Sliderland[/i] beer cellar is in transit to Finland as I type this post. :-D

(And I declare that the non-contest is now over.)

Post Reply