Reputation:
I am trying to go through a file and keep a consecutive group of 4
rows out of each consecutive group of 40
rows.
So in the whole file, I would keep rows 1-4
, 41-44
, 81-84
, etc.
I tried using sed
, but I am really only able to remove specific rows, not do a pattern like this.
Many thanks...
Upvotes: 1
Views: 124
Reputation: 10039
simple but do what is asked (thks to @Jotne for remark based on seq test)
sed -n 'N;N;N;p;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N;N' YourFile
just for fun
sed -n '
x
s/^$/ppppfffffffffffffffffffffffffffffffffff/
s/^p//
t keep
s/^f//
x
b
:keep
x
p' YourFile
there is another more traditionnal way by using only d
, p
and N
but not funny at all :-).
I use a kind of template counter that p
rint and f
orget lines (the ppppfffffffffffffffffffffffffffffffffff
) keep in hold buffer
Upvotes: 0
Reputation: 58430
This might work for you (GNU sed):
sed -n '1~40,+3p' file
Use a 40 line step starting at line 1 and range it over 4 lines.
Upvotes: 2
Reputation: 41456
This awk
should do:
awk 'NR%40==1 || NR%40==2 || NR%40==3 || NR%40==4' file
A loop version:
awk '{for (i=1;i<5;i++) if (NR%40==i) print $0}' file
Found this should work after I tested various solution:
awk 'NR%40~/^[1-4]$/' file
test
seq 1 100 > file
awk 'NR%40~/^[1-4]$/' file
1
2
3
4
41
42
43
44
81
82
83
84
Upvotes: 4
Reputation: 75565
You might be better off with awk
. This is not the most concise solution, but should get you what you want. The variable NR
represents the row number.
awk '(NR - 1) % 40 ==0 || (NR - 2) % 40 ==0 || (NR - 3) % 40 ==0 || (NR - 4) % 40 ==0 ' Input.txt
I tested this like this:
seq 1 50 > /tmp/Input.txt
awk '(NR - 1) % 40 ==0 || (NR - 2) % 40 ==0 || (NR - 3) % 40 ==0 || (NR - 4) % 40 ==0 ' /tmp/Input.txt
If you want to modify the original file, then output it to a temporary file and move it back.
awk '(NR - 1) % 40 ==0 || (NR - 2) % 40 ==0 || (NR - 3) % 40 ==0 || (NR - 4) % 40 ==0 ' Input.txt > /tmp/TempOutput
mv /tmp/TempOutput Input.txt
Upvotes: 1