Sed Delete Last Character

‮.www‬lautturi.com
Sed Delete Last Character

To delete the last character from each line of a file using the sed command, you can use the s/<pattern>$// substitution command.

The <pattern>$ regular expression matches the end of each line, and the s/<pattern>$// substitution command replaces the matched pattern with an empty string.

For example, to delete the last character from each line of the file input.txt and save the result to the file output.txt, you can use the following command:

sed 's/.$//' input.txt > output.txt

This will delete the last character from each line of the input.txt file and save the modified output to the output.txt file.

You can use a different regular expression to delete a different character or set of characters from the end of each line. For example, to delete the last two characters from each line, you can use the following command:

sed 's/..$//' input.txt > output.txt

This will delete the last two characters from each line of the input.txt file and save the modified output to the output.txt file.

It is important to note that the sed command operates on individual lines, and will not delete characters from the middle of a line or from lines that do not have the specified character or set of characters. To delete characters from the middle of a line or from lines that do not have the specified character or set of characters, you may need to use a different approach.

For more information about the sed command and its substitution command, you can consult the documentation for your specific Unix or Linux system.

Created Time:2017-10-30 14:27:18  Author:lautturi