Concat String with variable after a 'grep' and awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concat String with variable after a 'grep' and awk
# 1  
Old 07-04-2015
Concat String with variable after a 'grep' and awk

Here is the structure of my file:
MyFile.txt
Code:
    g-4.n.g.fr 10.147.243.63 g-4.n.g.fr-w1

Here is my sript:

test.sh
Code:
 #! /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"

 echo "*****************************************************"
 ip=10.147.243.63
 worker="abcd"
 echo "[Hostname: ]"
 echo $worker
 echo "[Before concat]"
 echo $worker
 echo "[After concat]"
 echo "$worker.v.1"

./test.sh MyFile.txt
I have this output:
Code:
 [Hostname: ]
g-4.n.g.fr-w1
[Before concat]
g-4.n.g.fr-w1
[After concat]
.v.1n.g.fr-w1
*****************************************************
[Hostname: ]
abcd
[Before concat]
abcd
[After concat]
abcd.v.1

Please, why i have wrong concatenation in the first case ?
Code:
awk --version
GNU Awk 4.0.1

echo $SHELL
/bin/bash

Thank you so much for help.
Kind Reagrds.

Last edited by Scrutinizer; 07-04-2015 at 10:55 AM.. Reason: Additional code tags
# 2  
Old 07-04-2015
There are probably carriage return (windows) characters in your input file.
Convert your file to UNIX format first:

Code:
tr -d '\r' < MyFile.txt > MyNewFile.txt

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-04-2015
Really, thank you so much for help.
it works now.

Please, can you explain me more why i have to convert to UNIX format ? Is there a hidden characters ?


Kind regards.

Last edited by chercheur111; 07-04-2015 at 11:37 AM..
# 4  
Old 07-04-2015
You're welcome..

You need to convert to Unix format, because you are on a Unix/Linux OS. I doubt that it would work on that other machine if your would use the same input file there (with the carriage return characters).

The carriage return characters are usually there because a file originated on Windows platform and was transferred to Unix without properly converting first...
# 5  
Old 07-04-2015
You are right. it works on the other machine because i have done a copy/paste of the content of DetailsWorkers.txt and not scp the file.

But,

i haven't used windows platform at all. The file DetailsWorkers.txt is generated by a script shell on linux
i think that there is a hidden charactar in the file DetailsWorkers.txt!!! not ?
Bests
# 6  
Old 07-04-2015
Then have a look at that shell script and see if it explicitly inserts them into the output.
Or... if that shell script itself uses another input file that happens to contain carriage returns, then they may stem from there...
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 07-04-2015
Awk, seldom, would require the help of grep to filter, and this is not one
Code:
worker=$(grep -e $ip $1 | awk '{ print $3; }')

This saves piping into it.
Code:
worker=$(awk -v IP=$ip '$2 ~ IP {print $3}' $1)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep a sub-string from a string stored in a variable.

For example: I am grepping "Hello" from a file and there are 10 matches. So all ten lines with match will get stored into a variable($match). Now I want to ignore those lines which have "Hi" present in that. Currently I tried this: match = grep "Hello" file | grep -v "Hi" file But that's not... (2 Replies)
Discussion started by: pavan
2 Replies

2. Shell Programming and Scripting

Python concat list with a string variable

Hello! #!/usr/bin/env python hn = "simsa-svr" for container in containerslist: Name = container I want to print Name along with hn i,e simsa-svr. I tried like Name = container'-'.join(hn) did not work. Needed result: lilly2232321-simsa-svr (2 Replies)
Discussion started by: ashokvpp
2 Replies

3. Shell Programming and Scripting

Grep command to find string in Variable in file2

Hi , I am executing 2 queries and output is saved in file1.txt and file2.txt example of file1.txt Testing word Doc.docx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,, Testing Excel _.xlsx,/Lab/Development and... (3 Replies)
Discussion started by: Sunil Mathapati
3 Replies

4. UNIX for Dummies Questions & Answers

How to grep exact string with quotes and variable?

As the title says I'm running a korn script in attempts to find an exact match in named.conf finddomain.ksh #!/bin/ksh # echo "********** named.conf ************" file=/var/named/named.conf for domain in `cat $1` do grep -n '"\$domain "' $file done echo "********** thezah.inc... (1 Reply)
Discussion started by: djzah
1 Replies

5. Shell Programming and Scripting

bash: using a string variable in grep

Hi, I've been stuck for several days on this. Using grep on a command line, I can use quotes, eg... grep 'pattern of several words' filename I want to do this in my bash script. In my script I have captured the several command line arguments (eg arg1 arg2) into a variable: variable=$@ I... (2 Replies)
Discussion started by: adrian777uk
2 Replies

6. Shell Programming and Scripting

Awk Concat

Hi All this may be somewhere in internet , but couldnt find the it. i have file as abc01 2010-07-01 12:45:24 2010-07-01 12:54:35 abc02 2010-07-01 12:59:24 2010-07-01 01:05:13 abc03 . . . the output using awk should look like this abc01|2010-07-01 12:45:24|2010-07-01 12:54:35... (3 Replies)
Discussion started by: posner
3 Replies

7. UNIX for Dummies Questions & Answers

Using GREP to extract variable following a string

Hello, I'm running calculations and I need to extract a specific number from a output file. So far I've only been able to GREP entire lines containing the string: '1 F=' . I would like to go a step further and extract just the number following '1 F='. The entire line looks like: 1 F=... (10 Replies)
Discussion started by: modey3
10 Replies

8. UNIX for Dummies Questions & Answers

String concat that keeps quotes

Hi All, I hope you can help. i am concatenating String variables using the following method. command="$command$x" i have created a script which takes a set of args passed to the script using the $* #example script args=$* count=0 for x in $args do count=`expr $count + 1` ... (8 Replies)
Discussion started by: duke
8 Replies

9. Programming

simple question on string concat

This is a simple question... char *str = NULL; int num = 0; scanf ("%d", &num); str = ??? I want str to point to the string num.txt For e.g: If user enters num = 5, str should point to "5.txt" How do I do that ? Thanks (2 Replies)
Discussion started by: the_learner
2 Replies

10. Shell Programming and Scripting

concat string

hey, I want to concat whole bunch of strings together but somehow they don't turn out the way I want them to a="HELLO " b="WORLD " c=$a$b I was expecting c to be "HELLO WORLD " but it... (1 Reply)
Discussion started by: mpang_
1 Replies
Login or Register to Ask a Question