Volodymyr
Volodymyr

Reputation: 1442

Limit lines in a file to some value of characters

I have some file: example.txt with the following content:

message(1)
message2(2).ERROR:nextMessage.response.statusLine.statusCode....blah-blah.verdict fail.
message(3)

I get this file as a result of grep:

grep -oP 'some regex' source.txt > example.txt

What I need is to limit(before writing into file or after) every string to specified number of characters (lets say 20) starting from the end of a line. Something like tail -c 20 but apply it for every line.

Upvotes: 2

Views: 82

Answers (1)

anubhava
anubhava

Reputation: 785246

To print last 20 characters in a line, you can use:

grep -oP 'some regex' source.txt |
awk -v n=20 '{print substr($0, length($0)-n+1, n)}' > example.txt

Upvotes: 2

Related Questions