I am using xargs
in conjuction with cut
but I am unsure how to get the output of cut
to a variable which 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..
CodePudding user response:
Try this:
cat test.txt | awk -F, '{print $1, $2}'
From man xargs
:
xargs [-L number] [utility [argument ...]]
-L number Call utility for every number non-empty lines read.
From man awk
:
Awk scans each input file for lines that match any of a set of patterns specified literally in prog or in one or more files specified as -f progfile.
So you don't have to use xargs -L1
as you don't pass the utility
to call.
Also from man awk
:
The -F fs option defines the input field separator to be the regular expression fs.
So awk -F,
can replace the cut -d,
part.
The fields are denoted $1, $2, ..., while $0 refers to the entire line.
So $1
is for the first column, $2
is for the second one.
An action is a sequence of statements. A statement can be one of the following:
print [ expression-list ] [ > expression ]
An empty expression-list stands for $0.
The
> file
or>> file
is present or on a pipe if| cmd
is present), separated by the current output field separator, and terminated by the output record separator.
Put all these together, cat test.txt | awk -F, '{print $1, $2}'
would achieve that you want.
CodePudding user response:
You may want to say something like:
#!/bin/bash
while IFS=, read -r f1 f2; do
echo ./mypgm -i "$f1" -o "$f2"
done < test.txt
IFS=, read -r f1 f2
reads a line fromtest.txt
one by one, splits the line on a comma, then assigns the variablesf1
andf2
to the fields.- The line
echo ..
is for the demonstration purpose. Replace the line with your desired command using$f1
and$f2
.