The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find files which has more than one occurance of pattern Prahlad Shell Programming and Scripting 10 08-08-2008 12:16 PM
Count of matched pattern occurance palash2k UNIX for Dummies Questions & Answers 3 04-24-2008 03:33 PM
Pattern not found : AWK , help jkl_jkl Shell Programming and Scripting 7 03-12-2008 07:46 AM
getting the stanza names if the pattern found vijaya2006 Shell Programming and Scripting 4 09-25-2007 09:03 AM
finding duplicate files by size and finding pattern matching and its count jerome Sukumar Shell Programming and Scripting 2 12-01-2006 04:20 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-23-2008
rakeshou rakeshou is offline
Registered User
  
 

Join Date: May 2007
Posts: 75
Thumbs up Finding Last occurance of another pattern when a pattern is found.

Hi,

I have two files viz,

rak1:
$ cat rak1
Quote:
#this is test

[section1]
PMAN 2
ZMAN 4

[section2]
xxx 0
NOP 6

[section 3]
PMAN 1
ZMAN 8
rak2:

$ cat rak2
Quote:
#this is test

[section1]
PMAN 2
ZMAN 9

[section2]

xxx 0
NOTA 5
TOCN 8






[section 3]
PMAN 1
Code:
sdiff rak1 rak2
returns:

Quote:
$ sdiff rak1 rak2
#this is test #this is test

[section1] [section1]
PMAN 2 PMAN 2
ZMAN 4 | ZMAN 9

[section2] [section2]
>
xxx 0 xxx 0
NOP 6 | NOTA 5
> TOCN 8

>
>
>
>
>
[section 3] [section 3]
PMAN 1 PMAN 1
ZMAN 8 <
I want the lines that got modified, changed, or deleted preceding with the section they are in.

I have done this so far:

Quote:
$ sdiff rak1 rak2 | sed 's/ //g' | grep -n "." | egrep -e "\||<|>" | sed '/>$/d'
5:ZMAN 4 |ZMAN 9
10:NOP 6 |NOTA 5
11:>TOCN 8
20:ZMAN 8 <
but I dont know how to put section in front of the changed,added or deleted lines.

Also it would be appreciated if the output could be printed as

[section X] <valInFile1/valInFile2> <valInFile1/valInFile2>
and so on.



Please Help me out
  #2 (permalink)  
Old 07-23-2008
alex_5161 alex_5161 is offline
Registered User
  
 

Join Date: Jan 2007
Location: Detroit
Posts: 124
I do not see why you need the egrep. Use sed for that filtering - easy.
Now, using -n option you prevent sed from printing unnessesary line.
After that just print what you need: lines with section number and lines with changes:
Code:
> sdiff rak1 rak2 | grep -n "." | sed -n '/>$/d; /section/p; /[|<>]/p'
Ok, deleting still needed

The only not-nice, the sections with no changes will be in that printout

I could not get it by 'sed'
Easy with nawk:
Code:
>....|
nawk '{if ( ($0 !~ /section/) || (prev !~ /section/) ) print prev; prev=$0;}
        END{if ($0 !~ /section/) print $0;}'
Not clear why it has empty line in beginning and end; so, remove it by :
Code:
>...|nawk NF;
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:38 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0