how to print every fifth row in a file
I have a file with numbers
20
18
21
16
14
30
40
24
and I need to output four files with rows printed with intervals of 4
So we have rows 1,5,9...
20
14
Then rows 2,6,10...
18
30
Then 3,7,11...
21
40
and then 4,8,12...
16
24
I did try the code below but it does not give me the control over the starting row
awk 'NR % 4 == 0'
bash awk sed
add a comment |
I have a file with numbers
20
18
21
16
14
30
40
24
and I need to output four files with rows printed with intervals of 4
So we have rows 1,5,9...
20
14
Then rows 2,6,10...
18
30
Then 3,7,11...
21
40
and then 4,8,12...
16
24
I did try the code below but it does not give me the control over the starting row
awk 'NR % 4 == 0'
bash awk sed
add a comment |
I have a file with numbers
20
18
21
16
14
30
40
24
and I need to output four files with rows printed with intervals of 4
So we have rows 1,5,9...
20
14
Then rows 2,6,10...
18
30
Then 3,7,11...
21
40
and then 4,8,12...
16
24
I did try the code below but it does not give me the control over the starting row
awk 'NR % 4 == 0'
bash awk sed
I have a file with numbers
20
18
21
16
14
30
40
24
and I need to output four files with rows printed with intervals of 4
So we have rows 1,5,9...
20
14
Then rows 2,6,10...
18
30
Then 3,7,11...
21
40
and then 4,8,12...
16
24
I did try the code below but it does not give me the control over the starting row
awk 'NR % 4 == 0'
bash awk sed
bash awk sed
edited Nov 14 '18 at 17:34
kvantour
9,66831731
9,66831731
asked Nov 14 '18 at 17:05
vkaul11vkaul11
1,40343053
1,40343053
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
In a single awk you can do:
awk '{print > ("file" (NR%4))}' inputfile
This will send the output to files file0
, file1
, file2
and file3
add a comment |
You may use these awk
commands:
awk -v n=1 'NR%4 == n%4' file
20
14
awk -v n=2 'NR%4 == n%4' file
18
30
awk -v n=3 'NR%4 == n%4' file
21
40
awk -v n=4 'NR%4 == n%4' file
16
24
add a comment |
IMHO awk
is the best solution. You can use sed
:
Inputfile generated with seq 12
:
for ((i=1;i<5; i++)); do
sed -n $i~4w$i.out <(seq 12)
done
Here w$i.out
writes to file $i.out.
add a comment |
This might work for you (GNU sed):
sed -ne '1~4w file1' -e '2~4w file2' -e '3~4w file3' -e '4~4w file4' file
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305380%2fhow-to-print-every-fifth-row-in-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
In a single awk you can do:
awk '{print > ("file" (NR%4))}' inputfile
This will send the output to files file0
, file1
, file2
and file3
add a comment |
In a single awk you can do:
awk '{print > ("file" (NR%4))}' inputfile
This will send the output to files file0
, file1
, file2
and file3
add a comment |
In a single awk you can do:
awk '{print > ("file" (NR%4))}' inputfile
This will send the output to files file0
, file1
, file2
and file3
In a single awk you can do:
awk '{print > ("file" (NR%4))}' inputfile
This will send the output to files file0
, file1
, file2
and file3
answered Nov 14 '18 at 17:29
kvantourkvantour
9,66831731
9,66831731
add a comment |
add a comment |
You may use these awk
commands:
awk -v n=1 'NR%4 == n%4' file
20
14
awk -v n=2 'NR%4 == n%4' file
18
30
awk -v n=3 'NR%4 == n%4' file
21
40
awk -v n=4 'NR%4 == n%4' file
16
24
add a comment |
You may use these awk
commands:
awk -v n=1 'NR%4 == n%4' file
20
14
awk -v n=2 'NR%4 == n%4' file
18
30
awk -v n=3 'NR%4 == n%4' file
21
40
awk -v n=4 'NR%4 == n%4' file
16
24
add a comment |
You may use these awk
commands:
awk -v n=1 'NR%4 == n%4' file
20
14
awk -v n=2 'NR%4 == n%4' file
18
30
awk -v n=3 'NR%4 == n%4' file
21
40
awk -v n=4 'NR%4 == n%4' file
16
24
You may use these awk
commands:
awk -v n=1 'NR%4 == n%4' file
20
14
awk -v n=2 'NR%4 == n%4' file
18
30
awk -v n=3 'NR%4 == n%4' file
21
40
awk -v n=4 'NR%4 == n%4' file
16
24
answered Nov 14 '18 at 17:11
anubhavaanubhava
532k47331408
532k47331408
add a comment |
add a comment |
IMHO awk
is the best solution. You can use sed
:
Inputfile generated with seq 12
:
for ((i=1;i<5; i++)); do
sed -n $i~4w$i.out <(seq 12)
done
Here w$i.out
writes to file $i.out.
add a comment |
IMHO awk
is the best solution. You can use sed
:
Inputfile generated with seq 12
:
for ((i=1;i<5; i++)); do
sed -n $i~4w$i.out <(seq 12)
done
Here w$i.out
writes to file $i.out.
add a comment |
IMHO awk
is the best solution. You can use sed
:
Inputfile generated with seq 12
:
for ((i=1;i<5; i++)); do
sed -n $i~4w$i.out <(seq 12)
done
Here w$i.out
writes to file $i.out.
IMHO awk
is the best solution. You can use sed
:
Inputfile generated with seq 12
:
for ((i=1;i<5; i++)); do
sed -n $i~4w$i.out <(seq 12)
done
Here w$i.out
writes to file $i.out.
answered Nov 14 '18 at 20:50
Walter AWalter A
11k21132
11k21132
add a comment |
add a comment |
This might work for you (GNU sed):
sed -ne '1~4w file1' -e '2~4w file2' -e '3~4w file3' -e '4~4w file4' file
add a comment |
This might work for you (GNU sed):
sed -ne '1~4w file1' -e '2~4w file2' -e '3~4w file3' -e '4~4w file4' file
add a comment |
This might work for you (GNU sed):
sed -ne '1~4w file1' -e '2~4w file2' -e '3~4w file3' -e '4~4w file4' file
This might work for you (GNU sed):
sed -ne '1~4w file1' -e '2~4w file2' -e '3~4w file3' -e '4~4w file4' file
answered Nov 16 '18 at 1:48
potongpotong
36.2k43062
36.2k43062
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305380%2fhow-to-print-every-fifth-row-in-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown