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 03-26-2016, 05:07 PM   #1
brjohnsmith
LQ Newbie
 
Registered: Mar 2015
Posts: 8

Rep: Reputation: Disabled
Question Script - looking into files


Hi,

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.

filenumber1 120.222.110.20

or for other files

filenumber2 IP
filenumber3 192.168.168.0

and so on.

Does anyone can help me? tks.
 
Old 03-26-2016, 08:07 PM   #2
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 19,314
Blog Entries: 28

Rep: Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137Reputation: 6137
Never done it myself, but this thread from another forum might help get you started: http://stackoverflow.com/questions/3...tern-in-a-file
 
Old 03-27-2016, 03:13 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please show what you have attempted and where you are stuck? You may also wish to advise what language you are going to be writing this script in?
 
1 members found this post helpful.
Old 03-27-2016, 07:07 PM   #4
brjohnsmith
LQ Newbie
 
Registered: Mar 2015
Posts: 8

Original Poster
Rep: Reputation: Disabled
Question

Hi,

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.

Done < $line

done < $filename


Tks.
 
Old 03-27-2016, 08:33 PM   #5
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748Reputation: 2748
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.

Last edited by allend; 03-27-2016 at 08:34 PM.
 
Old 03-28-2016, 06:19 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
  


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
[SOLVED] Bash script to read through text files and find largest number in files pomico Programming 15 09-13-2012 01:07 PM
Creating a script to move or copy files into multiple directories below the files matthes138 Linux - Newbie 5 08-25-2009 04:57 PM
Need help with script to organise files into folders as part of DVD backup script jasybee2000 Linux - Newbie 5 06-15-2009 07:29 PM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM

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

All times are GMT -5. The time now is 10:29 PM.

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