Need lines displayed of the files containing a particular string
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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.
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
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.
# 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.
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.