help to get the block from a file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users help to get the block from a file
# 1  
Old 10-09-2008
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  
Old 10-09-2008
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  
Old 10-09-2008
hi,

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

B'cos when i pass its not giving any output.
# 4  
Old 10-09-2008
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  
Old 10-09-2008
Very Thanks!
# 6  
Old 10-09-2008
Suppose If i want to search for name="HCL_RSDC02", how to achive this?
# 7  
Old 10-09-2008
Place the parameter within single quotes:

Code:
./scriptname 'name="SCB_ERSDC02"'

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get values block by block in same file

I have a file say "SAMPLE.txt" with following content, P1 10,9:6/123456 P2 blah blah P1 10,9:5/98765 P2 blah blah P1 blah blah P2 I want a output file say "RESULT.txt" as, Value1:123456 Value2:98765 Value3:NULL (17 Replies)
Discussion started by: garvit184
17 Replies

2. Shell Programming and Scripting

Printing a block of lines from a file, if that block does not contain two patterns using sed

I want to process a file block by block using sed, and if that block does not contain two patterns, then that complete block has to be printed. See below for the example data. ................................server 1............................... running process 1 running... (8 Replies)
Discussion started by: Kesavan
8 Replies

3. UNIX for Advanced & Expert Users

Move a block of lines to file if string found in the block.

I have a "main" file which has blocks of data for each user defined by tags BEGIN and END. BEGIN ID_NUM:24879 USER:abc123 HOW:47M CMD1:xyz1 CMD2:arp2 STATE:active PROCESS:id60 END BEGIN ID_NUM:24880 USER:def123 HOW:4M CMD1:xyz1 CMD2:xyz2 STATE:running PROCESS:id64 END (7 Replies)
Discussion started by: grep_me
7 Replies

4. Shell Programming and Scripting

How to extract block from a file?

I have siebel log file as following EventContext ....... 123 ....... SELECT ... .. EventConext <---- Question 1 , I should get this line 345 ...... SELECT <----- Question 2 , print this line Test..... <----- Question 2 , print this line .... <----- Question 2 , print... (5 Replies)
Discussion started by: ran123
5 Replies

5. Shell Programming and Scripting

finding a block in a file and replace with another file block.

(1) Yes but how is this block different from the other 24? You will need this information in order to identify and replace this block correctly (out of the 25). Ans: The 1st line and last line of this block are unique from other block. The 1st line is “rem Subset Rows (&&tempName.*) and The... (1 Reply)
Discussion started by: Zaheer.mic
1 Replies

6. Shell Programming and Scripting

Delete block of text in one file based on list in another file

Hi all I currently use the following in shell. #!/bin/sh while read LINE do perl -i -ne "$/ = ''; print if !m'Using archive: ${LINE}'ms;" "datafile" done < "listfile" NOTE the single quote delimiters in the expression. It's highly likely the 'LINE' may very well have characters in it... (3 Replies)
Discussion started by: Festus Hagen
3 Replies

7. Shell Programming and Scripting

how to append a block of statements after another block in the file

Hi I need to append the following block of statements in the middle of the file: # openpipe tsdbdwn2 set -x exec >> /tmp/tsdbdwn2.fifo 2>&1 # This needs to be appended right after another block of statements: if test $# -eq 0 ;then echo "Safety check - do you really wish to run" $0 "... (5 Replies)
Discussion started by: aoussenko
5 Replies

8. Shell Programming and Scripting

Adding a block to the file

Hi guys, I am trying to add the content of fileB into fileA but after a specific block within FileA for instance i have a file that starts as follow: ------------------------------------------------------------------------------- -- Title : -- Project : ... (4 Replies)
Discussion started by: ROOZ
4 Replies

9. Shell Programming and Scripting

Select last block from a file

Hi, I have to always select last portion of a block identified by 2 fixed keywords which repeats in a file for eg START .... ... END START .... ... END START .... ... END (9 Replies)
Discussion started by: misenkiser
9 Replies

10. UNIX for Dummies Questions & Answers

extract block in file

I need to extract a particular block from a file whose locations are not known but the only identity is a word. For example in a file I have ABC asdklf asdfk FGH dfdfg asdlfk asdfl ... JHK (5 Replies)
Discussion started by: sskb
5 Replies
Login or Register to Ask a Question