1

I want to create catalogs and in the same line I want to change path and save it in the output file I don't want to repeat code, so I tried:

cut -d ';' -f 1 tmp1 | tr '.' '/' | xargs -I %  bash -c 'mkdir -p %; sed 's/$/\/version/'% > output'

And I have a error:

sed: -e expression #1, char 6: unknown option to `s'

Do you know what I am doing wrong?

1

The error got generated because you are using many '. In particular s/$/\/version/ is outside any single quote.

sed is either working on a file either on the standard input. It cannot change a string passed as parameter. printf built-in command can be used to append /version.

Remarks:

  • there is one name extracted for each line read from the tmp1 file, so the names do not contain \n.
  • according to the use of cut and tr, the resulting directories will not contain any . nor ; char in their names.

What you are trying to achieve is this:

cut -d ';' -f 1 tmp1 | tr '.' '/' | xargs -I % bash -c 'mkdir -p "%" ; printf "%s/version/\n" "%"' > output

Note that special chars in names are going to break this above command-line: e.g. ' (single-quote).

cut and tr can be replaced with only one awk which with xargs -0 can be used to handle safely the names (separated with null char).

Give a try to this safe version:

awk -F\; '{gsub(/[.]/,"/",$1);printf("%s\0",$1)}' tmp1 | xargs -0 bash -c 'for f; do mkdir -p "${f}";printf "%s/version\n" "${f}";done' arg0dummy > output

awk script:

  • -F\; defines ; as field separator, so $1 is the first field (before ;)
  • gsub is used to replace . with /
  • printf is used to print the result and insert a final \0 (null) char.

xargs:

  • -0: Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes.

bash -c ...:

  • arg0dummy: it is not used but it is mandatory; see man bash.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.