Correct pipe useHow do I correct a directory incorrectly copied into itself?Trouble getting menu to workfind, xargs and mv: renaming files with double-quotes, expansion and bash precedence issueMerge and print matching and non matching values between a smaller file and a huge filelimit inotifywait output to n/secWait for stdout stream to finish and then add its contents to a fileHow to overwrite output file on every input updateadding filepath in the filesRedirection and piping for greppingPipe from a while loop to a command but execute another command if pipe command fails
How would I stat a creature to be immune to everything but the Magic Missile spell? (just for fun)
Which is the best way to check return result?
Why can't we play rap on piano?
What does “the session was packed” mean in this context?
Should I tell management that I intend to leave due to bad software development practices?
iPad being using in wall mount battery swollen
Why do bosons tend to occupy the same state?
What killed these X2 caps?
Why no variance term in Bayesian logistic regression?
How seriously should I take size and weight limits of hand luggage?
Can we compute the area of a quadrilateral with one right angle when we only know the lengths of any three sides?
Why are the 737's rear doors unusable in a water landing?
What mechanic is there to disable a threat instead of killing it?
What exploit Are these user agents trying to use?
Would Slavery Reparations be considered Bills of Attainder and hence Illegal?
Is it inappropriate for a student to attend their mentor's dissertation defense?
Is there an expression that means doing something right before you will need it rather than doing it in case you might need it?
Detention in 1997
Why didn't Miles's spider sense work before?
Am I breaking OOP practice with this architecture?
What is a romance in Latin?
Avoiding direct proof while writing proof by induction
Intersection Puzzle
Is it possible to create a QR code using text?
Correct pipe use
How do I correct a directory incorrectly copied into itself?Trouble getting menu to workfind, xargs and mv: renaming files with double-quotes, expansion and bash precedence issueMerge and print matching and non matching values between a smaller file and a huge filelimit inotifywait output to n/secWait for stdout stream to finish and then add its contents to a fileHow to overwrite output file on every input updateadding filepath in the filesRedirection and piping for greppingPipe from a while loop to a command but execute another command if pipe command fails
I am currently doing:
cat file1 file2 | sort -k1,1 > temp_file
command [options] -i temp_file > result_file
-i specifies the command input, in this case the file; I want a way to do this without explicitly specifying a temp_file.
cat file1 file2 | sort -k1,1 | command [options] -i > result_file
fails, complaining there's no file after the -i.
I also tried xargs but that led to execution on every line of content instead of on the file.
bash shell-script
New contributor
add a comment |
I am currently doing:
cat file1 file2 | sort -k1,1 > temp_file
command [options] -i temp_file > result_file
-i specifies the command input, in this case the file; I want a way to do this without explicitly specifying a temp_file.
cat file1 file2 | sort -k1,1 | command [options] -i > result_file
fails, complaining there's no file after the -i.
I also tried xargs but that led to execution on every line of content instead of on the file.
bash shell-script
New contributor
How can i know if it does or not?
– codeprimate123
2 days ago
1
Does your system provide either/dev/stdin
or/proc/self/fd/0
? If so you might be able to use them as the filename after the-i
, i.e.sort -k1,1 file1 file2 | command [options] -i /dev/stdin
(no cat needed). A big limitation of piped input is that the program can not use thelseek
orseek
calls on it so the program must be written to process the data sequentially or else provide its own buffering.
– icarus
2 days ago
add a comment |
I am currently doing:
cat file1 file2 | sort -k1,1 > temp_file
command [options] -i temp_file > result_file
-i specifies the command input, in this case the file; I want a way to do this without explicitly specifying a temp_file.
cat file1 file2 | sort -k1,1 | command [options] -i > result_file
fails, complaining there's no file after the -i.
I also tried xargs but that led to execution on every line of content instead of on the file.
bash shell-script
New contributor
I am currently doing:
cat file1 file2 | sort -k1,1 > temp_file
command [options] -i temp_file > result_file
-i specifies the command input, in this case the file; I want a way to do this without explicitly specifying a temp_file.
cat file1 file2 | sort -k1,1 | command [options] -i > result_file
fails, complaining there's no file after the -i.
I also tried xargs but that led to execution on every line of content instead of on the file.
bash shell-script
bash shell-script
New contributor
New contributor
edited 2 days ago
K7AAY
909928
909928
New contributor
asked 2 days ago
codeprimate123codeprimate123
31
31
New contributor
New contributor
How can i know if it does or not?
– codeprimate123
2 days ago
1
Does your system provide either/dev/stdin
or/proc/self/fd/0
? If so you might be able to use them as the filename after the-i
, i.e.sort -k1,1 file1 file2 | command [options] -i /dev/stdin
(no cat needed). A big limitation of piped input is that the program can not use thelseek
orseek
calls on it so the program must be written to process the data sequentially or else provide its own buffering.
– icarus
2 days ago
add a comment |
How can i know if it does or not?
– codeprimate123
2 days ago
1
Does your system provide either/dev/stdin
or/proc/self/fd/0
? If so you might be able to use them as the filename after the-i
, i.e.sort -k1,1 file1 file2 | command [options] -i /dev/stdin
(no cat needed). A big limitation of piped input is that the program can not use thelseek
orseek
calls on it so the program must be written to process the data sequentially or else provide its own buffering.
– icarus
2 days ago
How can i know if it does or not?
– codeprimate123
2 days ago
How can i know if it does or not?
– codeprimate123
2 days ago
1
1
Does your system provide either
/dev/stdin
or /proc/self/fd/0
? If so you might be able to use them as the filename after the -i
, i.e. sort -k1,1 file1 file2 | command [options] -i /dev/stdin
(no cat needed). A big limitation of piped input is that the program can not use the lseek
or seek
calls on it so the program must be written to process the data sequentially or else provide its own buffering.– icarus
2 days ago
Does your system provide either
/dev/stdin
or /proc/self/fd/0
? If so you might be able to use them as the filename after the -i
, i.e. sort -k1,1 file1 file2 | command [options] -i /dev/stdin
(no cat needed). A big limitation of piped input is that the program can not use the lseek
or seek
calls on it so the program must be written to process the data sequentially or else provide its own buffering.– icarus
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
Some applications understand -
there to mean stdin (check its man page), so
sort... | cmd -i - > result.txt
Most systems have a /dev/stdin
special file (same as /dev/fd/0
), which when open behaves like duplicating stdin, so:
sort... | cmd -i /dev/stdin > result.txt
With some shells (ksh, zsh, bash), you can also write it:
cmd -i <(sort...) > result.txt
While that would work, that file is a pipe so is not seekable. Some cmd
s may require a seekable file or a regular file and would fail if the file is a pipe. Then, you would have no other option than using a temporary file.
With zsh
, you can use the =(...)
form of process substitution:
cmd -i =(sort ...) > result.txt
for zsh
to create and populate and cleanup the temporary file automatically.
Same with fish
, using psub
:
cmd -i (sort... | psub -f) > result.txt
With bash
, still using a /dev/fd/x
file but this time on a regular temporary file, deleted straight after it's opened for a more reliable clean-up:
tmp=$(mktemp) &&
rm -f -- "$file" &&
sort... >&3 3>&- 4<&- &&
cmd -i /dev/fd/4 3>&-
3> "$tmp" 4< "$tmp"
The first solution worked, using the - it processed the as it was a file. I will try and remember the others as well. Great answer, thank you!
– codeprimate123
2 days ago
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
);
);
codeprimate123 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f509879%2fcorrect-pipe-use%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Some applications understand -
there to mean stdin (check its man page), so
sort... | cmd -i - > result.txt
Most systems have a /dev/stdin
special file (same as /dev/fd/0
), which when open behaves like duplicating stdin, so:
sort... | cmd -i /dev/stdin > result.txt
With some shells (ksh, zsh, bash), you can also write it:
cmd -i <(sort...) > result.txt
While that would work, that file is a pipe so is not seekable. Some cmd
s may require a seekable file or a regular file and would fail if the file is a pipe. Then, you would have no other option than using a temporary file.
With zsh
, you can use the =(...)
form of process substitution:
cmd -i =(sort ...) > result.txt
for zsh
to create and populate and cleanup the temporary file automatically.
Same with fish
, using psub
:
cmd -i (sort... | psub -f) > result.txt
With bash
, still using a /dev/fd/x
file but this time on a regular temporary file, deleted straight after it's opened for a more reliable clean-up:
tmp=$(mktemp) &&
rm -f -- "$file" &&
sort... >&3 3>&- 4<&- &&
cmd -i /dev/fd/4 3>&-
3> "$tmp" 4< "$tmp"
The first solution worked, using the - it processed the as it was a file. I will try and remember the others as well. Great answer, thank you!
– codeprimate123
2 days ago
add a comment |
Some applications understand -
there to mean stdin (check its man page), so
sort... | cmd -i - > result.txt
Most systems have a /dev/stdin
special file (same as /dev/fd/0
), which when open behaves like duplicating stdin, so:
sort... | cmd -i /dev/stdin > result.txt
With some shells (ksh, zsh, bash), you can also write it:
cmd -i <(sort...) > result.txt
While that would work, that file is a pipe so is not seekable. Some cmd
s may require a seekable file or a regular file and would fail if the file is a pipe. Then, you would have no other option than using a temporary file.
With zsh
, you can use the =(...)
form of process substitution:
cmd -i =(sort ...) > result.txt
for zsh
to create and populate and cleanup the temporary file automatically.
Same with fish
, using psub
:
cmd -i (sort... | psub -f) > result.txt
With bash
, still using a /dev/fd/x
file but this time on a regular temporary file, deleted straight after it's opened for a more reliable clean-up:
tmp=$(mktemp) &&
rm -f -- "$file" &&
sort... >&3 3>&- 4<&- &&
cmd -i /dev/fd/4 3>&-
3> "$tmp" 4< "$tmp"
The first solution worked, using the - it processed the as it was a file. I will try and remember the others as well. Great answer, thank you!
– codeprimate123
2 days ago
add a comment |
Some applications understand -
there to mean stdin (check its man page), so
sort... | cmd -i - > result.txt
Most systems have a /dev/stdin
special file (same as /dev/fd/0
), which when open behaves like duplicating stdin, so:
sort... | cmd -i /dev/stdin > result.txt
With some shells (ksh, zsh, bash), you can also write it:
cmd -i <(sort...) > result.txt
While that would work, that file is a pipe so is not seekable. Some cmd
s may require a seekable file or a regular file and would fail if the file is a pipe. Then, you would have no other option than using a temporary file.
With zsh
, you can use the =(...)
form of process substitution:
cmd -i =(sort ...) > result.txt
for zsh
to create and populate and cleanup the temporary file automatically.
Same with fish
, using psub
:
cmd -i (sort... | psub -f) > result.txt
With bash
, still using a /dev/fd/x
file but this time on a regular temporary file, deleted straight after it's opened for a more reliable clean-up:
tmp=$(mktemp) &&
rm -f -- "$file" &&
sort... >&3 3>&- 4<&- &&
cmd -i /dev/fd/4 3>&-
3> "$tmp" 4< "$tmp"
Some applications understand -
there to mean stdin (check its man page), so
sort... | cmd -i - > result.txt
Most systems have a /dev/stdin
special file (same as /dev/fd/0
), which when open behaves like duplicating stdin, so:
sort... | cmd -i /dev/stdin > result.txt
With some shells (ksh, zsh, bash), you can also write it:
cmd -i <(sort...) > result.txt
While that would work, that file is a pipe so is not seekable. Some cmd
s may require a seekable file or a regular file and would fail if the file is a pipe. Then, you would have no other option than using a temporary file.
With zsh
, you can use the =(...)
form of process substitution:
cmd -i =(sort ...) > result.txt
for zsh
to create and populate and cleanup the temporary file automatically.
Same with fish
, using psub
:
cmd -i (sort... | psub -f) > result.txt
With bash
, still using a /dev/fd/x
file but this time on a regular temporary file, deleted straight after it's opened for a more reliable clean-up:
tmp=$(mktemp) &&
rm -f -- "$file" &&
sort... >&3 3>&- 4<&- &&
cmd -i /dev/fd/4 3>&-
3> "$tmp" 4< "$tmp"
edited 2 days ago
answered 2 days ago
Stéphane ChazelasStéphane Chazelas
312k57592948
312k57592948
The first solution worked, using the - it processed the as it was a file. I will try and remember the others as well. Great answer, thank you!
– codeprimate123
2 days ago
add a comment |
The first solution worked, using the - it processed the as it was a file. I will try and remember the others as well. Great answer, thank you!
– codeprimate123
2 days ago
The first solution worked, using the - it processed the as it was a file. I will try and remember the others as well. Great answer, thank you!
– codeprimate123
2 days ago
The first solution worked, using the - it processed the as it was a file. I will try and remember the others as well. Great answer, thank you!
– codeprimate123
2 days ago
add a comment |
codeprimate123 is a new contributor. Be nice, and check out our Code of Conduct.
codeprimate123 is a new contributor. Be nice, and check out our Code of Conduct.
codeprimate123 is a new contributor. Be nice, and check out our Code of Conduct.
codeprimate123 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f509879%2fcorrect-pipe-use%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
How can i know if it does or not?
– codeprimate123
2 days ago
1
Does your system provide either
/dev/stdin
or/proc/self/fd/0
? If so you might be able to use them as the filename after the-i
, i.e.sort -k1,1 file1 file2 | command [options] -i /dev/stdin
(no cat needed). A big limitation of piped input is that the program can not use thelseek
orseek
calls on it so the program must be written to process the data sequentially or else provide its own buffering.– icarus
2 days ago