Question grep and sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question grep and sed
# 1  
Old 01-11-2011
Question grep and sed

hi everybody

i have this script and it work's if i use a single variable in grep, but when i put $searchterm_ the script stops work

i have a problem too in sed, because i don't know how i can search and replace more than one item, in this case is >>> $searchterm/$replaceterm and $searchterm_/$replaceterm_ .... four variables

thanks Smilie

Code:
       
 for file in $(grep -l -R "$searchterm $searchterm_" $startdirectory )
          do
           sed "s/$searchterm/$replaceterm/ig" $file > /tmp/teste/tempfile.tmp
           mv /tmp/teste/tempfile.tmp $file
           echo "Modified: " $file # overwrite
        done

# 2  
Old 01-11-2011
Code:
egrep -l -R "$searchterm|$searchterm_" ...
sed "s/$searchterm/$replaceterm/ig;s/$searchterm_/$replaceterm_/ig" ...

This User Gave Thanks to Scott For This Post:
# 3  
Old 01-11-2011
works, thanks man

one more question please, and if i want identifie and replace all line where have one $searchterm, for exemple, i have a txt with two lines and the second have the variable searchitem and more words that isn't identified in variable, but all the content of that line must be replaced to $replaceterm

like a line where is present "@" and change all line to another mail for exemple
# 4  
Old 01-11-2011
If I understand you correctly:

Code:
sed "/$searchterm/s/.*/$searchterm/" ...

Although your second point suggests you maybe want:
Code:
sed "s/$searchterm.*/$searchterm_/"

where searchterm_ would replace searchterm?

Then again, you might just want to replace everything after @:
Code:
sed "s/@.*/$searchterm/"


Last edited by Scott; 01-11-2011 at 04:05 PM..
# 5  
Old 01-11-2011
your code works perfect, thanks one more time

Code:
sed "/$searchterm/s/.*/$replaceterm/ig;/$searchterm_/s/.*/$replaceterm_/ig" $file > /tmp/teste/tempfile.tmp

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep / awk /sed question

Hi All, I have a file with following sample data. I am also attaching the file. (Est) (Est) Jobs Sch Workstation Job Stream SchedTime State Pr Start Elapse # OK Lim POOL #ACR_BILLING 0005 08/15 ... (2 Replies)
Discussion started by: Kevin Tivoli
2 Replies

2. Shell Programming and Scripting

grep question

Hello, Is there a way in grep to remember patterns? For eg: int a,b,c,d,a; If a variable is declared twice, like in the previous example, I should be able to print only those lines. Is there a way to print only the lines where the variable name occurs more than once, using grep... (1 Reply)
Discussion started by: prasanna1157
1 Replies

3. UNIX for Dummies Questions & Answers

sed/grep string replace question

Hi all, I know this question has probably been answered before, but I am struggling with this problem, even after googling a million pages. In a file named rdmt.conf I need a single character replaced, the number in the line below CUR_OC4J_ID=1 It will always appear after... (3 Replies)
Discussion started by: Mike AAA
3 Replies

4. Shell Programming and Scripting

General question about the relationship between KSH and sed/grep/awk etc

Greetings all, Unix rookie here, just diving into ksh scripting for the first time. My question may seem confusing but please bear with me: If I'm understanding everything I'm reading properly, it seems like the ksh language itself doesn't have a lot of string manipulation functions of... (2 Replies)
Discussion started by: DalTXColtsFan
2 Replies

5. UNIX for Dummies Questions & Answers

grep question

how can you create a script that would show the words you grep for as so grep -i WORD file.name This is where the WORD is. ^^^^ the ^ should be the same number of letters and directly or nearly under the word in question. thanks (1 Reply)
Discussion started by: farmerjack86
1 Replies

6. Shell Programming and Scripting

Sed Question 1. (Don't quite know how to use sed! Thanks)

Write a sed script to extract the year, rank, and stock for the most recent 10 years available in the file top10_mktval.csv, and output in the following format: ------------------------------ YEAR |RANK| STOCK ------------------------------ 2007 | 1 | Exxon... (1 Reply)
Discussion started by: beibeiatNY
1 Replies

7. Shell Programming and Scripting

Question on Grep!

Hi all, I am relatively new to Unix... Pls help me with the following : is there an option ion grep to find the first and last occurence of a pattern in a file? if not how else can this be accomplished? Cheers, Bourne. (5 Replies)
Discussion started by: bourne
5 Replies

8. Shell Programming and Scripting

question on sed grep awk from variable

i am still confusing on how to use sed, grep and awk if the input is not a file but a variable. such as: a="hello world" b="how are you" c="best wish to you" d="222,333,444" what if i want to check which variable $a,$b,$c,$d have contain "you" what if i want to replace the word "you"... (9 Replies)
Discussion started by: 3Gmobile
9 Replies

9. Shell Programming and Scripting

sed / grep question

Hello, I am new to shell scripting. I have a input file: Few lines of the input file has the following. /a0012/abcd12/abcd12 /a0003/xyzab1/lmno123 /a0006/pqrst1/abcde12 In my output file, I only need to get anything that is between the first and second '/' of each line: exampple:... (6 Replies)
Discussion started by: hemangjani
6 Replies

10. Shell Programming and Scripting

grep & sed question

I'm trying to write a bash script to perform a tedious task, but I have no experience and hardly any knowledge so I've been having a rough time with it. I'm on Mac OS X, and I want a script to do the following: I have a directory that has about 200 sudirectories. In each of these directories,... (1 Reply)
Discussion started by: der Kopf
1 Replies
Login or Register to Ask a Question