Bash - truncate even lines in txt file
Usage awk
:
$ awk -v x=2 -v y=3 '0==NR%2 {$0=substr($0,x+1,length($0)-y-x)} 1' file
1
BBBBB
2
DD
How it works:
-
-v x=2 -v y=3
The parameters
-v
define our two variables,x
andy
. -
0==NR%2 {$0=substr($0,x+1,length($0)-y-x)}
NR is the row count. When
0 == NR%2
, we are on an even line and delete charactersx
from the beginning andy
from the end. Bawk
,$0
- the whole line. We replace with a substring that starts at positionx+1
and has lengthlength($0)-y-x
. -
1
This is a critical shorthand for printing a line.
source to share