How do I organize and cut this portion of a directory into a file?
My main objective is to copy the contents of a directory and send it to a file. Then cut out the directory location to just have the name. Then to organize it's contents but most appeared. This is also homework and my restrictions are it has to be one command
This is what I thought would do the job but it doesn't
wc -l ~location/folder/folder/*.log > ~/log.info | cut -d "/" -f9 ~/log.info | sort
My output
1 /s/s/s/s/location/folder/folder/a.log
1 /s/s/s/s/location/folder/folder/b.log
1 /s/s/s/s/location/folder/folder/c.log
3 /s/s/s/s/location/folder/folder/d.log
2 /s/s/s/s/location/folder/folder/e.log
What I want it to be
1 a
1 b
1 c
2 e
3 d
shell text-processing pipe io-redirection
add a comment |
My main objective is to copy the contents of a directory and send it to a file. Then cut out the directory location to just have the name. Then to organize it's contents but most appeared. This is also homework and my restrictions are it has to be one command
This is what I thought would do the job but it doesn't
wc -l ~location/folder/folder/*.log > ~/log.info | cut -d "/" -f9 ~/log.info | sort
My output
1 /s/s/s/s/location/folder/folder/a.log
1 /s/s/s/s/location/folder/folder/b.log
1 /s/s/s/s/location/folder/folder/c.log
3 /s/s/s/s/location/folder/folder/d.log
2 /s/s/s/s/location/folder/folder/e.log
What I want it to be
1 a
1 b
1 c
2 e
3 d
shell text-processing pipe io-redirection
add a comment |
My main objective is to copy the contents of a directory and send it to a file. Then cut out the directory location to just have the name. Then to organize it's contents but most appeared. This is also homework and my restrictions are it has to be one command
This is what I thought would do the job but it doesn't
wc -l ~location/folder/folder/*.log > ~/log.info | cut -d "/" -f9 ~/log.info | sort
My output
1 /s/s/s/s/location/folder/folder/a.log
1 /s/s/s/s/location/folder/folder/b.log
1 /s/s/s/s/location/folder/folder/c.log
3 /s/s/s/s/location/folder/folder/d.log
2 /s/s/s/s/location/folder/folder/e.log
What I want it to be
1 a
1 b
1 c
2 e
3 d
shell text-processing pipe io-redirection
My main objective is to copy the contents of a directory and send it to a file. Then cut out the directory location to just have the name. Then to organize it's contents but most appeared. This is also homework and my restrictions are it has to be one command
This is what I thought would do the job but it doesn't
wc -l ~location/folder/folder/*.log > ~/log.info | cut -d "/" -f9 ~/log.info | sort
My output
1 /s/s/s/s/location/folder/folder/a.log
1 /s/s/s/s/location/folder/folder/b.log
1 /s/s/s/s/location/folder/folder/c.log
3 /s/s/s/s/location/folder/folder/d.log
2 /s/s/s/s/location/folder/folder/e.log
What I want it to be
1 a
1 b
1 c
2 e
3 d
shell text-processing pipe io-redirection
shell text-processing pipe io-redirection
edited 53 mins ago
Rui F Ribeiro
40.1k1479135
40.1k1479135
asked Feb 4 '16 at 18:23
JoeJoe
84
84
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can strip out all of the bits you don't want by piping the output through sed
:
wc -l ~location/folder/folder/*.log > ~/log.info
cut -d "/" -f9 ~/log.info | sort | sed 's_/.*/__;s_.log$__'
Doesn't this also remove the frequency of when they should up?
– Joe
Feb 4 '16 at 18:30
No, it preserves everything before the first/
and after the last/
, and then snips.log
off of the end.
– DopeGhoti
Feb 4 '16 at 18:56
add a comment |
How about:
perl -e 'print sort map {s/(d).*(w)..*//r} <>'
(Needs perl 5.14+ - this is probably installed on your system already)
Uses an inlined bit of perl code that:
- reads
<>
which is STDIN or files specified on command line. - Uses a substitution regex which captures a digit
(d)
and a letter just before a.
(w).
- then returns the transformed string.
r
regex flag. - uses
map
to iterateSTDIN
. - and
sort
to ... well, sort. - and
print
to print it.
Output:
1 a
1 b
1 c
2 e
3 d
Alternatively:
perl -e '%x=map {open($f,'<',$_); @f=<$f>; $_=>''.@f} @ARGV;print "$x{$_} $_n" for sort {$x{$a}<=>$x{$b}} keys %x;' ~location/folder/folder/*.log
add a comment |
wc … > ~/log.info | cut ~/log.info | …
The two sides of the pipe are executed in parallel. Unless wc
is especially quick to finish and cut
is especially slow to start, by the time cut
reads ~/log.info
, it's likely to be still empty, or even nonexistent. To compound the problem, the redirection on sort
is also executed in parallel and also truncates the file.
I don't understand what you're trying to do with this intermediate temporary file. It isn't useful:
wc -l ~location/folder/folder/*.log | cut -d "/" -f9 | sort > ~/log.info
It would be simpler to switch to the target directory:
{ cd ~location/folder/folder && wc -l -- *.log; } | sort > ~/log.info
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%2f259918%2fhow-do-i-organize-and-cut-this-portion-of-a-directory-into-a-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can strip out all of the bits you don't want by piping the output through sed
:
wc -l ~location/folder/folder/*.log > ~/log.info
cut -d "/" -f9 ~/log.info | sort | sed 's_/.*/__;s_.log$__'
Doesn't this also remove the frequency of when they should up?
– Joe
Feb 4 '16 at 18:30
No, it preserves everything before the first/
and after the last/
, and then snips.log
off of the end.
– DopeGhoti
Feb 4 '16 at 18:56
add a comment |
You can strip out all of the bits you don't want by piping the output through sed
:
wc -l ~location/folder/folder/*.log > ~/log.info
cut -d "/" -f9 ~/log.info | sort | sed 's_/.*/__;s_.log$__'
Doesn't this also remove the frequency of when they should up?
– Joe
Feb 4 '16 at 18:30
No, it preserves everything before the first/
and after the last/
, and then snips.log
off of the end.
– DopeGhoti
Feb 4 '16 at 18:56
add a comment |
You can strip out all of the bits you don't want by piping the output through sed
:
wc -l ~location/folder/folder/*.log > ~/log.info
cut -d "/" -f9 ~/log.info | sort | sed 's_/.*/__;s_.log$__'
You can strip out all of the bits you don't want by piping the output through sed
:
wc -l ~location/folder/folder/*.log > ~/log.info
cut -d "/" -f9 ~/log.info | sort | sed 's_/.*/__;s_.log$__'
answered Feb 4 '16 at 18:26
DopeGhotiDopeGhoti
45.5k55988
45.5k55988
Doesn't this also remove the frequency of when they should up?
– Joe
Feb 4 '16 at 18:30
No, it preserves everything before the first/
and after the last/
, and then snips.log
off of the end.
– DopeGhoti
Feb 4 '16 at 18:56
add a comment |
Doesn't this also remove the frequency of when they should up?
– Joe
Feb 4 '16 at 18:30
No, it preserves everything before the first/
and after the last/
, and then snips.log
off of the end.
– DopeGhoti
Feb 4 '16 at 18:56
Doesn't this also remove the frequency of when they should up?
– Joe
Feb 4 '16 at 18:30
Doesn't this also remove the frequency of when they should up?
– Joe
Feb 4 '16 at 18:30
No, it preserves everything before the first
/
and after the last /
, and then snips .log
off of the end.– DopeGhoti
Feb 4 '16 at 18:56
No, it preserves everything before the first
/
and after the last /
, and then snips .log
off of the end.– DopeGhoti
Feb 4 '16 at 18:56
add a comment |
How about:
perl -e 'print sort map {s/(d).*(w)..*//r} <>'
(Needs perl 5.14+ - this is probably installed on your system already)
Uses an inlined bit of perl code that:
- reads
<>
which is STDIN or files specified on command line. - Uses a substitution regex which captures a digit
(d)
and a letter just before a.
(w).
- then returns the transformed string.
r
regex flag. - uses
map
to iterateSTDIN
. - and
sort
to ... well, sort. - and
print
to print it.
Output:
1 a
1 b
1 c
2 e
3 d
Alternatively:
perl -e '%x=map {open($f,'<',$_); @f=<$f>; $_=>''.@f} @ARGV;print "$x{$_} $_n" for sort {$x{$a}<=>$x{$b}} keys %x;' ~location/folder/folder/*.log
add a comment |
How about:
perl -e 'print sort map {s/(d).*(w)..*//r} <>'
(Needs perl 5.14+ - this is probably installed on your system already)
Uses an inlined bit of perl code that:
- reads
<>
which is STDIN or files specified on command line. - Uses a substitution regex which captures a digit
(d)
and a letter just before a.
(w).
- then returns the transformed string.
r
regex flag. - uses
map
to iterateSTDIN
. - and
sort
to ... well, sort. - and
print
to print it.
Output:
1 a
1 b
1 c
2 e
3 d
Alternatively:
perl -e '%x=map {open($f,'<',$_); @f=<$f>; $_=>''.@f} @ARGV;print "$x{$_} $_n" for sort {$x{$a}<=>$x{$b}} keys %x;' ~location/folder/folder/*.log
add a comment |
How about:
perl -e 'print sort map {s/(d).*(w)..*//r} <>'
(Needs perl 5.14+ - this is probably installed on your system already)
Uses an inlined bit of perl code that:
- reads
<>
which is STDIN or files specified on command line. - Uses a substitution regex which captures a digit
(d)
and a letter just before a.
(w).
- then returns the transformed string.
r
regex flag. - uses
map
to iterateSTDIN
. - and
sort
to ... well, sort. - and
print
to print it.
Output:
1 a
1 b
1 c
2 e
3 d
Alternatively:
perl -e '%x=map {open($f,'<',$_); @f=<$f>; $_=>''.@f} @ARGV;print "$x{$_} $_n" for sort {$x{$a}<=>$x{$b}} keys %x;' ~location/folder/folder/*.log
How about:
perl -e 'print sort map {s/(d).*(w)..*//r} <>'
(Needs perl 5.14+ - this is probably installed on your system already)
Uses an inlined bit of perl code that:
- reads
<>
which is STDIN or files specified on command line. - Uses a substitution regex which captures a digit
(d)
and a letter just before a.
(w).
- then returns the transformed string.
r
regex flag. - uses
map
to iterateSTDIN
. - and
sort
to ... well, sort. - and
print
to print it.
Output:
1 a
1 b
1 c
2 e
3 d
Alternatively:
perl -e '%x=map {open($f,'<',$_); @f=<$f>; $_=>''.@f} @ARGV;print "$x{$_} $_n" for sort {$x{$a}<=>$x{$b}} keys %x;' ~location/folder/folder/*.log
edited Feb 4 '16 at 19:46
answered Feb 4 '16 at 19:31
SobriqueSobrique
3,819519
3,819519
add a comment |
add a comment |
wc … > ~/log.info | cut ~/log.info | …
The two sides of the pipe are executed in parallel. Unless wc
is especially quick to finish and cut
is especially slow to start, by the time cut
reads ~/log.info
, it's likely to be still empty, or even nonexistent. To compound the problem, the redirection on sort
is also executed in parallel and also truncates the file.
I don't understand what you're trying to do with this intermediate temporary file. It isn't useful:
wc -l ~location/folder/folder/*.log | cut -d "/" -f9 | sort > ~/log.info
It would be simpler to switch to the target directory:
{ cd ~location/folder/folder && wc -l -- *.log; } | sort > ~/log.info
add a comment |
wc … > ~/log.info | cut ~/log.info | …
The two sides of the pipe are executed in parallel. Unless wc
is especially quick to finish and cut
is especially slow to start, by the time cut
reads ~/log.info
, it's likely to be still empty, or even nonexistent. To compound the problem, the redirection on sort
is also executed in parallel and also truncates the file.
I don't understand what you're trying to do with this intermediate temporary file. It isn't useful:
wc -l ~location/folder/folder/*.log | cut -d "/" -f9 | sort > ~/log.info
It would be simpler to switch to the target directory:
{ cd ~location/folder/folder && wc -l -- *.log; } | sort > ~/log.info
add a comment |
wc … > ~/log.info | cut ~/log.info | …
The two sides of the pipe are executed in parallel. Unless wc
is especially quick to finish and cut
is especially slow to start, by the time cut
reads ~/log.info
, it's likely to be still empty, or even nonexistent. To compound the problem, the redirection on sort
is also executed in parallel and also truncates the file.
I don't understand what you're trying to do with this intermediate temporary file. It isn't useful:
wc -l ~location/folder/folder/*.log | cut -d "/" -f9 | sort > ~/log.info
It would be simpler to switch to the target directory:
{ cd ~location/folder/folder && wc -l -- *.log; } | sort > ~/log.info
wc … > ~/log.info | cut ~/log.info | …
The two sides of the pipe are executed in parallel. Unless wc
is especially quick to finish and cut
is especially slow to start, by the time cut
reads ~/log.info
, it's likely to be still empty, or even nonexistent. To compound the problem, the redirection on sort
is also executed in parallel and also truncates the file.
I don't understand what you're trying to do with this intermediate temporary file. It isn't useful:
wc -l ~location/folder/folder/*.log | cut -d "/" -f9 | sort > ~/log.info
It would be simpler to switch to the target directory:
{ cd ~location/folder/folder && wc -l -- *.log; } | sort > ~/log.info
answered Feb 6 '16 at 0:57
GillesGilles
537k12810861603
537k12810861603
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.
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%2f259918%2fhow-do-i-organize-and-cut-this-portion-of-a-directory-into-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