Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a specific line based on a pattern if found

Tags:

sed

awk

I am trying to gather the filenames from a very big file depending on if a particular user, in this case windowsdom\nasarchive is found.

I tried running sed -nr "/-{3,}/h; /Path\s*:/H; /windowsdom\\nasarchive\s+Allow\s+FullControl/{x;G;p}" logfilename but it does not bring anything.

-----------------------
Path: U:\Credit share\BI-WEEKLY CREDIT NOTES\2010\Credit status - April 21 - 2.doc


AccessToString : windowsdom\nasarchive Allow  FullControl
                 BUILTIN\Administrators Allow  FullControl
                 NT AUTHORITY\SYSTEM Allow  FullControl
                 BUILTIN\Users Allow  ReadAndExecute, Synchronize

-----------------------
Path: U:\Credit share\BI-WEEKLY CREDIT NOTES\2009\Credit status - Sept. 23 - 59.doc


AccessToString : windowsdom\acl_1 Allow  ReadAndExecute, Synchronize
                 windowsdom\acl_2 Allow  Modify, Synchronize
                 windowsdom\acl_3 Allow  ReadAndExecute, Synchronize
                 windowsdom\adm_server Allow  Modify, Synchronize
                 BUILTIN\Administrators Allow  FullControl


-----------------------
Path: U:\Credit share\BI-WEEKLY CREDIT NOTES\2010\Credit status - August 10 - 3.doc


AccessToString : windowsdom\nasarchive Allow  FullControl
                 BUILTIN\Administrators Allow  FullControl
                 NT AUTHORITY\SYSTEM Allow  FullControl
                 BUILTIN\Users Allow  ReadAndExecute, Synchronize



-----------------------

Expected result:

Path: U:\Credit share\BI-WEEKLY CREDIT NOTES\2010\Credit status - April 21 - 2.doc
Path: U:\Credit share\BI-WEEKLY CREDIT NOTES\2010\Credit status - August 10 - 3.doc 

Can someone think how to get the expected result?

like image 453
Eduardo Avatar asked Feb 15 '23 09:02

Eduardo


2 Answers

Edit: I know it's not the best idea to edit an accepted answer, but it was substantially inaccurate. It turns out that the hold space is retained between lines.

The main problem with your command is that you are using double quotes, so the escaped backslash is seen unescaped by sed. Change them to single quotes and it starts working:

$ sed -nr '/-{3,}/h; /Path\s*:/H; /windowsdom\\nasarchive\s+Allow\s+FullControl/{x;G;p}' file
-----------------------
Path: U:\Credit share\BI-WEEKLY CREDIT NOTES\2010\Credit status - April 21 - 2.doc
AccessToString : windowsdom\nasarchive Allow  FullControl
-----------------------
Path: U:\Credit share\BI-WEEKLY CREDIT NOTES\2010\Credit status - August 10 - 3.doc
AccessToString : windowsdom\nasarchive Allow  FullControl

Now, you can simplify it to match the desired output. What you'll eventually get is shown in protong's answer:

sed -rn '/^Path:/h;/windowsdom\\nasarchive\s+Allow\s+FullControl/{g;p}' file

POSIX alternative:

$ sed --posix -n '/^Path:/h;/windowsdom\\nasarchive[[:space:]]\{1,\}Allow[[:space:]]\{1,\}FullControl/{g;p}' log.txt
Path: U:\Credit share\BI-WEEKLY CREDIT NOTES\2010\Credit status - April 21 - 2.doc
Path: U:\Credit share\BI-WEEKLY CREDIT NOTES\2010\Credit status - August 10 - 3.doc
like image 167
Lev Levitsky Avatar answered Feb 23 '23 13:02

Lev Levitsky


This might work for you (GNU sed):

sed -rn '/^Path:/h;/windowsdom\\nasarchive\s+Allow\s+FullControl/{g;p}' file

This prints the last Path string when it encounters the required string.

like image 43
potong Avatar answered Feb 23 '23 13:02

potong