Concat string with variable after a 'grep'

1

Here is the structure of my file:

g-4.n.g.fr 10.147.243.63 g-4.n.g.fr-w1

Here is my script:

 #! /bin/sh
 ip=10.147.243.63
 worker=$(grep -e $ip $1 | awk '{ print $3; }')
 echo "[Hostname: ]"
 echo $worker
 echo "[Before concat]"
 echo $worker
 echo "[After concat]"
 echo "$worker.v.1"

I have this output:

[Hostname: ]
g-4.n.g.fr-w1
[Before concat]
g-4.n.g.fr-w1
[After concat]
.v.1n.g.fr-w1

I want to have .v.1 after g-4.n.g.fr-w1 without substitution, like this:

g-4.n.g.fr-w1.v.1

How can I modify my script to have this output?

researcher

Posted 2015-07-03T12:52:02.290

Reputation: 324

Answers

1

Strange results when playing with strings or parts thereof

First, test your line endings:

$ file test.txt
test.txt: ASCII text, with CRLF line terminators

If you get the above, and in fact don't want DOS line endings, you can use the dos2unix tool:

$ dos2unix test.txt
dos2unix: converting file test.txt to Unix format ...
$ file test.txt
test.txt: ASCII text

Okay, got my data in a consistent format

Your script should work more or less as intended (it does here):

[Hostname: ]
g-4.n.g.fr-w1
[Before concat]
g-4.n.g.fr-w1
[After concat]
g-4.n.g.fr-w1.v.1

if that doesn't work, try:

printf "%s.v.1" "$worker"

I would suggest modifying your script to make it clear that you are using grep to search for a string in the file, and removing plain echo $vars as per Bash pitfalls:

ip="10\.147\.243\.63"
worker=$(grep -e $ip $1 | awk '{ print $3; }')
printf "[Hostname: ]\n"
printf '%s\n' "$worker"
printf "[Before concat]\n"
printf '%s\n' "$worker"
printf "[After concat]\n"
printf '%s.v.1\n' "$worker"

bertieb

Posted 2015-07-03T12:52:02.290

Reputation: 6 181

Let us continue this discussion in chat.

– bertieb – 2015-07-03T14:40:38.690

Hi, please is there another way to do this treatement : worker=$(grep -e $ip $1 | awk '{ print $3; }') i'd like to avoid grep. Thanks you so much.Kind Regards. – researcher – 2015-07-04T10:15:53.647

it works now. i have converted the input file to UNIX format first.. – researcher – 2015-07-04T14:23:16.553

1My apologies, I was away for the weekend. Glad you got something sorted eventually; and my guess about encoding wasn't too far off the mark :) – bertieb – 2015-07-06T12:06:34.417

While quoting all shell variables, and using printf "%s" instead of echo (especially for unknown strings), are good ideas, they do not make sense as answers to this question.  Correcting/eliminating or otherwise dealing with the non-Unix line endings (i.e., CRLF) is the answer.  @researcher: If you figured that out by yourself, you should post an answer of your own explaining that, and then accept that answer.  While it is of course your decision which answer (if any) to accept, it is contrary to the intent of the checkmark to use it as a “thank you” for participation.  … (Cont’d) – G-Man Says 'Reinstate Monica' – 2015-08-18T15:23:00.960

(Cont’d) …  You really shouldn’t give it to somebody just because he spent time trying to help you, if he didn’t actually contribute to solving your problem.  And, if bertieb did, somehow, provide that solution (and I don’t see it on this page or in the chat), then it should be edited into his answer.  One of Super User’s goals (and of the Stack Exchange network in general) is to build a knowledgebase; a repository of questions and their answers.  It is of little benefit to future readers to “accept” an answer that does not answer the question (even if the answer is hidden in the comments). – G-Man Says 'Reinstate Monica' – 2015-08-18T15:23:30.577

@G-Man thanks for the rather long feedback, edited. – bertieb – 2015-08-18T15:36:44.453

Well, silver bullets are nice, but it would be better if you explained why CRLF line terminators produce the results that researcher was seeing. – G-Man Says 'Reinstate Monica' – 2015-08-18T15:40:47.893

@G-Man I'll try to get around to it, meantime feel free to edit it yourself.

– bertieb – 2015-08-18T15:42:54.637