Visit Our UNIX and Linux User Community


grep spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep spaces
# 1  
Old 08-31-2009
grep spaces

Hi all,
I'm writing a program that scans a file for a name and/or number the user enters ie:
$ sh phone.sh "Sally Turner"
or
$ sh phone.sh Bob 12345678

I've got it mostly working except when I use "" to try make a full name one argument ie "sally turner" it scans for a turner file in the current directory or when "Sally Turner" 12345678 is entered searches for Turner12345678. I think the "" are not removing the space making it read:
Code:
grep "Sally Turner" directory.txt

as
Code:
grep Sally Turner directory.txt

My question is does grep have an option that fixes this problem?
Here's the chunk of code with the problem:
Code:
        grep $1$2 directory.txt > /dev/null
        work=$?
        if [ $work -ne 1 ]
        then
        grep $1$2 directory.txt
        else
        echo "Adding new entry to directory..."
        $input >> directory.txt
        sleep 1
        echo "Entry added"

# 2  
Old 08-31-2009
For character or symbol searches you should preface the string with quotes inside the code and send a quoted input to ensure that the grep will work.

i.e. grep "$1" directory.txt > /dev/null

./phone.sh "search for this text"
# 3  
Old 08-31-2009
Quote:
Originally Posted by javajynx
Here's the chunk of code with the problem:
Code:
        grep $1$2 directory.txt > /dev/null


Code:
grep "$1 $2" directory.txt > /dev/null

Ot, if there could be one or two words:

Code:
grep "$1${2:+" $2"}" directory.txt > /dev/null

Or:

Code:
grep "$1 *$2" directory.txt > /dev/null

# 4  
Old 08-31-2009
YAY! it's finnaly all working correctly. I needed both "" around the single input variables and the 2 input variables so now it recognises input with "" as one input not two. Thankyou both very much for your help! Smilie

Previous Thread | Next Thread
Test Your Knowledge in Computers #201
Difficulty: Easy
Perl is implemented as a core interpreter, written in C, together with a large collection of modules, written in Perl and C.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep string causes extra spaces

Hello, I have an xml file and my aim is to grab each line in keywords file and search the string in another file. When keyword is found in xml file,I expect the script to go to previous line in the xml file and grab the string/value between two strings. It's almost working with an error. tab... (6 Replies)
Discussion started by: baris35
6 Replies

2. UNIX for Beginners Questions & Answers

Can I combine below mentioned grep commands using OR (when searching strings having spaces)

Command 1: $script | grep 'Write to ECC( SSID=MARGIN)' Command 2: $script | grep 'is not greater than existing logical processing' The above commands run my script and search the mentioned strings but I do not want to run my script twice. It is increasing run time. Can someone tell me... (3 Replies)
Discussion started by: Tanu
3 Replies

3. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

4. Shell Programming and Scripting

How to awk or grep the last column in file when date on column contains spaces?

Hi have a large spreadsheet which has 4 columns APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96085 Corp IT Desktop and Apps APM00111803814 server_2 96034 Storage Mgmt Team APM00111803814 server_2 96152 GWP... (6 Replies)
Discussion started by: kieranfoley
6 Replies

5. Shell Programming and Scripting

Grep words with spaces and save the output

I have a file that contains the schedule for a tournament with 41 teams. The team names have spaces in them. I would like to search for each teams schedule and then save that to that teams file For example Team name: "Team Two" I would like to search for all the games for "Team Two" and... (8 Replies)
Discussion started by: knijjar
8 Replies

6. Shell Programming and Scripting

grep on string separated by spaces

hi I am on AIX 5 and i have a script that runs the following command to list processes running. I then want to kill the returned processes. The PID are on field 2 separated by spaces. $ ps -ef|grep "rams.e $PORT" lesqa 1826998 2646248 0 11:20:35 pts/2 0:00 grep rams.e t24cm 2789380 ... (3 Replies)
Discussion started by: dustytina
3 Replies

7. UNIX for Dummies Questions & Answers

grep any number of spaces

which one of the following is the correct expression to ignore line with multiple spaces after any string cat file | grep -v "xyz *$" or cat file | grep -v "xyz*$" do i need "*" to specify the sapce or " *" will do? (2 Replies)
Discussion started by: manishma71
2 Replies

8. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

9. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies

Featured Tech Videos