Assigning variable to output gives error with expected result


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning variable to output gives error with expected result
# 1  
Old 10-10-2016
Assigning variable to output gives error with expected result

Hello,
I am trying to print out the first string matching query with grep and I need your help.

My scenario:

Database
Code:
John F 
4433 Street No 88 CA
Elisabeth Taylor
7733 Street No 26 ON
Jack Nicholson
0133 Green Park No 34 AR
John F 2
9399 Southpark No 02D UT

test.sh
Code:
#!/bin/bash
A="$(cat Database | grep "$1" -A1 | tail -n 1)"
printf "$A"

When I run
Code:
./test.sh John F

, it prints out the second John F.
How can I print the first one and how come can I make it searching accurately?

The most importan question is:
What would happen if I simultaneously and continuously run this script at the background with different search queries ( ten or twenty script.sh processes will be running I mean), do you think that $A will be confused and will give wrong answers?

I'd appreciate if you could give an answer to solve this or lead me to right direction.

Thanks in advance
Boris


Moderator's Comments:
Mod Comment Please use CODE tags correctly as required by forum rules!

Last edited by RudiC; 10-10-2016 at 05:57 PM.. Reason: Added CODE tags.
# 2  
Old 10-10-2016
The most important question first: ALL (interactive as well as background) processes run in their own environment and thus there's no danger the variables although commonly named will be confused.

For the other problem: in fact, grep matches and thus lists BOTH John Fs, but the tail -n1 command removes all but the very last line of grep's output.
You could use grep's -x (--line-regexp) option (if available) to eliminate the John F 2 line, but - it will fail unless you set $1 to "John F " as the file has a trailing space in this line...
Code:
set -- "John F"
grep -x "$1" -A1 file | tail -n 1
set -- "John F "
grep -x "$1" -A1 file | tail -n 1
4433 Street No 88 CA

This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-10-2016
Note also that when you invoke your script with:
Code:
./test.sh John F

$1 in your script expands to John; not John F. If you want your script to use John F as your search pattern, you need to call it with:
Code:
./test.sh "John F"

or:
Code:
./test.sh 'John F'

or:
Code:
./test.sh John\ F

# 4  
Old 10-10-2016
Hi,
In complement to say RudiC, you could use also option -m nth occurence (if available):
Code:
$ grep -A1 -m 1 "John F" file | tail -1
4433 Street No 88 CA
$ grep -A1 -m 2 "John F" file | tail -1
9399 Southpark No 02D UT

Beware: One line with 2 occurrences will count for 2 to -m option. So in this case, if you would second line with occurence you must set -m 3.

Regards.
# 5  
Old 10-10-2016
Thanks for your answers Rudic and Don,

In my understanding (--line-regexp) is not available in my computer as below ways are not giving an output:


Database:
Code:
"John F "
4433 Street No 88 CA
"Elisabeth Taylor"
7733 Street No 26 ON
Jack Nicholson
0133 Green Park No 34 AR
"John F 2"
9399 Southpark No 02D UT

test.sh

Code:
#!/bin/bash
A="$(cat Database | grep -x "$1" -A1 | tail -n 1)"
printf "$A"

Terminal:
Code:
./test.sh "John F"
./test.sh 'John F'
./test.sh John\ F

Only when I do not use -x I see output.
thanks
Boris


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-11-2016 at 04:12 AM.. Reason: Added CODE tags.
# 6  
Old 10-10-2016
I was responding to your 1st post in this thread (where there was no -x option in your grep command). It doesn't work with -x because of the trailing space in your file. If you want to exactly match a line that ends with a space you'll need to use something like:
Code:
./test.sh "John F "

to match either line,
Code:
./test.sh "John F $"

to match only the first line, or
Code:
./test.sh "John F 2"

to match only the last line.
This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 10-10-2016
Thanks Don,
The one with $ worked.


Thank You All
Boris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep output to file result not as expected

Hi Gurus, I run command grep ABC file1 > file2 against below file. I got all ABC_xxx in one line in file2. I expect to get multiple lines in file2. If I print result in screen, the result is expected. thanks in advance My os is SunOS 5.10 Generic_150400-64 sun4v sparc sun4v ABC_123 XXXXX... (2 Replies)
Discussion started by: green_k
2 Replies

2. Shell Programming and Scripting

Assigning basename result to another variable

This is a two part request for an assistance. I am not sure how retrieve value from basename command - line 270 -so in can be output as variable CLI_COMMAND - line 250 in whiptail input box. As coded I can input from keyboard ( stdin?) into input box using redirection. I can... (2 Replies)
Discussion started by: annacreek
2 Replies

3. Shell Programming and Scripting

Assigning bc output to a variable

I'm converting decimal to integer with bc, and I'd like to assign the integer output from bc to a variable 'val'. E.g. In the code below: If b is 5000.000, lines 6 and 8 will output: 5000 (5000.000+0.5)/1 | bc I'd like val to take the value 5000 though, rather than 5000.000 Does someone... (3 Replies)
Discussion started by: pina
3 Replies

4. Shell Programming and Scripting

Assigning output from awk to variable

I have a script whose contents are as below result= awk 's=100 END {print s }' echo "The result is" $result The desired output is The result is 100 My script is running without exiting and i am also not getting the desired output. Please help (5 Replies)
Discussion started by: bk_12345
5 Replies

5. Shell Programming and Scripting

Piping and assigning output to a variable in Perl

Hi All, I am trying to convert the below Csh code into Perl. But i have the following error. Can any expert help ? Error: ls: *tac: No such file or directory Csh set $ST_file = `ls -rt *$testid*st*|tail -1`; Perl my $ST_file = `ls -rt *$testid*st*|tail -1`; (10 Replies)
Discussion started by: Raynon
10 Replies

6. Shell Programming and Scripting

assigning SED output to a variable = trouble!

i'm on a Mac running BSD unix. i have a script in which i ask the user to input the name of a mounted volume. i then call SED to substitute backslashes and spaces in place of the spaces. that looks like this: echo "Enter the name of the volume" read Volume echo "You've chosen \"$Volume\""... (7 Replies)
Discussion started by: hungryd
7 Replies

7. Shell Programming and Scripting

Assigning output of a command to variable

When I run time -p <command>, it outputs: real X.XX user X.XX sys X.XXwhere X.XX is seconds. How I can take just that first number output, the seconds of real time, and assign that to a variable? (9 Replies)
Discussion started by: jeriryan87
9 Replies

8. Shell Programming and Scripting

Assigning output to a variable

I am new to unix shell scripting. I was trying to convert each lines in a file to upper case. I know how to convert the whole file. But here i have to do line by line. I am getting it in the below mentioned script #!/bin/bash #converting lower to upper in a file #tr "" "" <file1... (3 Replies)
Discussion started by: jpmena
3 Replies

9. Shell Programming and Scripting

Assigning output of command to a variable in shell

hi, I want to assign find command result into some temporary variable: jarPath= find /opt/lotus/notes/ -name $jarFile cho "the jar path $jarPath" where jarPath is temporary variable. Can anybody help on this. Thanks in advance ----Sankar (6 Replies)
Discussion started by: sankar reddy
6 Replies

10. Shell Programming and Scripting

Assigning output of command to a variable

Hi, I'm trying to assign the output of a command to a variable and then concat it with another string, however, it keeps overwriting the original string instead of adding on to the end of the string. Contents of test.txt --> This is a test var1="`head -n 1 test.txt`" echo $var1 (This is a... (5 Replies)
Discussion started by: oma04
5 Replies
Login or Register to Ask a Question