LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-29-2013, 03:47 PM   #1
sonia23
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Rep: Reputation: Disabled
Need lines displayed of the files containing a particular string


Hello,

I want to see all lines displayed containing a specific string in multiple files in multiple directory.

I have tried this command which helps me get the files which has a specific string, I also want the line to be displayed.(note trunk is a folder which has more folders and files within)

grep -r -l "inactiveConnectionTimeout" trunk/

or

find . -exec grep -l "inactiveConnectionTimeout" {} \;

To give more insight, I need to change value of inactiveConnectionTimeout from x to y value and so need this to check if I have really modified all files with the right new value.
 
Old 04-29-2013, 07:43 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
finding is easy enough, it would help to know what the file looks like, or specifically the line if changes are required.
 
Old 04-29-2013, 08:15 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
1. grep does display the whole of the line with the matched string
2. if you want all the lines set to a specific value, no need to check first, just used sed directly.
http://www.grymoire.com/Unix/Sed.html#uh-0
 
Old 04-29-2013, 10:37 PM   #4
sonia23
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
There are almost 100 files in various directories with the variable
inactiveConnectionTimeout.

inactiveConnectionTimeout=20
some files has value has 20, some might have some other number
I need to change it to 300 in all files. So I am planning to do it manaully since value after = is not same.

After changing all the files I need to verify if I have changed all the files right, so need to know a command which would display only lines containing inactiveConnectionTimeout.
 
Old 04-29-2013, 11:15 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Code:
# clear changed list file
>changed.list

# read filelist
while read file 
do
    # do replacement and make backup file just in case
    sed -i.bak 's/inactiveConnectionTimeout.*/inactiveConnectionTimeout=300/' $file

    # optionally, grep line out for later checking
    grep inactiveConnectionTimeout $file >>changed.list

done <filelist
Assumes files listed with full paths, one per line, in filelist file.
As above, no need to check existing value; if its already set, it just gets re-set ... same result.

Last edited by chrism01; 04-29-2013 at 11:17 PM.
 
1 members found this post helpful.
Old 04-30-2013, 12:19 PM   #6
sonia23
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks Chris.

I need one more help- if someone can guide me
I have this code where safile has the bunch of file locations

while read safile
do
grep inactiveConnectionTimeout $safile >>changed.list
done <dqa6202

The above code prints all the lines in those file with inactiveConnectionTimeout line. That variable needs to have 300 value, but there are some lines with some different numbers.

So to put it short, how do I get the filename from safile in changed.list

For e,g safile has following contents
a
b
c
d
....till z
currently I am getting changed.list as
inactiveConnectionTimeout="300"^M
inactiveConnectionTimeout="300"^M
inactiveConnectionTimeout="300"^M
inactiveConnectionTimeout="300"^M
inactiveConnectionTimeout="300"^M
inactiveConnectionTimeout="300"^M
inactiveConnectionTimeout="20"
spes.inactiveConnectionTimeout=900
inactiveConnectionTimeout="300"^M
inactiveConnectionTimeout="300"^M
inactiveConnectionTimeout="300"^M


But this deosnt helps me to find which file has wrong content like 20 above, what command will give me output like
a
inactiveConnectionTimeout="300"^M
b
inactiveConnectionTimeout="300"^M
c
inactiveConnectionTimeout="300"^M
d
inactiveConnectionTimeout="300"^M
d
inactiveConnectionTimeout="300"^M
e
inactiveConnectionTimeout="20"^M
f
inactiveConnectionTimeout="300"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Stripping all but lines matching string from text files d.vanheeckeren Linux - General 5 04-29-2013 08:00 AM
Shell script to find a string and print x lines before and y lines after the string igorza Linux - Newbie 6 04-18-2013 04:31 PM
[SOLVED] Need to remove dynamic string across multiple lines and files. vandigroup Linux - Server 2 05-14-2011 11:50 PM
number of Lines displayed in Terminal roadrash Linux - Newbie 1 07-03-2009 06:52 AM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration