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 > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding a block to the file ROOZ Shell Programming and Scripting 4 08-20-2008 04:34 PM
how to retreive a block of data from the file aoussenko Shell Programming and Scripting 4 06-26-2008 11:03 AM
Deleting text block in file andre123 Shell Programming and Scripting 1 02-27-2007 01:17 PM
Select last block from a file misenkiser Shell Programming and Scripting 9 10-11-2006 09:32 AM
extract block in file sskb UNIX for Dummies Questions & Answers 5 10-25-2001 10:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-09-2008
palsevlohit_123 palsevlohit_123 is offline
Registered User
  
 

Join Date: Aug 2008
Location: India-Chennai
Posts: 120
help to get the block from a file

Code:
<Connection^M
                        companyId="TCS"^M
                        connectionMode="client"^M
                        name="TCS_TEMP"^M
                        protocolVersion="4.2"^M
                        disableLogging="false"^M
</Connection>

<Connection^M
                        companyId="HCL"^M
                        connectionMode="client"^M
                        name="HCL_RSDC02"^M
                        protocolVersion="4.2"^M
                        disableLogging="false"^M
</Connection>

<Connection^M
                        companyId="SCB"^M
                        connectionMode="server"^M
                        name="SCB_ERSDC02"^M
                        protocolVersion="4.1"^M
                        disableLogging="false"^M
                        connectionIntervals="08:00:00,18:00:00"^M
                        connectAtStartup="true"^M

</Connection>
<Connection^M
                        companyId="SAT"^M
                        connectionMode="client"^M
                        name="SAT_RSDC02"^M
                        protocolVersion="4.0"^M
                        disableLogging="false"^M
</Connection>
I have the above content in one file. if i grep for name="SCB_ERSDC02" it should display the corresponding block from <Connection^M until </Connection>.
Any commands...?

Code:
output should be
 
<Connection^M
                        companyId="SCB"^M
                        connectionMode="server"^M
                        name="SCB_ERSDC02"^M
                        protocolVersion="4.1"^M
                        disableLogging="false"^M
                        connectionIntervals="08:00:00,18:00:00"^M
                        connectAtStartup="true"^M

</Connection>
  #2 (permalink)  
Old 10-09-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Use nawk or /usr/xpg4/bin/awk on Solaris.

Code:
awk '
/<Connection/{i=0}
{a[++i]=$0}
/SCB_ERSDC02/{f=1}
/<\/Connection>/ && f {for(j=1;j<=i;j++){print a[j]};exit}
' file
Regards
  #3 (permalink)  
Old 10-09-2008
palsevlohit_123 palsevlohit_123 is offline
Registered User
  
 

Join Date: Aug 2008
Location: India-Chennai
Posts: 120
hi,

Is there any way to pass "SCB_ERSDC02" as command-line arg.

B'cos when i pass its not giving any output.
  #4 (permalink)  
Old 10-09-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Save this script, make it executable with chmod:

Code:
#!bin/sh

awk -v var="$1" '
/<Connection/{i=0}
{a[++i]=$0}
$0 ~ var {f=1}
/<\/Connection>/ && f {for(j=1;j<=i;j++){print a[j]};exit}
' file
and call the script as follow:

Code:
./scriptname SCB_ERSDC02
Regards
  #5 (permalink)  
Old 10-09-2008
palsevlohit_123 palsevlohit_123 is offline
Registered User
  
 

Join Date: Aug 2008
Location: India-Chennai
Posts: 120
Very Thanks!
  #6 (permalink)  
Old 10-09-2008
palsevlohit_123 palsevlohit_123 is offline
Registered User
  
 

Join Date: Aug 2008
Location: India-Chennai
Posts: 120
Suppose If i want to search for name="HCL_RSDC02", how to achive this?
  #7 (permalink)  
Old 10-09-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Place the parameter within single quotes:

Code:
./scriptname 'name="SCB_ERSDC02"'
Regards
Sponsored Links
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 09:42 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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