Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Your advice and opinion on editing files in scripts 1

Status
Not open for further replies.

kasparov

Programmer
Feb 13, 2002
203
0
0
GB
I'm curious to hear what people think about tbe best way to edit files automatically in a script.

I'm making edits to a number of lines in a file (10+ lines need changing). It's straightforward editing which never varies so it's easy to do but what method would you use?

The way I'm doing it is by using a series of (single operation) sed statements each time writing the output to a temporary file (in /tmp), then editing that file & writing to another file (in /tmp). I toggle between these 2 files until I'm finished then send the output to the file I want to use.

I do this because I find it easier to document internally & I expect others to find it easier to maintain.

I could use a long string of sed commands which no doubt could be more efficient but I think would be harder for others to read.

There will be lots of ways to do this & I'm curious what your views are. Maybe I'm missing a neat, efficient but also easily read method? I consider maintainability to be the main priority as it's not going to be a long, processor intensive operation.

Thanks, Chris
 
Hi

Chris said:
I could use a long string of sed commands which no doubt could be more efficient but I think would be harder for others to read.
Not necessarily. Any code is quite well readable if is indented and commented. For example :
Code:
sed [green][i]'[/i][/green]
[green][i]# add a title to the file[/i][/green]
[green][i]1i I Am Title[/i][/green]

[green][i]# capitalize each line[/i][/green]
[green][i]s/\(.\)/\u\1/[/i][/green]

[green][i]# emphasize each word sed[/i][/green]
[green][i]s/\bsed\b/_SED_/g[/i][/green]
[green][i]'[/i][/green] /input/file
Or :
Code:
sed -f change.sed /input/file
Code:
# add a title to the file
1i I Am Title

# capitalize each line
s/\(.\)/\u\1/

# emphasize each word sed
s/\bsed\b/_SED_/g
Or :
Code:
change.sed /input/file
Code:
#!/usr/bin/sed -f

# add a title to the file
1i I Am Title

# capitalize each line
s/\(.\)/\u\1/

# emphasize each word sed
s/\bsed\b/_SED_/g


Feherke.
 
OK - sounds good. I like the first example as then the code is all in one file.

I'll use that & report back.

Thanks feherke
 
I tend to use the perl -pi.bak -e '<insert sed-like perl-stuff>' /some/file method quite a lot. It saves you managing the copying and backing up the original file yourself. It's similar to the GNU sed's -i mode, but since I work across multiple platforms the perl version tends to work everywhere.

All of feherke's suggestions equally apply (i.e. where to put the code and commenting for readability, etc.) to this method too.

Annihilannic.
 
Thanks for the responses chaps. ... ruby ... perl ... it's all a bit much for me. I'll stick with good old sed :)

Star for feherke as I used his suggestion but thanks again to both.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top