1

I am using xargs in conjunction with cut, but I am unsure how to get the output of cut to a variable that I can pipe to use for further processing.

So, I have a text file like so:

test.txt:

/some/path/to/dir,filename.jpg
/some/path/to/dir2,filename2.jpg
...

I do this:

cat test.txt | xargs -L1 | cut -d, -f 1,2
/some/path/to/dir,filename.jpg

but what Id like to do is:

cat test.txt | xargs -L1 | cut -d, -f 1,2 | echo $1 $2

where $1 and $2 are /some/path/to/dir and filename.jpg.

I am stumped that I cannot seem to able to achieve this.

What I want to achieve is:

read_somehow_the text_fields_from text_file |  ./mypgm -i $1 -o $2
Kusalananda
  • 333,661
JohnJ
  • 135

1 Answers1

2

GNU parallel has more options than xargs for reading columnar data; you could use

$ parallel --colsep , echo ./mypgm -i {1} -o {2} :::: test.txt
./mypgm -i /some/path/to/dir -o filename.jpg
./mypgm -i /some/path/to/dir2 -o filename2.jpg

Alternatively, using a shell loop:

$ while IFS=, read -r x y rest_if_any_ignored; do echo ./mypgm -i "$x" -o "$y"; done < test.txt
./mypgm -i /some/path/to/dir -o filename.jpg
./mypgm -i /some/path/to/dir2 -o filename2.jpg

Remove the echo once you are satisfied that it is doing the right thing.

steeldriver
  • 81,074
  • very interesting - because I was originally running parallel wih two max jobs using the --jobs param and I assumed using one job with parallel is impossible. thank you for the soluruon! – JohnJ Mar 15 '22 at 18:24