LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2022, 04:21 AM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
recursive search for pattern and print out all text next to it


Hello everyone , i am trying to get a text after specific pattern using awk , however awk only gets the first match on each line and i want to do it recursively .

On this text
Quote:
The following signatures couldn't be verified because the public key is not available: NO_PUBKEY ED444FF07D8D0BF6
Reading package lists...
W: GPG error: http://ftp.de.debian.org/debian stretch Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 04EE7237B7D453EC NO_PUBKEY 648ACFD622F3D138 NO_PUBKEY 0E98404D386FA1D9 NO_PUBKEY EF0F382A1A7B6500
E: The repository 'http://ftp.de.debian.org/debian stretch Release' is not signed.
W: GPG error: http://kali.download/kali kali-rolling InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY ED444FF07D8D0BF6
E: The repository 'http://http.kali.org/kali kali-rolling InRelease' is not signed.
I want to get the expired pubkeys only , but using this code :

Code:
grep "NO_PUBKEY" myfile.log | awk -F'NO_PUBKEY' '{print $2}'
I am unable to get all the codes , only 3 because awk only search the first match on a line instead recursively .

Probably there is a more easy way to do this but i can not remember at this moment .

Any idea ?
 
Old 01-15-2022, 04:34 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,350
Blog Entries: 3

Rep: Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766
The -F addresses the Field Separator variable (FS), you may want to use the Record Separator (RS) variable instead:

Code:
awk '{print $1}' RS='NO_PUBKEY' myfile.log
That will print the fingerprints from the log file.
 
1 members found this post helpful.
Old 01-15-2022, 04:42 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,151

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Don't you just hate it when the computer does what you tell it, instead of what you want it to do ?.
 
Old 01-15-2022, 04:43 AM   #4
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Thank you for the tip , works perfectly .
 
Old 01-15-2022, 07:02 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Quote:
Originally Posted by pedropt
works perfectly
Really??
Code:
awk '{print $1}' RS='NO_PUBKEY' myfile.log
The
ED444FF07D8D0BF6
04EE7237B7D453EC
648ACFD622F3D138
0E98404D386FA1D9
EF0F382A1A7B6500
ED444FF07D8D0BF6
 
Old 01-15-2022, 07:40 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,350
Blog Entries: 3

Rep: Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766Reputation: 3766
Quote:
Originally Posted by pedropt View Post
Thank you for the tip , works perfectly .
Well about 86% or so, not quite perfectly.
 
Old 01-15-2022, 07:53 AM   #7
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
I guess this would do better
Code:
grep -Po 'NO_PUBKEY \K\w+' myfile.log
 
Old 01-15-2022, 11:23 AM   #8
mjolnir
Member
 
Registered: Apr 2003
Posts: 824

Rep: Reputation: 106Reputation: 106
Quote:
Originally Posted by shruggy View Post
I guess this would do better
Code:
grep -Po 'NO_PUBKEY \K\w+' myfile.log
Works 'perfectly' in my experiment but I wonder why the output is colored red in my terminal?
 
Old 01-15-2022, 12:08 PM   #9
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,035

Rep: Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344Reputation: 7344
Quote:
Originally Posted by mjolnir View Post
Works 'perfectly' in my experiment but I wonder why the output is colored red in my terminal?
because grep --color is on

Code:
awk 'BEGIN {RS="(NO_PUBKEY|\n)"}  NF==1 { print $1 }'
 
1 members found this post helpful.
Old 01-15-2022, 01:00 PM   #10
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Put spaces around NO_PUBKEY in RE to trim them from the output
Code:
awk NF==1 RS=' NO_PUBKEY |\n'

Last edited by shruggy; 01-15-2022 at 05:41 PM.
 
Old 01-15-2022, 02:36 PM   #11
mjolnir
Member
 
Registered: Apr 2003
Posts: 824

Rep: Reputation: 106Reputation: 106
Quote:
Originally Posted by pan64 View Post
because grep --color is on

Code:
awk 'BEGIN {RS="(NO_PUBKEY|\n)"}  NF==1 { print $1 }'
Yep, forgot about that - thanks.
Attached Thumbnails
Click image for larger version

Name:	myfile.png
Views:	10
Size:	84.6 KB
ID:	38127  
 
Old 01-15-2022, 03:03 PM   #12
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
The color settings affect only output to terminal, redirecting or piping the output of grep is safe regardless of the value of --color. This is also true for most (all?) GNU tools that understand --color
Code:
grep -Po 'NO_PUBKEY \K\w+' myfile.log|cat
 
Old 01-15-2022, 03:32 PM   #13
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,151

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Or simply escape the command "\grep ..."
 
Old 01-15-2022, 06:34 PM   #14
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,658

Rep: Reputation: 255Reputation: 255Reputation: 255
The most simple method:

Quote:
void listdir(const char *name, int indent)
{
DIR *dir;
struct dirent *entry;

if (!(dir = opendir(name)))
return;

while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
printf("%*s[%s]\n", indent, "", entry->d_name);
listdir(path, indent + 2);
} else {
printf("%*s- %s\n", indent, "", entry->d_name);
}
}
closedir(dir);
}

int main(void) {
listdir(".", 0);
return 0;
}
 
Old 01-15-2022, 10:17 PM   #15
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Quote:
Originally Posted by Xeratul View Post
The most simple method:
I think that perhaps you posted to the wrong thread? Your reply has no bearing on the question being asked as far as I can tell.
 
  


Reply

Tags
awk



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
vi search multiple pattern and remove only exact matching pattern amateurscripter Linux - Newbie 4 05-07-2018 01:19 PM
AWK - skip line if line contains pattern and print next line udiubu Linux - Newbie 15 09-06-2017 10:28 PM
Search and replace Pattern preceeding another pattern nbkisnz Programming 3 05-13-2012 01:50 PM
[SOLVED] /bin/bash if statement pattern search, end of pattern special character? headhunter_unit23 Programming 3 04-29-2010 08:05 AM
replace a text pattern with the reverse of another text pattern lothario Linux - Software 5 07-25-2008 02:43 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:54 AM.

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