Rather simply sed:
command/output explanation $ cat simple Here is a file containing some words. A total of 3 lines have been used. This line makes the last one right.The contents of a file named simple: $ sed s/i/x/ simple Here xs a file containing some words. A total of 3 lxnes have been used. Thxs line makes the last one right.We use sed to replace 'i' by 'x' throughout the file (but only once per line) $ cat simple Here is a file containing some words. A total of 3 lines have been used. This line makes the last one right.Note that the file 'simple' remains unchanged. $ sed s/i/x/g simple Here xs a fxle contaxnxng some words. A total of 3 lxnes have been used. Thxs lxne makes the last one rxght.This time we do it to all the 'i's in the file. $ sed 1s/i/x/g simple Here xs a fxle contaxnxng some words. A total of 3 lines have been used. This line makes the last one right.Here we do it to all the 'i's, but only in the first line. $ sed s/file.*/file./ simple Here is a file. A total of 3 lines have been used. This line makes the last one right.find the first occurrence of 'file' in each line and truncate the line at that point, adding a '.' at the end. $ sed 2d simple Here is a file containing some words. This line makes the last one right.delete the second line from our display of the file. $ cat simple Here is a file containing some words. A total of 3 lines have been used. This line makes the last one right.Just a reminder of what the file 'simple' looks like. $sed 1,2d simple This line makes the last one right.deleting a range of lines (lines 1 through 2) $sed /makes/d simple Here is a file containing some words. A total of 3 lines have been used.deleting any line that matches a regular expression. $sed /makes/p simple Here is a file containing some words. A total of 3 lines have been used. This line makes the last one right. This line makes the last one right.duplicating any line that matches a regular expression $sed -n /makes/p simple This line makes the last one right.Produce only the lines that match a regular expression.
The -n flag keeps sed "quiet" unless overtly told to print a line, through the 'p' command. $sed /last/s/is/at/ simple Here is a file containing some words. A total of 3 lines have been used. That line makes the last one right.Substitute the string 'is' by 'at' but only in lines containing a particular regular expression. Note that 'is' in the first line is unaffected.
$ sed s/i/x/2 simple Here is a fxle containing some words. A total of 3 lines have been used. This lxne makes the last one right.modify the 2nd occurence of 'i' in each line. $ sed = simple 1 Here is a file containing some words. 2 A total of 3 lines have been used. 3 This line makes the last one right.Something very easy, but sometimes useful -- the equal sign places line numbers into the output.
Here is some more complex material on sed.