2

The command I'm trying to run:

mdb-tables davidoff.mdb | xargs -I {} mdb-export davidoff.mdb {} > {}.csv

mdb-export takes two arguments, I'm trying to pipe the second one. And then I want each one to be written to a file.

This won't work. Even when I remove the > {}.csv there is still an error:

Error: Table {} does not exist in this database.

Shell is zsh

example output from first part:

% mdb-tables davidoff.mdb
anatomypiclink bugs bugseverity bugtype classifications Diseases docaccess docaccesstype docassignments docclassifications docdetails docnotes docpicturelink docqa docs doctype keypicturelink keywords links logons mediatype navdoclinks navimagelinks navstructures organs Paste Errors pictureclassifications pictures picturetype qub3_queries_que qub3_relations_rel qub3_settings_set quotes references sequencelinks sequences subDocType videos dictionarytable doclinks docstatus media docs_ExportErrors

As a test case, I was able to make this work: printf "1\n2\n3\n" | xargs -I touch {}

Why is it that I cannot use replstr as a second argument?

Raven
  • 151
  • xargs --verbose will show the command that's going to be executed after substitution. Can you check that and see how it differs from what you expect? – Haxiel Oct 28 '20 at 17:58
  • 1
    The > {}.csv will happen just once, outside xargs, and will redirect the output of all the mdb-export ... commands into a single file named exactly {}.csv. –  Oct 28 '20 at 18:15
  • Try mdb-tables davidoff.mdb | xargs -I {} echo sh -c 'mdb-export davidoff.mdb "$1" > "$1.csv"' sh-c {} (remove the echo if the commands look OK) –  Oct 28 '20 at 18:19
  • Note that -I implies -L, i.e. one line from standard input will be substituted to {}. Assuming mdb-tables and mdb-export are from this project, the first command seems to expect one table as the second argument while the second command seems to output several tables on the same line (though I may be wrong on this). – fra-san Oct 28 '20 at 18:25
  • @user414777, I get this error 'Error: Table {} does not exist in this database.', which means its not substituting correctly – Raven Oct 28 '20 at 20:23
  • 1
    Thanks for the update. From the manual, I read that "You must specify a delimiter (-d) if you intend on piping the output of mdb-tables to a program such as awk or cut." This may be one of those pipe aware programs that output one thing in a terminal and another thing in a pipe. – Quasímodo Oct 28 '20 at 21:45
  • 1
    What system is that? What did the echo-prepended, dry-run command from my comment print? –  Oct 29 '20 at 00:03
  • I don't know what you mean by 'system' exactly? The output was: 1" > "$1.csv"' sh-c {} sh -c mdb-export davidoff.mdb "$1" > "$1.csv" sh-c {} – Raven Oct 29 '20 at 00:08
  • 1
    I guess the question about your system meant "what operating system are you using?". E.g. a Linux distribution, macOS, the Windows Subsystem for Linux, ...? Also, which version of xargs are you using? – fra-san Oct 29 '20 at 10:22

1 Answers1

0

Some versions of xargs on some systems places a 255 byte limit on arguments; if this is the case, it should be mentioned in the man page in the section on the -I argument. When an argument is greater than this limit, the placeholder isn't replaced. mdb-tables is outputting the table name list as a single line of 500 bytes.

As Quasímodo mentions, you can use the -d "\n" argument to break the table names into separate lines; you can also use -1.

outis
  • 113