grep, sed in a shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep, sed in a shell script
# 1  
Old 04-06-2005
grep, sed in a shell script

Hi,

I have a problem with a simple script I am trying to write. I want a user to type grep, sed commands that are then stored in variables. Those variables are stored in a function, and the function is then called to execute the commands. The idea is that the user does it step by step.

script:-
Code:
#!/bin/bash
#Filename: variabletest : Author: L.Pearce

function tptfunction
{
 $tool $criteria $filename
}

echo
echo
echo "Type command in full:"
echo "1. Type the tool you are using."
      read tool
echo "You typed $tool"

echo "2. Type criteria you wish to search."
      read criteria
echo "You typed $criteria"

echo "3. Type the name of the file you are searching:"
         read filename
echo "$filename is what you typed."

             tptfunction

echo
echo

I type sed '/Pearce/p' NewFile

the error:-
Code:
sed: -e expression #1, char 1: Unknown command: `''

When I type this command, and other similar ones where quotes are used:-
Code:
sed -n '/Pearce/p' NewFile

ls | grep '^g'

I get similar errors, like command not found.
I have the saem problem when trying to pipe unix commands to grep or sed

Code:
who|grep ^l

I have used:-
Code:
grep -n Name NewFile

and this seems to work fine.

I am guessing the spacing is not an issue, moreover the quotes or syntax.

Can anyone help?



Trufla

Last edited by Trufla; 04-06-2005 at 06:27 AM..
# 2  
Old 04-06-2005
i just tested your script on a Solaris 8 box and it works fine with sed until you give " '/Pearce/p' " as an argument to the criteria prompt ... use "/Pearce/p" only without the single quotes ... also make sure you are using single quotes and not backticks --- backticks are with the tilde key while single quotes are with the double quotes key on both sun and pc keyboards i use ...
# 3  
Old 04-07-2005
I still have problems:
I typed:-
Code:
#no single quotes/backquotes or back ticks 
-n /Pearce/
#I also tried it without suppression command

I got an error:-
Code:
sed: -e expression #1, char 8: Missing command

I did a search for this type of error, but could not find any info I understand.

I do not know if this makes a difference but I am using a windows PC, and using SSh to connect to a remote unix box.

Would this make a difference?

Trufla
# 4  
Old 04-07-2005
sed needs to know what to do with "/Pearce/" ... try "/Pearce/p" ...

and no, it doesn't matter how you're getting to the unix box as you're not executing the script on your pc ... that only matters when you're starting a Xwindows application and you need to have a Xserver (i.e., exceed, cygwin, etc.) on your PC for it to work ...
# 5  
Old 04-07-2005
FANTASTIC!

Cheers!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Wget, grep, sort, sed in 1 command/script

Hi, I need to join these statements for efficiency, and without having to make a new directory for each batch. I'm annotating commands below. wget -q -r -l1 URL ^^ can't use -O - here and pipe | to grep because of -r grep -hrio "\b\+@\+\.\{2,4\}\+\b" * > first.txt ^^ Need to grep the output... (14 Replies)
Discussion started by: p1ne
14 Replies

2. Shell Programming and Scripting

Grep in Shell script

hi guys very new to this game so excuse my ignorance. I need to create a script that simply greps for a text string and then outputs a message depending on whether the text string is there or not. The script I have setup is below, but whenever I run it I get the following error: ... (5 Replies)
Discussion started by: ap2112
5 Replies

3. Shell Programming and Scripting

Shell Script with Grep

Hi guys - below is my script that is checking for current file, size and timestamp. However I added a "grep" feature in it (line in red), but not getting the desired result. I am trying to acheive in output: 1. Show me the file name, timestamp, size and grep'ed words It would be a... (2 Replies)
Discussion started by: DallasT
2 Replies

4. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

5. Shell Programming and Scripting

Shell script grep help

Hey there, newbie question : echo "::kmastat" | /usr/bin/mdb -k | grep Total | grep "kmem_*" Total 17326080 432853 0 Total 426508288 65458 0 Total 704757760 1572001732 0 Total ... (11 Replies)
Discussion started by: shriyer
11 Replies

6. Shell Programming and Scripting

How to call last 14 characters with grep/sed in shell script.

Hi. This is my first post on the forums. I am trying to write a script that will parse a folder of files "oneverylongfilenamexyz.pdf" and create a .dat file named "oneverylongfilenamexyz.dat" with the first line of each .dat file saying variable="xyz" where xyz is the last 14 characters of $i... (4 Replies)
Discussion started by: attonbitusira
4 Replies

7. Shell Programming and Scripting

assigning a multiline grep output which has been piped through sed to a shell variabl

Hi, I wish to format the output of a grep command in such a way that sed will be able to handle the newline characters held in the output. Since sed does not allow newlines to be contained in a replacement pattern, that means adding a backslash '\' character to the end of each line from... (8 Replies)
Discussion started by: timculhane
8 Replies

8. Shell Programming and Scripting

grep in Shell script

Hello I do want to write a script which will check any errors say "-error" in the log file then have to send email to the concern person . And the concern person will correct the error . Next time if the script runs eventhough the error has been corrected it will ... (1 Reply)
Discussion started by: Krishnaramjis
1 Replies

9. Shell Programming and Scripting

Need shell/sed script for grep+string replacement

Hi, Let me explain the situation. There are many files in a directory and its sub-directories that conatin the string pattern "pa". I want to replace all such instances with the pattern "pranavagarwal" doing a grep "pa" `ls` does give me all the instances of the occurence of that... (3 Replies)
Discussion started by: pranavagarwal
3 Replies

10. Shell Programming and Scripting

Using Grep in a Shell Script

Hi everyone, Im trying to write a Shell script that basically creates a set of files based on a file with many records. For example if a file called dummy has the following content: a.txt 1st line of a's text file 2nd line of a's text file 3rd line of a's text file b.txt 1st line of b's... (8 Replies)
Discussion started by: nbvcxzdz
8 Replies
Login or Register to Ask a Question