bashcsvxargs

Process tab-separated (tsv) file with xargs while preserving quotes


I have a tab-separated-file myfile.tsv of form:

"a"      "b"
"c"      "d"

How can I process it with xargs (i.e. what args I need to pass) to pass these fields as two arguments to an external program while preserving all characters, including double-quotes (as I don't want to mess-up escaping and this is crucial if values in the tsv are actually JSON-formatted) and splitting by tab into external command's arguments?

E.g. I'd like to have cat myfile.tsv | xargs ... python -c 'import sys;print(sys.argv[1:])' to print

['"a"', '"b"']
['"c"', '"d"']

Thanks!


Solution

  • You should use NULL chars as delimiters for xargs:

    tr '\t\n' '\0' < myfile.tsv | xargs -0 -L2 python -c 'import sys;print(sys.argv[1:])' 
    ['"a"', '"b"']
    ['"c"', '"d"']