Re-print an array in a certain format?
Clash Royale CLAN TAG#URR8PPP
I wish to take 10 random lines of file, which is 100 lines long. First, I randomly generate 10 integers between 1 and 100 (inclusive) with
ind=$(shuf -i 1-100 -n 10 | sort -n)
Then, I wish to use sed
to extract out those lines (indicated by ind
). So I need to re-print the array ind
to generate
<ind(1)>p;<ind(2)>p;...;<ind(10)>p
as in
sed -n '<ind(1)>p;<ind(2)>p;...;<ind(10)>p' ~/orig.txt > ~/short.txt
How may I do this?
shell-script sed array
add a comment |
I wish to take 10 random lines of file, which is 100 lines long. First, I randomly generate 10 integers between 1 and 100 (inclusive) with
ind=$(shuf -i 1-100 -n 10 | sort -n)
Then, I wish to use sed
to extract out those lines (indicated by ind
). So I need to re-print the array ind
to generate
<ind(1)>p;<ind(2)>p;...;<ind(10)>p
as in
sed -n '<ind(1)>p;<ind(2)>p;...;<ind(10)>p' ~/orig.txt > ~/short.txt
How may I do this?
shell-script sed array
add a comment |
I wish to take 10 random lines of file, which is 100 lines long. First, I randomly generate 10 integers between 1 and 100 (inclusive) with
ind=$(shuf -i 1-100 -n 10 | sort -n)
Then, I wish to use sed
to extract out those lines (indicated by ind
). So I need to re-print the array ind
to generate
<ind(1)>p;<ind(2)>p;...;<ind(10)>p
as in
sed -n '<ind(1)>p;<ind(2)>p;...;<ind(10)>p' ~/orig.txt > ~/short.txt
How may I do this?
shell-script sed array
I wish to take 10 random lines of file, which is 100 lines long. First, I randomly generate 10 integers between 1 and 100 (inclusive) with
ind=$(shuf -i 1-100 -n 10 | sort -n)
Then, I wish to use sed
to extract out those lines (indicated by ind
). So I need to re-print the array ind
to generate
<ind(1)>p;<ind(2)>p;...;<ind(10)>p
as in
sed -n '<ind(1)>p;<ind(2)>p;...;<ind(10)>p' ~/orig.txt > ~/short.txt
How may I do this?
shell-script sed array
shell-script sed array
edited May 27 '15 at 22:57
Gilles
528k12810581583
528k12810581583
asked May 27 '15 at 16:38
Sibbs Gambling
5252922
5252922
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
In your example, ind
is a string, not an array. You must use:
ind=($(shuf -i 1-100 -n 10 | sort -n))
to make ind
to be array (in shell support array like bash
, zsh
, ksh
).
Simply, you can try:
$ printf '%spn' $(shuf -i 1-100 -n 10 | sort -n) > /tmp/short.sed
$ sed -n -f /tmp/short.sed < orig.txt > short.txt
add a comment |
An awk
based solution (without need of sorting):
awk 'NR==FNRa[$1];next NR in a' <(shuf -i 1-100 -n 10) ~/orig.txt > ~/short.txt
And a pure GNU awk
variant (without need of other external processes):
awk '
BEGIN srand(); do a[int(100*rand()+1)]; while (length(a)<10)
NR in a
' ~/orig.txt > ~/short.txt
add a comment |
Instead of trying to go the long way, creating a sed
script to extract the lines from your file, just use shuf
directly on the file:
shuf -n 10 ~/short.txt
If you want the lines in the order that they are found in the original file:
cat -n ~/short.txt | shuf -n 10 | sort -n | cut -f 2-
This enumerates the lines in the file, extracts 10 lines from it (in random order), sorts the extracted lines, and removes the numbering.
add a comment |
U could...
shuf -i 1-100 -n 10 | sort -n | sed 's/$/p;/' | sed -nf - orig.txt >short.txt
or, under bash
sed -nf <(sed 's/$/p;/' <(sort -n < <(shuf -i 1-100 -n 10))) <orig.txt >short.txt
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2funix.stackexchange.com%2fquestions%2f205919%2fre-print-an-array-in-a-certain-format%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 your example, ind
is a string, not an array. You must use:
ind=($(shuf -i 1-100 -n 10 | sort -n))
to make ind
to be array (in shell support array like bash
, zsh
, ksh
).
Simply, you can try:
$ printf '%spn' $(shuf -i 1-100 -n 10 | sort -n) > /tmp/short.sed
$ sed -n -f /tmp/short.sed < orig.txt > short.txt
add a comment |
In your example, ind
is a string, not an array. You must use:
ind=($(shuf -i 1-100 -n 10 | sort -n))
to make ind
to be array (in shell support array like bash
, zsh
, ksh
).
Simply, you can try:
$ printf '%spn' $(shuf -i 1-100 -n 10 | sort -n) > /tmp/short.sed
$ sed -n -f /tmp/short.sed < orig.txt > short.txt
add a comment |
In your example, ind
is a string, not an array. You must use:
ind=($(shuf -i 1-100 -n 10 | sort -n))
to make ind
to be array (in shell support array like bash
, zsh
, ksh
).
Simply, you can try:
$ printf '%spn' $(shuf -i 1-100 -n 10 | sort -n) > /tmp/short.sed
$ sed -n -f /tmp/short.sed < orig.txt > short.txt
In your example, ind
is a string, not an array. You must use:
ind=($(shuf -i 1-100 -n 10 | sort -n))
to make ind
to be array (in shell support array like bash
, zsh
, ksh
).
Simply, you can try:
$ printf '%spn' $(shuf -i 1-100 -n 10 | sort -n) > /tmp/short.sed
$ sed -n -f /tmp/short.sed < orig.txt > short.txt
edited May 28 '15 at 3:23
answered May 27 '15 at 16:50
cuonglm
102k23200301
102k23200301
add a comment |
add a comment |
An awk
based solution (without need of sorting):
awk 'NR==FNRa[$1];next NR in a' <(shuf -i 1-100 -n 10) ~/orig.txt > ~/short.txt
And a pure GNU awk
variant (without need of other external processes):
awk '
BEGIN srand(); do a[int(100*rand()+1)]; while (length(a)<10)
NR in a
' ~/orig.txt > ~/short.txt
add a comment |
An awk
based solution (without need of sorting):
awk 'NR==FNRa[$1];next NR in a' <(shuf -i 1-100 -n 10) ~/orig.txt > ~/short.txt
And a pure GNU awk
variant (without need of other external processes):
awk '
BEGIN srand(); do a[int(100*rand()+1)]; while (length(a)<10)
NR in a
' ~/orig.txt > ~/short.txt
add a comment |
An awk
based solution (without need of sorting):
awk 'NR==FNRa[$1];next NR in a' <(shuf -i 1-100 -n 10) ~/orig.txt > ~/short.txt
And a pure GNU awk
variant (without need of other external processes):
awk '
BEGIN srand(); do a[int(100*rand()+1)]; while (length(a)<10)
NR in a
' ~/orig.txt > ~/short.txt
An awk
based solution (without need of sorting):
awk 'NR==FNRa[$1];next NR in a' <(shuf -i 1-100 -n 10) ~/orig.txt > ~/short.txt
And a pure GNU awk
variant (without need of other external processes):
awk '
BEGIN srand(); do a[int(100*rand()+1)]; while (length(a)<10)
NR in a
' ~/orig.txt > ~/short.txt
edited May 27 '15 at 17:20
answered May 27 '15 at 17:07
Janis
10.1k21437
10.1k21437
add a comment |
add a comment |
Instead of trying to go the long way, creating a sed
script to extract the lines from your file, just use shuf
directly on the file:
shuf -n 10 ~/short.txt
If you want the lines in the order that they are found in the original file:
cat -n ~/short.txt | shuf -n 10 | sort -n | cut -f 2-
This enumerates the lines in the file, extracts 10 lines from it (in random order), sorts the extracted lines, and removes the numbering.
add a comment |
Instead of trying to go the long way, creating a sed
script to extract the lines from your file, just use shuf
directly on the file:
shuf -n 10 ~/short.txt
If you want the lines in the order that they are found in the original file:
cat -n ~/short.txt | shuf -n 10 | sort -n | cut -f 2-
This enumerates the lines in the file, extracts 10 lines from it (in random order), sorts the extracted lines, and removes the numbering.
add a comment |
Instead of trying to go the long way, creating a sed
script to extract the lines from your file, just use shuf
directly on the file:
shuf -n 10 ~/short.txt
If you want the lines in the order that they are found in the original file:
cat -n ~/short.txt | shuf -n 10 | sort -n | cut -f 2-
This enumerates the lines in the file, extracts 10 lines from it (in random order), sorts the extracted lines, and removes the numbering.
Instead of trying to go the long way, creating a sed
script to extract the lines from your file, just use shuf
directly on the file:
shuf -n 10 ~/short.txt
If you want the lines in the order that they are found in the original file:
cat -n ~/short.txt | shuf -n 10 | sort -n | cut -f 2-
This enumerates the lines in the file, extracts 10 lines from it (in random order), sorts the extracted lines, and removes the numbering.
edited Dec 18 at 19:34
answered Aug 1 at 11:20
Kusalananda
121k16229372
121k16229372
add a comment |
add a comment |
U could...
shuf -i 1-100 -n 10 | sort -n | sed 's/$/p;/' | sed -nf - orig.txt >short.txt
or, under bash
sed -nf <(sed 's/$/p;/' <(sort -n < <(shuf -i 1-100 -n 10))) <orig.txt >short.txt
add a comment |
U could...
shuf -i 1-100 -n 10 | sort -n | sed 's/$/p;/' | sed -nf - orig.txt >short.txt
or, under bash
sed -nf <(sed 's/$/p;/' <(sort -n < <(shuf -i 1-100 -n 10))) <orig.txt >short.txt
add a comment |
U could...
shuf -i 1-100 -n 10 | sort -n | sed 's/$/p;/' | sed -nf - orig.txt >short.txt
or, under bash
sed -nf <(sed 's/$/p;/' <(sort -n < <(shuf -i 1-100 -n 10))) <orig.txt >short.txt
U could...
shuf -i 1-100 -n 10 | sort -n | sed 's/$/p;/' | sed -nf - orig.txt >short.txt
or, under bash
sed -nf <(sed 's/$/p;/' <(sort -n < <(shuf -i 1-100 -n 10))) <orig.txt >short.txt
answered Dec 18 at 20:59
F. Hauri
2,6191327
2,6191327
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2funix.stackexchange.com%2fquestions%2f205919%2fre-print-an-array-in-a-certain-format%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