Find and replace text in a file between range of lines using sed
You can use sed addresses:
sed '19,33s/google/facebook/g' file
This will run the substitution on lines between and including 19 and 33.
The form of a sed command is as follows:
[address[,address]]function[arguments]
Where 19,33
is the addreses,s
ubstitute is function
and g
lobal is the argument
How to perform sed replacement on range of lines?
You were close
sed -i '200,220 s/1 2 3 4 5 /replacement numbers/' file
in 200-220 range this will replace '1 2 3 4 5 ' with other stuff only once (per line). If more replacements needed than added g here ...numbers/g' file and so on
Substitution on range of lines
Each can be done with something similar to this:
sed -i "s/<host><\/host>/<host>my_db<\/host>/" foo.txt
The -i
means that it'll overwrite the file in-place. Remove that flag to have the changes written to stdout.
If this is for a regular task to fill in details for the file, consider adding anchors to the file to make it easier. For example:
<host>DB_HOST</host>
Then the sed would just need to be:
sed -i 's/DB_HOST/my_db/'
replace line feeds with sed in a certain range of line
Edited again
Try awk
:
awk '{l=substr($0,1,10);r=substr($0,11);gsub(/n/,"m",l);print l r}' file
where l
is the left part of the string and r
is the right and gsub()
does global substitutions.
Edited
I would probably use Bash parameter substitution for that - documentation here:
#!/bin/bash
while read line
do
left=${line:1:16} # Get left 16 chars
right=${line:17} # Get remainder of line
left=${left//n/m} # Do global replacement in left part
echo $left $right # Show output
done < file
Original answer
Sure, just on lines 2-8
sed '2,8s/foo/bar/' file
Or, between start and end patterns:
sed '/start/,/end/s/foo/bar/' file
perl idiom for substituting in a line range (sed-like)
There is. What you have in perl is the 'range operator'.
It goes a bit like this:
if ( m/Page 5:/ .. m/Page 6:/ ) {
s/this/that/g;
}
This will evaluate as 'true' if you're between the two patterns, and false otherwise.
E.g.:
use strict;
use warnings;
while (<DATA>) {
if ( m/Page 5:/ .. m/Page 6:/ ) {
s/this/that/g;
}
print;
}
__DATA__
Page 1:
this
this
more this
Page 5:
this
this this
this
Page 6:
this
more this
and some more this
search and replace through multiple ranges in vim
Haha... ok, replacing linebreaks with spaces in the first range causes my second range to not exist, as lines 12-18 change to something like 3-9. Solution is to flip the ranges so the range later in the file comes first in the command:
:12,18s/\n/ /g | 2,10&&
Search and replace in a range of line and column
:1,2 s/\%3cBBB/YYY/
\%3c
means third column (see :help /\%c
or more globally :help pattern
)
Search and replace in vim in specific lines
Vim has special regular expression atoms that match in certain lines, columns, etc.; you can use them (possibly in addition to the range) to limit the matches:
:5,12s/\(\%5l\|\%12l\)foo/bar/g
See :help /\%l
Related Topics
How to Make Linux Power Off When Halt Is Run
Cmsg_Nxthdr() Returns Null Even Though There Are More Cmsghdr Objects
Rust Linux Version Glibc Not Found - Compile for Different Glibc/Libc6 Version
How Does Gdb Start an Assembly Compiled Program and Step One Line at a Time
Is Ethernet Checksum Exposed via Af_Packet
Determine Vm Size of Process Killed by Oom-Killer
Stop Being Root in the Middle of a Script That Was Run with Sudo
Replace Bash Variables in Template File
Usb Modem Is Echoing Back Wrong Characters
Multiple -A with Greater Than/Less Than Break Bash Script
Start Docker-Compose Automatically on Ec2 Startup
Comparison of Cat Pipe Awk Operation to Awk Command on a File
How to Start/Restart/Stop Apache Server on Linux as Non-Root User