Remove the First and Last Character of a Line with sed (Linux)
Continuing the series on useful console commands, this time it’s sed.
In one of my scripts I needed to remove the first and the last character of each line. You can do it with sed.
A working example:
cat 1.txt | sed -e "s/^.//;s/.$//"
The command prints a text file and uses a sed regular expression to replace the first and the last character of every line with nothing, which simply deletes them.
The number of dots controls how many characters are removed (a dot matches any single character, so one dot is one character). For example, to remove the first two and the last two characters:
sed -e "s/^..//;s/..$//" 1.txt
