LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 06-13-2018, 11:17 AM   #1
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 137

Rep: Reputation: 11
bash script to filter a text file


This is a follow up to my thread: controlling grep output to file. I am working on the reply to that thread.

I started getting long winded then decided to make this succinct. Given a file of the basic format


Quote:
Searching for a123.sh
/home/users/repo/another.sh
/home/users/repo/a123.sh
/home/users/repo/.git/index

Searching for a456.sh
Etc
In that file I want to keep the first two lines.
The third one is to be deleted because it is a self reference. When the line ends with the searched for string it is to be deleted.
The fourth is to be deleted because it is within a git directory. Anything containing “.git” is to be deleted.

When the phrase “Searching for” is found, the filtering begins anew. There is a blank line before each “Searching for” phrase if that matters.

The question is: What is the best utility to implement this? Just point me in the right direction and I will see what I can discover.

RESOLVED
After writing a few test scripts and spending some time with awk I have decided that filtering the text file with awk is the best way to go.
Thank you for taking the time to read this question and posting replies.

Last edited by bkelly; 06-19-2018 at 10:59 AM.
 
Old 06-13-2018, 11:26 AM   #2
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 1,633

Rep: Reputation: 534Reputation: 534Reputation: 534Reputation: 534Reputation: 534Reputation: 534
grep, sed, and/or awk
 
Old 06-13-2018, 11:50 AM   #3
bkelly
Member
 
Registered: Jan 2008
Distribution: Centos 7-4
Posts: 137

Original Poster
Rep: Reputation: 11
So far awk seems to be the best fit.
Thanks for the post.
 
Old 06-13-2018, 12:33 PM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 11,282

Rep: Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390Reputation: 3390
I suggest you to continue the original thread (opening new threads makes it hard to follow you and you will not get more help [ or attention ] - because some part of the information is not available). From the other hand it would be better to explain your problem better - to be able to give better help.
 
Old 06-13-2018, 12:50 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,639

Rep: Reputation: 1335Reputation: 1335Reputation: 1335Reputation: 1335Reputation: 1335Reputation: 1335Reputation: 1335Reputation: 1335Reputation: 1335Reputation: 1335
that all depends on where you put your vars to "print out" echo, as everything you need is within that script I wrote incorporating @keefaz stuff , and perhaps more.
it gives you the file it is looking for, the file it finds it in, and you have the file it is looking in to find the match. its all there.

https://www.linuxquestions.org/quest...95#post5867095

Code:
$ ./findscripts
1: Begin
1: Begin
2:Searching for file0.sh
3: grep found file0.sh in  /home/userx/testscripts/lookingfiles
4: on line 12
5 file holding call to script is:
/home/userx/searchscritps/file5
2:Searching for file0.sh
3: grep found file0.sh in  /home/userx/testscripts/lookingfiles
4: on line 1
5 file holding call to script is:
/home/userx/searchscritps/file4
1: Begin
2:Searching for file3.sh
3: grep found file3.sh in  /home/userx/testscripts/lookingfiles
4: on line 6
5 file holding call to script is:
/home/userx/searchscritps/file5
it just now depends on you formatting your output.
and swapping the line to this
Code:
while IFS=: read -r  number file other; do

Last edited by BW-userx; 06-13-2018 at 01:08 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] [Shell script] How to read, filter and split from text file Mr.Tee Linux - Newbie 4 01-01-2016 03:00 AM
Shell script for read user data with emptyLines in a text file and filter them srimal Linux - Newbie 7 11-01-2009 04:37 AM
Shell script to read lines in a text file and filter user data srimal Linux - Newbie 5 10-21-2009 07:41 AM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-10-2008 11:13 PM
how to read text file using bash script kkpal Linux - Newbie 4 03-12-2008 01:57 AM

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

All times are GMT -5. The time now is 09:39 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration