Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash printing lines if line before matches

I only need to print the line directly before the +- or — symbol in bash which contains the location of the change. how can I edit this text to do so?

Input:

--- /opt/   
 /opt/location1:
 /opt/location2:
 /opt/location3:
+-rw-r--r-- 1 ra 
--rwxr-xr-x 1 rt 
+-rwxr-xr-x 1 ft 
 /opt/location4:
 /opt/location5:
+-rw-r--r-- 1 dra 
--rwxr-xr-x 1 drt 
+-rwxr-xr-x 1 dft 

output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  /opt/location3:
+-rw-r--r-- 1 ra 
--rwxr-xr-x 1 rt 
+-rwxr-xr-x 1 ft 
 /opt/location5:
+-rw-r--r-- 1 dra 
--rwxr-xr-x 1 drt 
+-rwxr-xr-x 1 dft

>Solution :

You can probably just do grep -B1 '^[+-]-' input, but the output is not quite what you want. To get the exact output you demonstrate, try:

awk '/^[+-]-/ {if(p) print prev; p=0; print; next} { p=1; prev = $0}' input
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading