d-_-b
d-_-b

Reputation: 23141

concatenating string in bash

I'm having trouble concatenating this string together. My goal is to have /folder/p/t/e

test.txt contains the string "test".

cat test.txt|cd /folder/p/`awk '{print substr($,0,1)}'`/`awk '{print substr($0,1,1)}'`

it is outputting /folder/p/t/ so I think there is something wrong with the second substr part of it.

Could anyone help shed light on how I can do this?

Thanks!

Upvotes: 1

Views: 131

Answers (3)

Beta
Beta

Reputation: 99084

FOO=$(< test.txt)
cd /folder/p/${FOO:0:1}/${FOO:1:1}

Upvotes: 1

nemo
nemo

Reputation: 57599

You're assuming that your second call to awk will get something from test.txt, which it doesn't. The text from cat test.txt is piped to the command after the pipe and the command in the sub-shell (the first awk) receives all the input, leaving no input for the second awk, as kojiro already answered.

While merging both awk commands will fix the problem, is is not guaranteed that this will work in other shells. Because many people confuse bash with 'shell' in general I think it's noteworthy that a more portable solution would be the one made by Beta.

Upvotes: 0

kojiro
kojiro

Reputation: 77059

Your first awk instance is capturing all of stdin, so your second isn't reading anything in it. Whatever reads stdin must be a single command.

cat test.txt | cd /folder/p/`awk '{print substr($0,0,1)"/"substr($0,2,1)}'`

Upvotes: 3

Related Questions