in reply to How to write a new line in a while loop?
You're doing a substitution at every line.
A "one-liner" solution:
perl -i.bak -nle' BEGIN { print("IPADDR=", shift(@ARGV)) } print if !/^IPADDR=/; ' 1.1.1.1 dir/sys/network-scripts/ifcfg-eth0
A script solution:
print("IPADDR=$ip\n"); while (<$fh_in>) { print $fh_out $_ if !/^IPADDR=/; }
If you wanted to use substitution (to maintain order), it would look like
my $found = 0; while (<$fh_in>) { ++$found if s/^IPADDR=.*/IPADDR=$ip/; print $fh_out $_; } print("IPADDR=$ip\n") if !$found;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to write a new line in a while loop?
by xjlittle (Beadle) on Oct 06, 2009 at 23:48 UTC |