LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash script to filter a text file (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-to-filter-a-text-file-4175631866/)

bkelly 06-13-2018 11:17 AM

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.

scasey 06-13-2018 11:26 AM

grep, sed, and/or awk

bkelly 06-13-2018 11:50 AM

So far awk seems to be the best fit.
Thanks for the post.

pan64 06-13-2018 12:33 PM

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.

BW-userx 06-13-2018 12:50 PM

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


All times are GMT -5. The time now is 12:27 PM.