Having a little trouble with grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Having a little trouble with grep
# 1  
Old 11-03-2013
Having a little trouble with grep

I am trying to make a script that has grep finding lines that I get from the cat command, that start with something and end with an argument. I can get the first part, but whenever I try to add on the part that looks at the end of the line too, it stops working. Any ideas why? Here is my script:
Code:
#!/bin/csh
ls -l | grep ' ^[l]$argv[1]$ '

Thank you for any insight.
Edit: Using xtrace it says that variable name must start with a letter. I am guessing that the format in the $argv[1] is wrong, but I'm not sure. I've tried using quotes around it and single backquotes around it also.

Last edited by sammythesp3rmy; 11-03-2013 at 02:55 PM..
# 2  
Old 11-03-2013
Does it really have to be csh ?
# 3  
Old 11-03-2013
unfortunately it does
# 4  
Old 11-03-2013
Code:
ls -l | grep '^[l]'"$argv[1]"'$'

The shell sees 3 strings where the midle one is in " " i.e. allows variable substitution. grep sees one string without any quotes.
# 5  
Old 11-03-2013
I'm not sure I understand. I tried what you wrote too, and it still says the variable must start with a letter.
# 6  
Old 11-03-2013
Neither ls nor grep will complain about a variable name in this context. Please show us:
  1. the exact command line that you're using to invoke this script,
  2. the exact error message you're getting,
  3. the output from the command ls -l in the directory where you are running this script, and
  4. the output you were hoping to get out of the grep command.
# 7  
Old 11-03-2013
I am getting no error messages now, but I am not getting the results that should come with the script.

For example, my script name is softLinkFinder.txt
I type in softLinkFinder.txt softlink.txt for the command.
With my script, I am trying to get what soft links are to the softlink.txt file. I am trying to do this by creating the script that is using grep to find from ls -l what lines start with an l and end with softlink.txt
When I type in ls -l I get:
Code:
tdrwxr-xr-x 2 pallen user 3864 Nov  3 15:49 bin
drwxr-xr-x 2 pallen user 3864 Oct 28 12:36 cpfolder
-rwxrwxrwx 1 pallen user  47 Oct 30 13:18 files.txt
lrwxrwxrwx 1 pallen user  29 Nov  3 13:11 softlink1.txt -> /home/p/pallen/softlink.txt
lrwxrwxrwx 1 pallen user   29 Nov  3 13:13 softlink2.txt -> /home/p/pallen/softlink.txt
lrwxrwxrwx 1 pallen user   29 Nov  3 13:13 softlink3.txt -> /home/p/pallen/softlink.txt
lrwxrwxrwx 1 pallen user   29 Nov  3 13:13 softlink4.txt -> /home/p/pallen/softlink.txt
lrwxrwxrwx 1 pallen user   29 Nov  3 13:13 softlink5.txt -> /home/p/pallen/softlink.txt
-rw-r--r-- 1 pallen user   55 Nov  3 09:11 softlink.txt

my script is a variation of:
Code:
#!/bin/csh
if ($#argv != 1) then
 echo "You have the wrong number of arguments."
endif
ls -l | grep '^[l] "$argv[1]"$'

I haven't gotten my else set up yet, but when I run the script, grep finds nothing. I am wanting to find the lines that start with l and end with the name of the argument. I think that was all. Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trouble with tail and grep

Good Morning, i ran into some trouble this morning while 'improving' my monitoring stuff. i would like to get a warning when the number of mails sent (outbound) by postfix is above a certain number. so far, so easy. to test that i simply put cat /var/log/mail.info | grep 'to=<' | grep -v -e... (1 Reply)
Discussion started by: Mike
1 Replies

2. UNIX for Beginners Questions & Answers

Grep trouble in Bash

PH=(6H 0 0 JD 9S 0 KD 0) #input from .txt file In the above array, I am trying to get the computer to tell me at what indices the non-zeros are at. I don't understand why this doesn't work... grep -v -b "0" ph.txt > position.txt Isn't grep -v supposed to show non matches in Bash?... (2 Replies)
Discussion started by: cogiz
2 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. OS X (Apple)

Having trouble creating an alias for grep

Hi, I'm using Mac 10.9.1. I would like to create an alias for grep so that it won't print out messages like "grep: /Users/davea/workspace/myproject/subdir/: Is a directory" all the time. So in my terminal, I opened ~/.profile and entered alias grep='grep -s' However, when I close and... (5 Replies)
Discussion started by: laredotornado
5 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Having trouble with simple grep search

I have a text file (allWords.txt), that I would like to search through. Here is a snippet of what it looks like... a aah aahed aahing aahs aardvark aardvarks aardwolf ab abaci aback abacus abacuses abaft ...... I would like to use the grep search to search, line by line, for... (8 Replies)
Discussion started by: blackvelvet
8 Replies

6. Shell Programming and Scripting

Emergency grep/delete trouble!!!

Hi everyone, thank you so much for reading. I built a user reference page from /etc/password for a class project. Now I need two shell scripts: 1) Add names to the reference page 2) Delete names from the reference page. I know grep -v is involved somehow and some piping but I am super stuck. ... (1 Reply)
Discussion started by: jsmpdx
1 Replies

7. Shell Programming and Scripting

grep : having trouble with a single quote

Hi, I search for the string below which contains a single quote, some text '/home/myuser in the file myfile.txt as another user with the grep command as follows su - myuser -c "grep 'some text \'/home/myuser' myfile.txt" I also tried using two backslashes su - myuser... (6 Replies)
Discussion started by: cimcmahon
6 Replies

8. UNIX for Dummies Questions & Answers

X trouble

Hi there, I'm new to unix-environments. I'm richard, and i'm mostly a web-developer, under php. I've done work in unix env before, but never had my own. Today, I've got debian 3.1 r4 from the official site, and i've attempted to install it twice. I installed it initially as "Desktop... (0 Replies)
Discussion started by: izua
0 Replies

9. Solaris

Trouble with tr

I'm not sure where to post this but it's happening on a SunOS 5.8 server so I'll try here. I've discovered some unexpected behavior when using tr. For example: echo a | tr Z echo b | tr a echo a | tr B echo a | tr B echo a | tr A (8 Replies)
Discussion started by: Mike@Work
8 Replies

10. Shell Programming and Scripting

Trouble with grep again :(.. URGENT plss!

Frenz, I have a file with certain columns from ls -lR which looks something like: file stemp ---------- lrwxrwxrwx ./temp/s1 -> 2.ksh lrwxrwxrwx ./temp/s2 -> 3.ksh lrwxrwxrwx ./temp/subtemp/s3 -> 1.ksh lrwxrwxrwx ./temp/subtemp/s4 -> 2.ksh lrwxrwxrwx ./temp/s5 -> ... (9 Replies)
Discussion started by: manthasirisha
9 Replies
Login or Register to Ask a Question