You are not logged in.

#1 2011-06-16 17:11:28

I'mGeorge
Member
Registered: 2011-03-23
Posts: 150

[SOLVED] shell script trouble

I need to insert some characters after a line that contains a certain word. Let's say my text file contains something like:

this
is
about
shell
programming

how can I insert the word "advanced" so it would be between about and shell, resulting in a text file that looks like

this
is
about
advanced
shell
programming

I'm not interested in a method that will insert advanced using the number of the proper line, I need to know how can I insert a word, on a new line referring to another string of characters.

Thanks for your time

Last edited by I'mGeorge (2011-06-17 05:27:33)


I've first installed Arch in March

Offline

#2 2011-06-16 17:20:02

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED] shell script trouble

Use sed. Add the -i flag to edit "in place" or just redirect output to a new file.

$ cat file
this
is
about
shell
programming

$ sed '/^about$/aadvanced' file
this
is
about
advanced
shell
programming

$ sed '/^about$/iadvanced' file
this
is
advanced
about
shell
programming

Offline

#3 2011-06-16 17:21:30

egan
Member
From: Mountain View, CA
Registered: 2009-08-17
Posts: 273

Re: [SOLVED] shell script trouble

You can replace the word of interest with itself, a newline, and the inserted word.

< TEXT sed 's/about/about\nadvanced/'

Offline

#4 2011-06-17 05:27:13

I'mGeorge
Member
Registered: 2011-03-23
Posts: 150

Re: [SOLVED] shell script trouble

thanks a bunch, this is exactly what I was looking for


I've first installed Arch in March

Offline

Board footer

Powered by FluxBB