1

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!

1
  • must it be done in one line? Commented Mar 24, 2013 at 6:20

3 Answers 3

3

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)}'`
Sign up to request clarification or add additional context in comments.

3 Comments

Avoid useless cat ! Use: awk < text.txt ... instead of cat text.txt | awk ... !!
could (better) be written: cd $(awk < test.txt '{printf "/folder/p/%s", substr($0,0,1)"/"substr($0,2,1)}')
@F.Hauri actually, it could be better written with a mild adaptation of @Beta's parameter expansion. x=$(<test.txt); cd "/folder/p/${x:0:1}/${x:1:1}" But that's not what I was going for. I was trying to explain why OP's solution didn't work.
1
FOO=$(< test.txt)
cd /folder/p/${FOO:0:1}/${FOO:1:1}

Comments

0

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.

1 Comment

I know that it hasn't anything to do with cd being no builtin. I was mentioning it just to educate.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.