Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
-1
votes
3
answers
120
views
zgrep - Find an IP address in 1200 *.gz files
I want to find out in which of my 1200 *.gz files a certain IP address occurs on a certain date (17.07.2025). I could not find the IP address in my current logs: grep 'IP address' *.logs My attempt to see if the IP even exists in the older, zipped log files returns: 'The argument list is too long'....
I want to find out in which of my 1200 *.gz files a certain IP address occurs on a certain date (17.07.2025).
I could not find the IP address in my current logs:
grep 'IP address' *.logs
My attempt to see if the IP even exists in the older, zipped log files returns: 'The argument list is too long'.
zgrep 'IP address' *
A combination of find and grep was also unsuccessful.
I am grateful for any assistance.
fl1tz
(1 rep)
Jul 21, 2025, 04:18 PM
• Last activity: Jul 25, 2025, 06:22 AM
1
votes
1
answers
78
views
Process some Folders and format the output
In Linux there are some Folders. I want to process each of this Folders with the program `myprogramm`. I get a list with `ls -1` or much better, sure only Folders with: `find . -maxdepth 1 -type d -printf '%f\n' | tr -d '.' | sed '/^[[:space:]]*$/d'` I Can pipe this to xargs. `xargs -I {} -n1 -P1 $(...
In Linux there are some Folders.
I want to process each of this Folders with the program
myprogramm
.
I get a list with ls -1
or much better, sure only Folders with:
find . -maxdepth 1 -type d -printf '%f\n' | tr -d '.' | sed '/^[[:space:]]*$/d'
I Can pipe this to xargs.
xargs -I {} -n1 -P1 $(cat $TEMPDIR)/myprogramm -switch1 -switch2 -someoptions {}/ -o {}.info
In one line :
find . -maxdepth 1 -type d -printf '%f\n' | tr -d '.' | sed '/^[[:space:]]*$/d' | xargs -I {} -n1 -P1 $(cat $TEMPDIR/pwd)/myprogramm -switch1 -switch2 -someoptions {}/ -o {}.info
That works so far.
But i am looking for a way to bring a little bit info into it.
Everytime if myprogramm
ends, it would be nice to have an output like: Folder 1 of 22 finished
Count how many Folders exist can i do with:
find . -maxdepth 1 -type d -printf '%f\n' | tr -d '.' | sed '/^[[:space:]]*$/d' | wc -l
But how to bring this into the line they process each Folders? And how to count after every one is finished one up?
Next thing, how to bring a random sleep value after every finished myprogramm
run and count them down before to work at the next Folder?
A random sleep can do with sleep $(shuf -i 30-60 -n 1)
.
But how to bring this into, and how to show and count them down?
Banana
(189 rep)
Jul 13, 2025, 02:26 AM
• Last activity: Jul 13, 2025, 04:47 PM
0
votes
3
answers
1773
views
One-liner to run commands from a file in parallel using xargs
I have a script like this: ```lang-shell #!/bin/csh command 1 \ -f \"input1\" \ -l input2 -other_swithes1 command 2 \ -f \"input1\" \ -m input2 \ -l input3 -other_swithes1 command 3 \ -f \"input1\" \ -l input2 -other_swithes2 ``` so any idea for an one-liner with `xargs` to run these commands in par...
I have a script like this:
-shell
#!/bin/csh
command 1 \
-f \"input1\" \
-l input2 -other_swithes1
command 2 \
-f \"input1\" \
-m input2 \
-l input3 -other_swithes1
command 3 \
-f \"input1\" \
-l input2 -other_swithes2
so any idea for an one-liner with xargs
to run these commands in parallel. I tried various variants but all failed. I do not really want to write a script, I think that should have been possible with -d switch and -c, not sure though.
To simplify and extend the problem further, what I have is
cat file | grep -v "#.*" | sed -z 's/[\]\n/ /g' | xargs -I {} -n1 -P10 sh -c '{}'
and while this does the job there is a particular problem and that is that \"
get removed. So any clue how to solve that?!
inman
(9 rep)
Aug 23, 2021, 12:31 PM
• Last activity: Jun 30, 2025, 01:48 PM
0
votes
1
answers
62
views
Run md5sum parallel with a Spinner or Progressbar
In Linux in a Folder there are some files (no Sub-Folders). I will create with `md5sum` MD5-Checksums from all these files and write it in `../md5sum`. The Files are stored on an SSD and the System has many Cores, so i will calculate several at the same time. That works with `find . -type f | xargs...
In Linux in a Folder there are some files (no Sub-Folders).
I will create with
md5sum
MD5-Checksums from all these files and write it in ../md5sum
.
The Files are stored on an SSD and the System has many Cores, so i will calculate several at the same time.
That works with
find . -type f | xargs -I {} -n1 -P16 /bin/sh -c '../md5sum {} >> ../md5sum.txt'
quite good.
Is there a way to make it a little bit visible that md5sum is still working?
Is there a way to add a simple Spinner - \ | /
with a Status like File 4 of 134 done
?
Or a Progress-Bar or a Status with like 714 of 101077 MB done
?
Ouput like this:
/ -- File 4 of 134 -- 714 of 101077 MB -- [#.........] -- 00,71 % done
will be very usefull, but i think that is hard to create?
Banana
(189 rep)
Jun 27, 2025, 12:15 AM
• Last activity: Jun 27, 2025, 04:07 AM
-1
votes
3
answers
4204
views
How to get the json data used in each curl statement while using xargs and maps it to its corresponding result?
I have text file that has arguments of curl command. This is how the file looks ``` 'https://example.com/tl/' -X POST -H 'Content-Type: application/json' --data-raw '{"email":"username2",}' 'https://example.com/tl/' -X POST -H 'Content-Type: application/json' --data-raw '{"email":"username3",}' 'htt...
I have text file that has arguments of curl command. This is how the file looks
'https://example.com/tl/ ' -X POST -H 'Content-Type: application/json' --data-raw '{"email":"username2",}'
'https://example.com/tl/ ' -X POST -H 'Content-Type: application/json' --data-raw '{"email":"username3",}'
'https://example.com/tl/ ' -X POST -H 'Content-Type: application/json' --data-raw '{"email":"username4",}'
'https://example.com/tl/ ' -X POST -H 'Content-Type: application/json' --data-raw '{"email":"username5",}'
'https://example.com/tl/ ' -X POST -H 'Content-Type: application/json' --data-raw '{"email":"username6",}'
This is the command I use
/AbsolutePath/Inputfile.txt | xargs -P 10000 -n 10 curl -s | jq '.message'
I'm using jq , to parse json in command line
What I want is,
1) Pipe or send output of above command to another command, so that if message
has certain text, grab the email **value** used in that corresponding curl command and write to a log file or create a filename with usernameX.txt
For example, only if username2 and username5 cURL command's message = 'success', these two usernames should be written to a log file or two files username2.txt and username5.txt should be created.
sofs1
(117 rep)
Jan 4, 2022, 11:26 AM
• Last activity: Jun 26, 2025, 01:07 AM
2
votes
3
answers
354
views
Create Subfolders for files and move them into, each for themselves
In Linux in a Folder there are these files: LQ0gfKQej7GKG44Cn0sSAC.part01.rar LQ0gfKQej7GKG44Cn0sSAC.part02.rar LQ0gfKQej7GKG44Cn0sSAC.part03.rar LQ0gfKQej7GKG44Cn0sSAC.part04.rar LQ0gfKQej7GKG44Cn0sSAC.part05.rar LQ0gfKQej7GKG44Cn0sSAC.part06.rar LQ0gfKQej7GKG44Cn0sSAC.part07.rar LQ0gfKQej7GKG44Cn0...
In Linux in a Folder there are these files:
LQ0gfKQej7GKG44Cn0sSAC.part01.rar
LQ0gfKQej7GKG44Cn0sSAC.part02.rar
LQ0gfKQej7GKG44Cn0sSAC.part03.rar
LQ0gfKQej7GKG44Cn0sSAC.part04.rar
LQ0gfKQej7GKG44Cn0sSAC.part05.rar
LQ0gfKQej7GKG44Cn0sSAC.part06.rar
LQ0gfKQej7GKG44Cn0sSAC.part07.rar
LQ0gfKQej7GKG44Cn0sSAC.part08.rar
LQ0gfKQej7GKG44Cn0sSAC.part09.rar
LQ0gfKQej7GKG44Cn0sSAC.part10.rar
no9e1hvqlVEbYHs9YU3.part1.rar
no9e1hvqlVEbYHs9YU3.part2.rar
NwgdNaort1EqT0ch.part1.rar
NwgdNaort1EqT0ch.part2.rar
NwgdNaort1EqT0ch.part3.rar
NwgdNaort1EqT0ch.part4.rar
VEwMzBPH91J.part1.rar
VEwMzBPH91J.part2.rar
I want to create a folder with the name
LQ0gfKQej7GKG44Cn0sSAC
In this Folder I want to move all LQ0gfKQej7GKG44Cn0sSAC.part...
files.
For no9e1hvqlVEbYHs9YU3
,NwgdNaort1EqT0ch
and VEwMzBPH91J
the same way too.
To get a list with the names I do this:
ls -1 * | cut -f1 -d "." | uniq > ../names
For creating the Subfolders I use:
cat ../names | xargs mkdir
I then tried then something like:
cat ../names | xargs -I '{}' mv '{}*' {}
But I think I am not on the right way....
It does not work.
Banana
(189 rep)
Jun 22, 2025, 12:12 AM
• Last activity: Jun 23, 2025, 09:53 AM
0
votes
2
answers
65
views
One-liner piping from find/xargs with paths including spaces
The following question likely does not relate specifically to Vim. I use a Vim example, as this is where I encounter the issue. Working on Ubuntu, I often open multiple files in Vim using tab pages: ``` $ vim --help | grep tab -p[N] Open N tab pages (default: one for each file) ``` I also use `find`...
The following question likely does not relate specifically to Vim. I use a Vim example, as this is where I encounter the issue.
Working on Ubuntu, I often open multiple files in Vim using tab pages:
$ vim --help | grep tab
-p[N] Open N tab pages (default: one for each file)
I also use find
with xargs
and grep -l
to obtain a list of files.
find . -type f -name "*.txt" | xargs grep -l "zod"
I can then quickly review the files output by find
in vim:
vim -p find . -type f -name "*.txt" | xargs grep -l "zod"
The earlier grep
command would fail if there are spaces in the files or directories, so -print0
can be added to the arguments to find
; and -0
can be added to the arguments to xargs
. The following creates a MWE set of sample files and directories including spaces:
echo zod > xx.txt && echo zod > 'x x.txt' && mkdir aa && echo zod > aa/xx.txt && echo zod > 'aa/x x.txt' && mkdir 'a a' && echo zod > 'a a/xx.txt' && echo zod > 'a a/x x.txt'
The following will then list the 6 text files we expect:
find . -type f -name "*.txt" -print0 | xargs -0 grep -l "zod"
But if I then try to pass the output of this command to vim tab pages (as below), the paths including spaces are split, and opened as 2 existent, and 9 non-existent files. Is there a way to get past the problem?
vim -p find . -type f -name "*.txt" -print0 | xargs -0 grep -l "zod"
I am keen to avoid side-effects such intermediate files or shell/environment variables (such as used in the top answer to a similar question [here](https://unix.stackexchange.com/a/597765)) ; and so I am looking specifically for a single-line command.
user7543
(274 rep)
May 21, 2025, 03:11 PM
• Last activity: May 22, 2025, 01:23 PM
7
votes
8
answers
18899
views
Remove numbers from the start of filenames
I've a problem modifying the files' names in my `Music/` directory. I have a list of names like these: $ ls 01 American Idiot.mp3 01 Articolo 31 - Domani Smetto.mp3 01 Bohemian rapsody.mp3 01 Eye of the Tiger.mp3 04 Halo.mp3 04 Indietro.mp3 04 You Can't Hurry Love.mp3 05 Beautiful girls.mp3 16 Apolo...
I've a problem modifying the files' names in my
Music/
directory.
I have a list of names like these:
$ ls
01 American Idiot.mp3
01 Articolo 31 - Domani Smetto.mp3
01 Bohemian rapsody.mp3
01 Eye of the Tiger.mp3
04 Halo.mp3
04 Indietro.mp3
04 You Can't Hurry Love.mp3
05 Beautiful girls.mp3
16 Apologize.mp3
16 Christmas Is All Around.mp3
Adam's song.mp3
A far l'amore comincia tu.mp3
All By My Self.MP3
Always.mp3
Angel.mp3
And similar and I would like to cut all the numbers in front of the filenames (not the 3 in the extension).
I've tried first to grep
only the files with the number with find -exec
or xargs
but even at this first step I had no success. After being able to grep
I'd like doing the actual name change.
This is what I tried by now:
ls > try-expression
grep -E '^[0-9]+' try-expression
and with the above I got the right result. Then I tried the next step:
ls | xargs -0 grep -E '^[0-9]+'
ls | xargs -d '\n' grep -E '^[0-9]+'
find . -name '[0-9]+' -exec grep -E '^[0-9]+' {} \;
ls | parallel bash -c "grep -E '^[0-9]+'" - {}
And similar but I got error like 'File name too long' or no output at all. I guess the problem is the way I'm using xargs
or find
as expressions in separate commands work well.
Thank you for your help
Luigi Tiburzi
(887 rep)
May 29, 2012, 08:07 AM
• Last activity: May 14, 2025, 11:19 AM
1
votes
2
answers
2997
views
Curl Parallel requests using links source file
I have this script to go through a list of URLs and the check return codes using Curl. Links file goes like this: https://link1/... https://link2/... https://link200/... (...) The script: INDEX=0 DIR="$(grep [WorkingDir file] | cut -d \" -f 2)" WORKDIR="${DIR}/base" ARQLINK="navbase.txt" for URL in...
I have this script to go through a list of URLs and the check return codes using Curl.
Links file goes like this:
https://link1/ ...
https://link2/ ...
https://link200/ ...
(...)
The script:
INDEX=0
DIR="$(grep [WorkingDir file] | cut -d \" -f 2)"
WORKDIR="${DIR}/base"
ARQLINK="navbase.txt"
for URL in $(cat $WORKDIR/$ARQLINK); do
INDEX=$((INDEX + 1))
HTTP_CODE=$(curl -m 5 -k -o /dev/null --silent --head --write-out '%{http_code}\n' $URL)
if [ $HTTP_CODE -eq 200 ]; then
printf "\n%.3d => OK! - $URL" $INDEX;
else
printf "\n\n%.3d => FAIL! - $URL\n" $INDEX;
fi
done
It takes a little while to run through every URL, so I was wondering how to speed those up curl requests.
Maybe I could use some parallel Curl requests, but using "xargs" inside a "for" loop while also printing a message doesn't seem the way to go.
I was able to use "xargs" out of the script and it sort of works, although not showing the correct HTTP code.
cat navbase.txt | xargs -I % -P 10 curl -m 5 -k -o /dev/null --silent --head --write-out '%{http_code}\n' %
I couldn't find a way to insert that into the script.
Any tips?
markfree
(425 rep)
Jan 4, 2022, 02:05 PM
• Last activity: Mar 19, 2025, 11:10 AM
9
votes
5
answers
1327
views
Run command on each line of CSV file, using fields in different places of the command
I have a CSV file and want to run a command for each line, using the fields of the file as separate arguments. For example given the following file: ``` foo,42,red bar,13,blue baz,27,green ``` I want to run the following commands one after another (note that order of arguments in the command may dif...
I have a CSV file and want to run a command for each line, using the fields of the file as separate arguments.
For example given the following file:
foo,42,red
bar,13,blue
baz,27,green
I want to run the following commands one after another (note that order of arguments in the command may differ from the input file):
my_cmd --arg1 42 --arg2 foo --arg3 red
my_cmd --arg1 13 --arg2 bar --arg3 blue
my_cmd --arg1 27 --arg2 baz --arg3 green
What is the easiest way to achieve this? It seems like it might be possible with xargs but I couldn't figure out how exactly.
luator
(302 rep)
Feb 25, 2025, 01:38 PM
• Last activity: Feb 27, 2025, 10:34 AM
0
votes
1
answers
85
views
How to batch compress pdfs?
I'm looking to batch compress some pdfs, using the convert to/from ps trick outlined elsewhere. I have a function defined in an .zshrc file: ``` function pdfcompress() { for f in "$1"; do echo "compressing $1" pdf2ps "$f" "$f".ps ps2pdf -dPDFSETTINGS=/default "$f".ps "$f" rm "$f".ps done } ``` My un...
I'm looking to batch compress some pdfs, using the convert to/from ps trick outlined elsewhere. I have a function defined in an .zshrc file:
function pdfcompress() {
for f in "$1"; do
echo "compressing $1"
pdf2ps "$f" "$f".ps
ps2pdf -dPDFSETTINGS=/default "$f".ps "$f"
rm "$f".ps
done
}
My understanding is that I should be able to pass this a list of pdfs, and it compresses them. However, the following happens:
% ls | pdfcompress
compressing
Error: /undefinedfilename in --findlibfile--
Operand stack:
()
Execution stack:
%interp_exit .runexec2 --nostringval-- findlibfile --nostringval-- 2 %stopped_push --nostringval-- findlibfile
findlibfile false 1 %stopped_push findlibfile 1815 1 3 %oparray_pop findlibfile
Dictionary stack:
--dict:743/1123(ro)(G)-- --dict:0/20(G)-- --dict:85/200(L)--
Current allocation mode is local
GPL Ghostscript 10.04.0: Unrecoverable error, exit code 1
GPL Ghostscript 10.04.0: Device 'pdfwrite' requires an output file but no file was specified.
**** Unable to open the initial device, quitting.
It seems that none of the file names are getting passed, for some reason. I've also tried approaches with find . -iname "*.pdf" -exec pdfcompress
, and piping to xargs pdfcompress
, but neither of those work because the function isn't in the environment. Other times the placeholder variables seem to be interpreted as literal strings.
I've been trying to get this to work in some form for over an hour with no luck. Any tips?
I have a folder structure of pdfs that I would like to maintain, so a solution that works recursively within directories would be best, if possible.
Thank you!
sporkl
(103 rep)
Feb 21, 2025, 08:26 PM
• Last activity: Feb 22, 2025, 06:13 PM
0
votes
4
answers
622
views
Reversing order of pdf files to be processed
Context: Catalina = zsh (preferred) or 16.04 Ubuntu = bash A `qpdf` example indicates: # To merge (concatenate) all the pages of a list of PDF files and save the result as a new PDF: qpdf --empty --pages -- A set of .pdf files (with spaces in filenames), in a particular directory, are to be concaten...
Context: Catalina = zsh (preferred) or 16.04 Ubuntu = bash
A
qpdf
example indicates:
# To merge (concatenate) all the pages of a list of PDF files and save the result as a new PDF:
qpdf --empty --pages --
A set of .pdf files (with spaces in filenames), in a particular directory, are to be concatenated:
# Concatenate Drafts file to ../concatDrafts.pdf (76 pdf files)
# https://stackoverflow.com/a/53754681/4953146
qpdf --empty --pages *.pdf -- out.pdf
Although qpdf
command is concatenates .pdf files, the reverse order of the .pdf files is to be concatenated. The order of the files to be processed is returned by:
ls -r.pdf
To handle spaces in the .pdf file names: xargs research indicates the need for:
ls -r *.pdf | xargs -E '\n'
What is the the thought process to arrive at a command the pipes the output of ls into the qpdf
command?
gatorback
(1522 rep)
Nov 1, 2020, 05:22 PM
• Last activity: Feb 5, 2025, 02:55 PM
0
votes
2
answers
81
views
Using find with vim for file that contains whitespace
I have a file which contains whitespace in its name: `my file.txt`. If I want to find a file which has this name and pipe its output to a vim with `args`, I must use `--null|-0` in order to handle whitespace correctly: find . -iname "my*file.txt" | xargs --null vim The **problem is that the filename...
I have a file which contains whitespace in its name:
my file.txt
.
If I want to find a file which has this name and pipe its output to a vim with args
, I must use --null|-0
in order to handle whitespace correctly:
find . -iname "my*file.txt" | xargs --null vim
The **problem is that the filename that is passed to the command is my file.txt^@
.**
The ^@
is the [null character](https://en.wikipedia.org/wiki/Null_character#Representation) in vim.
**So how can I pass the my file.txt
file from find
or [fzf
](https://github.com/junegunn/fzf) to vim ?**
glacier
(391 rep)
Jan 19, 2025, 07:20 AM
• Last activity: Jan 21, 2025, 05:00 PM
11
votes
5
answers
20626
views
Properly escaping output from pipe in xargs
Example: % touch -- safe-name -name-with-dash-prefix "name with space" \ 'name-with-double-quote"' "name-with-single-quote'" \ 'name-with-backslash\' `xargs` can't seem to handle double quotes: % ls | xargs ls -l xargs: unmatched double quote; by default quotes are special to xargs unless you use th...
Example:
% touch -- safe-name -name-with-dash-prefix "name with space" \
'name-with-double-quote"' "name-with-single-quote'" \
'name-with-backslash\'
xargs
can't seem to handle double quotes:
% ls | xargs ls -l
xargs: unmatched double quote; by default quotes are special to xargs unless you use the -0 option
ls: invalid option -- 'e'
Try 'ls --help' for more information.
If we use the -0
option, it has trouble with name that has dash prefix:
% ls -- * | xargs -0 -- ls -l --
ls: invalid option -- 'e'
Try 'ls --help' for more information.
This is before using other potentially problematic characters like newline, control character, etc.
Gerry Lufwansa
(523 rep)
Aug 11, 2017, 12:17 PM
• Last activity: Dec 6, 2024, 11:00 AM
0
votes
3
answers
96
views
Expect script with Xargs and multiple parameters
I have an expect script that will SSH into a list of routers. The expect script works on a list of routers in a file and is called from a bash script with xargs. Bash : script get-rtr.sh #!/bin/bash /usr/bin/xargs -P 10 -L 1 -r ./get-rtr-expect.sh router_list_temp list=`cat router_list` for i in `ec...
I have an expect script that will SSH into a list of routers.
The expect script works on a list of routers in a file and is called from a bash script with xargs.
Bash : script get-rtr.sh
#!/bin/bash
/usr/bin/xargs -P 10 -L 1 -r ./get-rtr-expect.sh router_list_temp
list=
cat router_list
for i in echo $list
do
echo $i $foo $bar >> router_list_temp
done
/usr/bin/xargs -P 10 -L 1 -r ./get-rtr-expect.sh router_list_temp
I am looping thru the router list, and writing to a new file with the args added. Then feed the new list to the expect script, each line of the line is arg0 arg1 and arg 2.
Then wipe it clean at the end.
I don't like this at all. I don't want to create a temp file, but it seems to be working.
All better ideas are welcome.
If router_list_temp could be an array of routers with 2 args, rather than a file, that would be better .... it would all live in RAM.
but I don't know how to create and array or pass it to an expect script.
Roger McCarrick
(21 rep)
Nov 22, 2024, 06:20 PM
• Last activity: Nov 23, 2024, 10:07 AM
0
votes
2
answers
112
views
Run program only on matching lines
Let's say I have a program `blackbox`, and a file with the following contents: ```raw in this file this line contains =TAG= so does =TAG= this one as =TAG= does this other line this line does not nor does this line =TAG= here again gone again ``` **How do I run `blackbox` on only the lines containin...
Let's say I have a program
blackbox
, and a file with the following contents:
in this file
this line contains =TAG=
so does =TAG= this one
as =TAG= does this other line
this line does not
nor does this line
=TAG= here again
gone again
**How do I run blackbox
on only the lines containing =TAG=
?**
**Note 1:** One way is to use a while read
loop, [but this is considered bad practice](https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice) . So what is the canonical, right way to do this (if there is one)?
**Note 2:** Of course, if I were just editing text, a solution with AWK
or sed
would be appropriate—but blackbox
might have desired side effects. This question is for those situations where I need to *execute another process*.
**Note 3:** You might ask what happens if blackbox
is something like nl
or sort
—where running it on multiple lines together has a different outcome from running a new process on each line.
In that case, I want to be able to do it **each of these three ways**:
- **Block-wise:** replace each block of contiguous lines containing =TAG=
with the result of blackbox
on that block.
Expected output with blackbox
= nl
:
in this file
1 this line contains =TAG=
2 so does =TAG= this one
3 as =TAG= does this other line
this line does not
nor does this line
1 =TAG= here again
gone again
- **Line-wise:** replace each line containing =TAG=
with the result of blackbox
on that line.
Expected output with blackbox
= nl
:
in this file
1 this line contains =TAG=
1 so does =TAG= this one
1 as =TAG= does this other line
this line does not
nor does this line
1 =TAG= here again
gone again
- **Continuously:** send all the lines containing =TAG=
to a blackbox
process, and replace each block with the lines that would be newly printed before blackbox
received the next block.
Expected output with blackbox
= nl
:
in this file
1 this line contains =TAG=
2 so does =TAG= this one
3 as =TAG= does this other line
this line does not
nor does this line
4 =TAG= here again
gone again
(If we used sort
instead, all matching lines would end up sorted in the last matching block, because they wouldn't get printed until the end.)
I haven't found any questions on here asking about the general problem, but these are all special cases of this problem:
- https://unix.stackexchange.com/questions/786941/how-to-write-script-prefix-braces-with-backslashes/786944#786944 (line-wise)
- https://unix.stackexchange.com/questions/282605/edit-file-based-on-existence-of-a-string (line-wise)
- https://unix.stackexchange.com/questions/470965/conditionally-replace-lines-of-file1-with-the-corresponding-lines-of-file2 (line-wise)
- https://unix.stackexchange.com/questions/443320/possible-to-speed-this-while-read-bash-script-up/443352 (line-wise)
- https://unix.stackexchange.com/questions/20322/replace-string-with-contents-of-a-file-using-sed (line-wise)
- https://unix.stackexchange.com/questions/776261/change-newline-with-space-on-certain-condition-with-sed/776262 (block-wise)
- https://unix.stackexchange.com/questions/315275/sorting-block-of-lines-matching-only-the-first-one (block-wise)
- https://unix.stackexchange.com/questions/747272/remove-lines-matching-pattern-plus-any-lines-following-it-matching-a-different (block-wise)
- https://unix.stackexchange.com/questions/316586/replace-lines-matching-a-pattern-with-lines-from-another-file-in-order (continuously)
- https://unix.stackexchange.com/questions/728389/something-like-paste-but-with-a-vertical-alignment-after-a-delimiter (continuously)
- https://unix.stackexchange.com/questions/317264/pass-multiple-command-line-arguments-to-an-executable-with-text-files/317277 (continuously)
- https://unix.stackexchange.com/questions/280641/delete-nth-line-from-each-line-matching-a-pattern (continuously)
wobtax
(1135 rep)
Nov 21, 2024, 09:36 PM
• Last activity: Nov 22, 2024, 05:01 PM
26
votes
2
answers
3948
views
find . -print0 | xargs -0 cmd vs find . -exec cmd {} +
- `find . -exec cmd {} +` - `find . -print0 | xargs -0 cmd` Both are meant to be reliable ways to run a command on the files found by find. Which is preferred? Which is more portable, reliable, efficient, versatile and why?
-
find . -exec cmd {} +
- find . -print0 | xargs -0 cmd
Both are meant to be reliable ways to run a command on the files found by find.
Which is preferred? Which is more portable, reliable, efficient, versatile and why?
Stéphane Chazelas
(579282 rep)
Jan 7, 2023, 10:55 AM
• Last activity: Nov 8, 2024, 04:09 PM
2
votes
1
answers
132
views
cp 'same file' error when using -L with find and xarg
I'm trying to replace symlinks in my current directory with the files they point to. I thought I understood 'cp' and the '-L' argument, but apparently not. Here's the command I am using find ./ -maxdepth 1 -type l -regex ".*\(pdf\|odp\)" | xargs readlink -f | xargs cp -Lf -t . Here's the error I get...
I'm trying to replace symlinks in my current directory with the files they point to. I thought I understood 'cp' and the '-L' argument, but apparently not.
Here's the command I am using
find ./ -maxdepth 1 -type l -regex ".*\(pdf\|odp\)" | xargs readlink -f | xargs cp -Lf -t .
Here's the error I get
cp: '/home/user/dir1/dir2/dir3/file.pdf' and './file.pdf' are the same file
What am I missing
mikemtnbikes
(241 rep)
Oct 16, 2024, 06:09 PM
• Last activity: Oct 17, 2024, 09:06 AM
19
votes
3
answers
25646
views
Why doesn't this xargs command work?
I wanted to delete all .sh extensions so did this: ls *.sh | xargs -I {} mv {} `basename {} .sh` However it doesn't work, it behaves like `basename` returns unchanged file name. Why is it behaving that way ? For instance, this works: ls *.sh | xargs -I {} echo `basename {}.jpg .jpg`; **EDIT**: Solut...
I wanted to delete all .sh extensions so did this:
ls *.sh | xargs -I {} mv {}
basename {} .sh
However it doesn't work, it behaves like basename
returns unchanged file name.
Why is it behaving that way ?
For instance, this works:
ls *.sh | xargs -I {} echo basename {}.jpg .jpg
;
**EDIT**:
Solution: single quote prevents ``basename ...
`` evaluation by the shell before the command is run.
ls *.sh | xargs -I {} sh -c 'mv {} basename {} .sh
'
majkinetor
(610 rep)
Feb 18, 2013, 06:37 PM
• Last activity: Oct 11, 2024, 01:03 PM
0
votes
2
answers
77
views
How to print the processed argument after each command invocation with xargs
i run this command: ls -1 | xargs -I% -n1 -P8 myprogramm -s1 -r5 -q -o %.dat % it works with 8 runs at one time, the `-q` switch in myprogramm for quit, that is ok so. but i will add something like this `; echo %` to become the output name at the screen if the programm run is finished, for each run....
i run this command:
ls -1 | xargs -I% -n1 -P8 myprogramm -s1 -r5 -q -o %.dat %
it works with 8 runs at one time, the
-q
switch in myprogramm for quit, that is ok so.
but i will add something like this ; echo %
to become the output name at the screen if the programm run is finished, for each run.
i run this command:
ls -1 | xargs -I% -n1 -P8 myprogramm -s1 -r5 -q -o %.dat % ; echo %
i see no errors, but the output is not that what i wanted.
i see after all runs are finished in bash only a new line with only one sign, %
but i will have the output like:
file1
file2
file3
...
%
= fileN is that with xargs run the jobs, how to become this output?
user447274
(539 rep)
Oct 10, 2024, 07:29 PM
• Last activity: Oct 10, 2024, 09:10 PM
Showing page 1 of 20 total questions