Search code examples
bashsolariscut

Keep the delimiter in the output of cut


I have a script that has uses cut to pick some information out of a search that contains absolute paths. It looks something like:

PLACE=$(grep foo flatfile.txt | cut -d '/' -f 1-6)

The output looks like this:

machine1:/path/to/where/foo/is
machine2:/another/path/to/find/foo

I need it to look like this:

machine1:/path/to/where/foo/is/
machine2:/another/path/to/find/foo/

This needs to be printed to the console at the end of the script with echo "$PLACE" or something like that. The output will always be at least 2 lines, but usually more.

I tried about everything I can think of with echo, but it either shows no output at all or gives the output:

grep: '/' is a directory

I am running bash 3.00 on Solaris, if that helps any. I would really like to K.I.S.S. this by just having something tacked onto the end of the cut command, and not having to monkey with sed or awk. But, if that is the only way, so be it.


Solution

  • try this :

    PLACE=$(grep foo flatfile.txt | cut -d '/' -f 1-6 | xargs -I "%" echo %/)