Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have this output as a result of comparing files with diff:

< IF-Name :STRING: "lns-wall-01-t2"       Index:Gge32: 260
---
> IF-Name :STRING: "lns-wall-01-t2"       Index:Gge32: 25

I need this output:

lns-wall-01-t2 old:260 new:25

I want to use sed.

share|improve this question

3 Answers 3

With awk:

awk '
/^</ { old = $NF }
/^>/ { str = $4 ; gsub(/"/,"",str) ; printf "%s old:%s new:%s\n", str, old, $NF }
' your_files_list_here
share|improve this answer
    
Thankyou for your response .I am trying to put this in script it gives error : Rep_Scripts='/home/scripts' for i in `cat $Rep_Scripts/diff.txt | awk ' /^</ { old = $NF } /^>/ { str = $4 ; gsub(/"/,"",str) ; printf "%s old:%s new:%s\n", str, old, $NF } ' done –  toto Apr 28 at 14:40
    
In your command line there are missing a few ; between the commands (assuming it's a on-liner as presented here), and a loop over a single file is unnecessary anyway. Moreover, awk can accept a list of files as arguments. - Instead of using a loop try this call: awk ' ... ' "$Rep_Scripts"/diff.txt, where the file argument is directly read by awk. In case you will have more than one file to process extend the argument list or use wildcards, as in: awk ' ... ' "$Rep_Scripts"/*.txt. –  Janis Apr 28 at 15:03
    
If you have issues with your code, please post the error message. –  Janis Apr 28 at 15:07
    
I try it..the script is not able to be executed and and no error on compiling so what could be the problem –  toto Apr 28 at 15:28
    
In case it's not clear, I've explicitly appended in my answer where the files list is to be placed. - Beyond that; what do you mean by "the script is not able to be executed"? - You certainly need not "compile" the awk command, as you wrote, just call it. –  Janis Apr 28 at 15:33
sed '
    /^</{
        # first line formatting
        s/^.*"\(.*\)".*: /\1 old:/
        # append next 2 lines
        N
        N
        # exchange from 2nd line begining till last ":" by "new"
        s/\n---.*: / new:/
        }' "$Rep_Scripts"/diff.txt
share|improve this answer
    
thankyou it's working –  toto Apr 28 at 15:43

use a different type of diff output:

diff -y --suppress-common-lines f1 f2

which gives you a more userfriendly format:

F-Name :STRING: "lns-wall-01-t2"       Index:Gge32: 260   |IF-Name :STRING: "lns-wall-01-t2"       Index:Gge32: 25

and

diff -y ........ |
perl -nE '/"(.*?)".*?32:\s*(\d+).*32:\s*(\d+)/ 
          and say "$1 old:$2 new:$3"'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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