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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Command to grep a word and print the whole line splitted into many
# 1  
Old 01-19-2012
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 "sendFeedback", it is giving the first line only.

The output is,
operationContext.sendFeedback(

But i need whole line till ";" i.e.

1.operationContext.sendFeedback( 2.FeedbackType.ERROR, null, "Input is empty.", "Input Details: pr
3.ovide Valid pair(s): "+pairType);

Please provide me a command to get the output as above.

Thanks and Regards,
Tulasi






I need the command
# 2  
Old 01-19-2012
grep's output is correct based on the regular expression...dont expect it to output multilines unless there are multiple "-e" switches on the command line or use another tool to extract that info...
# 3  
Old 01-19-2012
Code:
perl -0777 -ne '/[^\n]*sendFeedback.*?;/s && print "$&\n"' FILE

For multiple occurrences:
Code:
perl -0777 -ne 'print "$&\n\n" while /[^\n]*sendFeedback.*?;/gs' FILE

# 4  
Old 01-19-2012
Thanks for your Replies Yazu and Shamrock.
Yazu/Shamrock,
Actually my intention is, when we use grep command it will print Filename, line number and the whole line having the word it matches.

Along with that i want the next few lines which ends with ";" also.

For Ex: Suppose if am search for "showAndLogWarning" in all the files. my script is printing as below,

Script is :
Code:
#!/usr/bin/ksh
files=`find ./ -name "*.java"`
fileNames=`echo ${files}`
grep -i -n "logWarning" ${fileNames} > logMessages.txt

and the output is,
Code:
./DLSLogger.java:144:   public abstract void showAndLogWarning(OperationContext opContext, String message);
 ./MOTCM01450919/PlanetNeighborData.java:214:                            logging.showAndLogWarning(oprContext, "The LTE
SRCCELL" + nbrCellDn

When you see the above output, in line 214 it is incomplete when compared to line 144.

Please suggest me a command.

Thanks and Regards,
Tulasi Ram.Bora

Moderator's Comments:
Mod Comment How to use code tags when posting data and code samples.

Last edited by Franklin52; 01-19-2012 at 03:26 PM.. Reason: Please use code tags for data and code samples, thank you
# 5  
Old 01-20-2012
Code:
awk '/sendFeedback\(/,/\)/' infile

# 6  
Old 01-21-2012
Thanks rdcwayx,

The command is working. But as i said, the result should contain Filename and line number also.

Thanks and Regards,
Tulasi Ram.Bora
# 7  
Old 01-21-2012
Try:
Code:
awk '/sendFeedback/{print FILENAME":"NR": "$0}' RS=\; ORS=";\n" infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep command to search a regular expression in a line an only print the string after the match

Hello, one step in a shell script i am writing, involves Grep command to search a regular expression in a line an only print the string after the match an example line is below /logs/GRAS/LGT/applogs/lgt-2016-08-24/2016-08-24.8.log.zip:2016-08-24 19:12:48,602 ERROR... (9 Replies)
Discussion started by: Ramneekgupta91
9 Replies

2. Shell Programming and Scripting

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 grep -ohr "]*.xyz.com]*" $HOME/source/group/ | sort | uniq > $HOME/output.txt while read -r -u9 line; ... (4 Replies)
Discussion started by: raokl
4 Replies

3. Shell Programming and Scripting

Need to print the next word from the same line based on grep string condtion match.

I need to fetch particular string from log file based on grep condition match. Actual requirement is need to print the next word from the same line based on grep string condtion match. File :Java.lanag.xyz......File copied completed : abc.txt Ouput :abc.txt I have used below... (5 Replies)
Discussion started by: siva83
5 Replies

4. Shell Programming and Scripting

How to print few lines before and after matching word is found suing grep?

Hi, here are few lines present in the logs. I want to grep on Error and print few lines before and after Error word is found line1 Line2 Line3 Error Line4 Line5 Line6 Line7 I want the output to be Line2 Line3 Error Line5 (1 Reply)
Discussion started by: arghadeep adity
1 Replies

5. 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

6. UNIX for Dummies Questions & Answers

How to print line starts with specific word and contains specific word using sed?

Hi, I have gone through may posts and dint find exact solution for my requirement. I have file which consists below data and same file have lot of other data. <MAPPING DESCRIPTION ='' ISVALID ='YES' NAME='m_TASK_UPDATE' OBJECTVERSION ='1'> <MAPPING DESCRIPTION ='' ISVALID ='NO'... (11 Replies)
Discussion started by: tmalik79
11 Replies

7. Shell Programming and Scripting

How to print last word of line

Hi, How to print last word of line? #!/bin/bash x1="This is Kiran" echo "$x1" how to print "Kiran" in new variable.i.e x2=kiran (7 Replies)
Discussion started by: kiran_j
7 Replies

8. UNIX for Dummies Questions & Answers

Can grep command return word instead of complete line

Hi Is there any way GREP command can return word and not complete line. My file has following data: Hello Everyone I am NitinrajSrivastava Hi Friends Welcome VrajSrivastava I am using grep 'raj' which is returning me complete line.However I want only the word having keyword 'raj'. Required... (11 Replies)
Discussion started by: dashing201
11 Replies

9. 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

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