1

I am trying to print all the lines from a file that has the same characters of the matching pattern. Here is my pattern -

CurrentPrincipal[MRC]
CurrentPrincipalLegalEventAssociation

In the file I have the lines like the below ones

823,agg.listgroup,CurrentPrincipal[MRC],CompanyElementDefinition
d4f170,atom.list,CurrentPrincipal[MRC][Type][*],CompanyElementDefinition
1097,agg.listgroup,CurrentPrincipalLegalEventAssociation,CompanyElementDefinition
c755ad,atom.list,CurrentPrincipal[LegalEventAssociation][Type][*],CompanyElementDefinition
8798c3,atom.list,CurrentPrincipal[MailingAddressStreetLine1][*],CompanyElementDefinition

I am iterating my pattern and printing its matching lines from the file. What I need is, when I iterate my pattern CurrentPrincipal[MRC] I should get only its matching line

d4f170,atom.list,CurrentPrincipal[MRC][Type][*],CompanyElementDefinition

and when the pattern is CurrentPrincipalLegalEventAssociation I should get only

c755ad,atom.list,CurrentPrincipal[LegalEventAssociation][Type][*],CompanyElementDefinition

My requirement is to ignore the [ ] from the line while matching pattern. I have tried my best to put my issue affront. Do let me know if anything else is needed from me. Thanks in advance.

  • 1
    I'm sorry but I don't understand what you need. Please edit your question and show us an example of your input file, including both desired and undesired lines, and then show us the output you would expect from that example. – terdon Jan 5 '18 at 12:16
2

You appear to want the [ and ] characters to be treated literally rather than as indicating a character range. You can do that by escaping them:

grep 'CurrentPrincipal\[LegalEventAssociation\]' file

Ex. given:

$ cat file
823,agg.listgroup,CurrentPrincipal[MRC],CompanyElementDefinition
d4f170,atom.list,CurrentPrincipal[MRC][Type][*],CompanyElementDefinition
1097,agg.listgroup,CurrentPrincipalLegalEventAssociation,CompanyElementDefinition
c755ad,atom.list,CurrentPrincipal[LegalEventAssociation][Type][*],CompanyElementDefinition
8798c3,atom.list,CurrentPrincipal[MailingAddressStreetLine1][*],CompanyElementDefinition

then

$ grep 'CurrentPrincipal\[LegalEventAssociation\]' file
c755ad,atom.list,CurrentPrincipal[LegalEventAssociation][Type][*],CompanyElementDefinition

Alternatively use the -F or --fixed-strings option to tell grep to treat all characters literally:

$ grep -F 'CurrentPrincipal[LegalEventAssociation]' file
c755ad,atom.list,CurrentPrincipal[LegalEventAssociation][Type][*],CompanyElementDefinition
  • This is fine but my search pattern is CurrentPrincipalLegalEventAssociation and I cannot modify it to include the escape sequence. All I want is like grep or awk command that will print the line c755ad,atom.list,CurrentPrincipal[LegalEventAssociation][Typ‌​e][*],CompanyElement‌ when the search pattern is CurrentPrincipalLegalEventAssociation. While searching the file with this pattern I should tell the command to ignore the brackets of that line. – Alex Raj Kaliamoorthy Jan 4 '18 at 14:19
  • 1
    @AlexRajKaliamoorthy please see my updated answer using grep -F. Also, it shouldn't be necessary to iterate over a file containing search patterns - there is ` -f` option (lower case f) to read patterns directly from a file. – steeldriver Jan 4 '18 at 14:27
  • Thanks for updating but the pattern what I have mentioned in my question does not have any brackets. It is just CurrentPrincipalLegalEventAssociation – Alex Raj Kaliamoorthy Jan 4 '18 at 14:35
  • 1
    @AlexRajKaliamoorthy, Do you have a policy against modifying them or is it a principal thing? Because it's not impossible to do so the only thing stopping you from doing it is yourself -- or possibly some weird company policy? You shouldn't approach automation with a "can't do" attitude. "Whether you think you can, or you think you can't...you're right." -Henry Ford – Jesse_b Jan 4 '18 at 15:10
  • 1
    Also if you are receiving bad input from some upstream application, really the correct thing to do would be to fix that application rather than massage it's output, but I digress. – Jesse_b Jan 4 '18 at 15:16
1

grep seems to do what you want.

grep 'word' filename

Will print every line in the file 'filename' that contains 'word'.

  • I have tried this already but if you see, my file has 1097,agg.listgroup,CurrentPrincipalLegalEventAssociation,CompanyElementDefinition c755ad,atom.list,CurrentPrincipal[LegalEventAssociation][Type][*],CompanyElementDefinition and I need only c755ad,atom.list,CurrentPrincipal[LegalEventAssociation][Type][*],CompanyElementDefinition . The line I need contains brackets and I want to ignore that while searching. – Alex Raj Kaliamoorthy Jan 4 '18 at 14:13
0

Using the following, the file output appears in next line in the file after pattern search line.

sed -n '/CurrentPrincipal\[MRC\]/p' filename | sed -n '2p'  

Output

d4f170,atom.list,CurrentPrincipal[MRC][Type][*],CompanyElementDefinition  

sed -n '/CurrentPrincipalLegalEventAssociation/,+1p' l.txt | sed -n '2p'

Output

c755ad,atom.list,CurrentPrincipal[LegalEventAssociation][Type][*],CompanyElementDefinition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.