putting strings together (variable plus pipe)

Viewed 64

The goal is to use a static variable and combine it with the output of a pipe. I am looking for an one line solution !

This is an complete overhaul of the written examples, to keep it more clean! The old post I have removed cause of to mutch noise.

link="http://example.com/"
echo "index.html" > test.file
echo "search.php" >> test.file
echo "login.js" >> test.file

cat test.file | awk 'BEGIN {var=ARGV[1];ARGV[1]=""} {print var, $0}' "$link"
http://example.com/ index.html
http://example.com/ search.php
http://example.com/ login.js

cat test.file | awk -v var="$link" '{print var, $0}'
http://example.com/ index.html
http://example.com/ search.php
http://example.com/ login.js

This looks like what I need, but without the nasty white space in it. I need that field separation gone! Option -F'' result in error message. Adding | tr or | sed to remove the while space seems like error correction to me.

The following solution works so far ...

for string in $(cat test.file)
do
    printf "${link}%s\n" "$string"
done

Ouptut:

http://example.com/index.html
http://example.com/search.php
http://example.com/login.js

In a loop I can combine all the output of a pipe with my static variable. But isn't there a better solution without using a loop ?

It seems approaching the pipe with printf doesn't work by default and I have to put xargs to it.

echo "World" | xargs printf 'Hello %s\n' "$1"
Hello
Hello World

Even then the output is doubled, but why ?

I am still looking for an one line solution !

1 Answers

You pipe something into printf, but printf does not process its standard input. You can easily verify this by doing a

echo x | printf foo%s bar

which just prints foobar, and ignores the x.

Related