I have a text file with thousands of lines. The last 7 characters on each line are a mix of letters and numbers (eg AAP8945 or GGR6645). I want to save these in a separate file.
Excuse the noob question, but I can't work it out.
I have a text file with thousands of lines. The last 7 characters on each line are a mix of letters and numbers (eg AAP8945 or GGR6645). I want to save these in a separate file.
Excuse the noob question, but I can't work it out.
grep
Assuming you have GNU grep
:
grep -o -E '.{7}$' input > output
The -o
option means 'output only what matches' (rather than the whole line). This is the key feature which makes it possible to use grep
for the job. Without support for -o
(or an equivalent option), grep
is the wrong tool for the job.
The -E
option is for extended regular expressions, and it means that the .
(any character) is matched 7 times and then matches the end of line.
grep
If you don't have GNU grep
(or a compatible grep
with the -o
option or equivalent), then you can use sed
instead (GNU or any other variant):
sed -e 's/.*\(.\{7\}\)$/\1/' input > output
This matches the start of the line (.*
) and captures the last 7 characters (\(…\)
) of the line; it replaces the whole with the captured part, and prints the result. If your variant of sed
has extended regular expressions (usually -E
or sometimes -r
), then:
sed -E -e 's/.*(.{7})$/\1/' input > output
The difference is in the number of backslashes needed.
Both of those will print any short lines in their entirety. If those should be omitted, use:
sed -n -e 's/.*\(.\{7\}\)$/\1/p' input > output
sed -n -E -e 's/.*(.{7})$/\1/p' input > output
grep -Eo '.{7}$'
Or without grep:
rev input|cut -c -7|rev >output
The double rev
is necessary here because I can not specify a position of the text from the right with cut
.