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.
I need to create a script to read the files in the specific folder, and looking into line by line, for example, a keyword: IP or a specific the IP 100.100.100.100 - not IPX is not valid, neither XIP - blank before and after is important to reduce the amount of records found. If I find it, I need to create a new file with the filename and the line where it was found the keyword.
Example:
Folder: /home/user1/tstfile
files in this folder:
filenumber1
filenumber2
filenumber3
supposing that the keyword was found in the filenumber1. I need to create a new file - for example: found.txt, where in this file we will be able to find it.
I don’t have much to show you. I didn’t find a command to list only the files, not the directories, besides this, I also don’t have enough knowledge.
#!/bin/bash
## I need to receive as parameter $1, the directory to read and check the files
## One idea is to save the list of the files
## The command below is not correct – I was not able to find a command to list only files
ls –la > `$(mktemp)`
filename=$mktemp
while IFS='' read -r line || [[ -n "$line" ]]; do
## I get the name of each file in the directory
## For each file, I need to read line by line as well and check the string
while IFS='' read -r line || [[ -n "$linetosearch" ]]; do
## The line below is not correct; it should have the consistence to find the strings:
## IP (blank before and after, 192.168.168.10 (specific IP), and other strings.
## If it is found, a new file would be saved with file and the linetosearch
$filename $linetosearch >> $2
## $2 the second parameter where there is the filename to be saved.
Perhaps grep can do what you want. Something like:
Code:
grep -r -d skip -n -e 'IP ' -e '192.168.168.10' *
will look in all files in the current directory and subdirectories for either the string 'IP ' or the string '192.168.168.10' and print the filename, linenumber and the actual line. Redirect the output of the command to a file by adding ' > output.txt' if it does what you want.
Assuming we are only reading the single level of the directory, create a for loop to read all objects in the directory and perform an if to test if object is a file, then simply grep the data you need.
Please remember to place code or data in [code][/code] tags
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.