Sedding
First let’s generate some text to practice on
cat --help > randtxt
Compare the line numbers from the command nl
with and without the option -nl
cat randtxt | nl
cat randtxt | nl -ba
The second is good for telling us actual line numbers in the file because it counts the blank lines..
Now we can do a replace command for the word FILE
.
cat randtxt | sed 's/FILE/DAFTAR/g' # without g, only the first occurance in the line is replaced
What if we just want to replace the match on the 3rd line? or certain line ranges?
cat randtxt | sed '5 s/FILE/DAFTAR/g'
Extracting lines containing text matches. These commands are equivelant
cat randtxt | nl -ba | sed -ne '/FILE/p'
cat randtxt | nl -ba | grep 'FILE'
Specifying line ranges for printing (or deleting d
, or replacing s
):
cat randtxt |sed -ne '3p' # printing specific line numbers
cat randtxt |sed -ne '3,7p'
cat randtxt |sed -ne '3,$p' # to the end of the file
cat randtxt |sed -ne '3,+2 p' # to 2 lines after the the 3rd line