Recursively grep for a pattern and print that whole word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Recursively grep for a pattern and print that whole word
# 1  
Old 05-12-2016
Recursively grep for a pattern and print that whole word

Hello Forum Members,

I am trying to write a script for a requirement where i have to recursively search for a pattern and replace it with the new string in run time from user inputs
Code:
 grep -ohr "[[:alpha:]]*.xyz.com[[:alpha:]]*" $HOME/source/group/ | sort | uniq  > $HOME/output.txt
  while read -r -u9 line;
  do
     echo "The URL that should have to be changed is $line ..., Please enter the new URL  "
     read $user_url
     echo " User Entered URL is $user_url ..."
     echo ""
     echo " Changing URL from $line to $user_url ..."
     grep -irwl '$line' $HOME/source/group/ | xargs sed -i 's/$line/$user_url/g'
  done 9< $HOME/output.txt;

but the issue with the above is

Code:
grep -ohr "[[:alpha:]]*.xyz.com[[:alpha:]]*" $HOME/source/group/ | sort | uniq  > $HOME/output.txt

this grep only prints for entries with *.xyz.com ( ex:abc.xyz.com) but i also have some entrites as dev-abc.xyz.com but it is getting them

Can some one please suggest me a better way to grep for all the entries




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

Last edited by RudiC; 05-12-2016 at 01:46 PM.. Reason: Added code tags
# 2  
Old 05-12-2016
What happens if you
- leave out the -o option
- add a - to the regex (like [-[:alpha:]])
# 3  
Old 05-12-2016
Hello RudiC,

Thank you for the response , As per your suggestion i have tried below

Code:
grep -hr "[-[:alpha:]]*.xyz.com[[:alpha:]]*" $HOME/source/group/ | sort | uniq  > $HOME/output.txt

I should have informed in my original posting my bad , but there are 100's of xml files inside group folder/subfolders so with the above command it prints the entire date in the tags of the XML file and not the actual URL that is required.

I have also tried below

Code:
grep -ohr "[-[:alpha:]]*.xyz.com[[:alpha:]]*" $HOME/source/group/ | sort | uniq  > $HOME/output.txt

the above nearly gets all the information that i need it prints

Code:
dev-abc.xyz.com
qa-def.xyz.com
rty.xyz.com

But the only issue is we have some URL's like dev1-tyu.xyz.com and qa1-opt.xyz.com , It does not get them properly, it gets them as
Code:
-tyu.xyz.com
-opt.xyz.com

can you please help in addressing the above as well.

Last edited by Don Cragun; 05-13-2016 at 01:30 AM.. Reason: Add CODE tags again.
# 4  
Old 05-13-2016
Code:
grep -ohr "[-[:alnum:]]*\.xyz\.com"

A . (dot) does match a literal dot, but also any character except the newline. If you want to match a literal dot you need to escape it.

Substituting [:alpha:] for [:alnum:] will include digits.

You do not need the last trailing [:alpha:]*

This is not, by any means, fool proofed, since you keep showing us different information on each post on what to match.
# 5  
Old 05-13-2016
With the -o option the trailing [:alpha:]* makes a difference:
it shows a hostname rty.xyz.comx in full length.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

2. Shell Programming and Scripting

Command to grep a word and print the whole line splitted into many

Hi, I need to search a word in the java file. Assume the line in the java file is, (the line was splitted into 3 lines) 1.operationContext.sendFeedback(2.FeedbackType.ERROR, null, "Input is empty.", "Input Details: pr 3.ovide Valid pair(s): "+pairType); When i grep for the word... (6 Replies)
Discussion started by: tulasiram
6 Replies

3. Shell Programming and Scripting

How ti Grep for a word and print the next word

Hi can we grep for a word and print the next word of the greped word? ex:- create or replace function function_name create function function_name we should search for word "function" and output next word "function_name" from both lines. (3 Replies)
Discussion started by: manasa_vs
3 Replies

4. Shell Programming and Scripting

print word after pattern match in two instances

i have a file like below. how can i printout the digits followed by the pattern -bwout and -bwin. say i run the script by entering line number 145 (the fourth line), then the o/p should be like 5000000 1024000 8 test1 -ipprot erp -ppsout 500 -ppsin 500 -bwout 300000 -bwin 300000 -statsdevice... (7 Replies)
Discussion started by: sb245
7 Replies

5. Shell Programming and Scripting

print next word after found pattern

Hi all, I'd like to print the next word after a found pattern. example text: word1 word2 word3 word4 pattern word5 pattern word1 word2 word3 word4 word1 word2 pattern word4 basiclly the word after pattern. Thanks (9 Replies)
Discussion started by: stinkefisch
9 Replies

6. Shell Programming and Scripting

Grep word between matched pattern

would like to print word between matched patterns using sed for example : create INDEX SCOTT.OR_PK ON table_name(....) would like to print between SCOTT. and ON which is OR_PK Please help me out Thanks (4 Replies)
Discussion started by: jhonnyrip
4 Replies

7. UNIX for Dummies Questions & Answers

grep only word matching the pattern

Hi gurus, A file contains many words in format "ABC.XXXX.XXXX.X.GET.LOG" (X->varying). Now my shell script want this list (only words in formatABC.XXXX.XXXX.X.GET.LOG ) to continue the process. Pls help me. Thanks, Poova. (8 Replies)
Discussion started by: poova
8 Replies

8. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

9. Shell Programming and Scripting

Search word in a line and print earlier pattern match

Hi All, I have almost 1000+ files and I want to search specific pattern. Looking forwarded your input. Search for: word1.word2 (Which procedure contain this word, I need procedure name in output. Expected output: procedure test1 procedure test2 procedure test3 procedure test4 ... (7 Replies)
Discussion started by: susau_79
7 Replies

10. Shell Programming and Scripting

print a line containing word in a column using grep

hi, how to print a row which contains a perticular word in its third column using grep, cut, or any thing else. thanks (2 Replies)
Discussion started by: useless79
2 Replies
Login or Register to Ask a Question