using sed to conditionally extract stanzas of a file based on a search string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using sed to conditionally extract stanzas of a file based on a search string
# 1  
Old 11-25-2008
using sed to conditionally extract stanzas of a file based on a search string

Dear All,
I have a file with the syntax below (composed of several <log ..... </log> stanzas)
I need to search this file for a number e.g. 2348022225919, and if it is found in a stanza, copy the whole stanza/section (<log .... </log>) to another output file.

The numbers to search for are listed in an input file (easy enough to read that file in one line at a time), but I am having major problems trying to build a conditional instruction using sed to search the file (below) for a particular number and then copy its whole section if a match is found. Thanks.

<log logid="847b00c2190810312356170828/1/1/1">
<category>ProcEngineBL.Normal</category>
<operation>Get</operation>
<target>GsmFlex.Connection</target>
<instance>msisdn=2348022225919</instance>
<user>tester1</user>
<context>sog.Pro/context>
<status>SUCCESSFUL</status>
</log>
<log logid="827b00c43190810312356170828/1/2/1">
<category>ProcEngineBL.Normal</category>
<operation>Set</operation>
<target>GsmFlex.Connection</target>
<instance>msisdn=234802225919</instance>
<user>onwer1</user>
<context>sog.Pro/context>
<status>SUCCESSFUL</status>
</log>
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to search and replace string in column in file with command sed?

how to search and replace string in column in file with command sed or other search "INC0000003.in" and replace column 4 = "W" $ cat file.txt INC0000001.in|20150120|Y|N|N INC0000002.in|20150120|Y|N|N INC0000003.in|20150120|Y|N|N INC0000004.in|20150120|Y|N|Noutput... (4 Replies)
Discussion started by: ppmanja3
4 Replies

2. UNIX for Dummies Questions & Answers

Obtaining File information based on String Search

Is there a single Command in Unix to get the following Information when searching for files containing one or more strings in a Unix Directory (including sub directories within it) : 1) Complete filename ( path and filename) 2) Owner of the file 3) Size of the file 4) Last Modified date... (3 Replies)
Discussion started by: pchegoor
3 Replies

3. Shell Programming and Scripting

Search for string in a file, extract two another strings and concatenate to a variable

I have a file with <suit:run date="Trump Tue 06/19/2012 11:41 AM EDT" machine="garg-ln" build="19921" level="beta" release="6.1.5" os="Linux"> Need to find word "build" then extract build number, which is 19921 also release number, which is 6.1.5 then concatenate them to one variable as... (6 Replies)
Discussion started by: garg
6 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

bash script to find date based on search string for continuesly updating file

Hi All, I am very new to UNIX and I have tried this for a longtime now and unable to crack it.... There is a file that is continuously updating. I need to search for the string and find the date @ which it updated every day..... eg: String is "work started" The log entry is as below: ... (1 Reply)
Discussion started by: Nithz
1 Replies

6. Shell Programming and Scripting

Use grep sed or awk to extract string from log file and put into CSV

I'd like to copy strings from a log file and put them into a CSV. The strings could be on different line numbers, depending on size of log. Example Log File: File = foo.bat Date = 11/11/11 User = Foo Bar Size = 1024 ... CSV should look like: "foo.bat","11/11/11","Foo Bar","1024" (7 Replies)
Discussion started by: chipperuga
7 Replies

7. Shell Programming and Scripting

Extract string from multiple file based on line count number

Hi, I search all forum, but I can not find solutions of my problem :( I have multiple files (5000 files), inside there is this data : FILE 1: 1195.921 -898.995 0.750312E-02-0.497526E-02 0.195382E-05 0.609417E-05 -2021.287 1305.479-0.819754E-02 0.107572E-01 0.313018E-05 0.885066E-05 ... (15 Replies)
Discussion started by: guns
15 Replies

8. Shell Programming and Scripting

Extracting data between tags based on search string from unix file

Input file is on Linux box and the input file has data in just one line with 1699741696 characters. Sample Input: <xxx><document coll="uspatfull" version="0"><CMSdoc>xxxantivirus</CMSdoc><tag1>1</tag1></document><document coll="uspatfull"... (5 Replies)
Discussion started by: gaya
5 Replies

9. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

10. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies
Login or Register to Ask a Question
patterns(4)						     Kernel Interfaces Manual						       patterns(4)

NAME
patterns - Patterns for use with internationalization tools SYNOPSIS
See the Description section. DESCRIPTION
The patterns file contains the patterns that must be matched for the internationalization tools extract, strextract, and strmerge. The pattern file in the following example is the default patterns file located in /usr/lib/nls/patterns. # This is the header to insert at the beginning of the first new # source file $SRCHEAD1 (1) #include <nl_types.h> nl_catd _m_catd; # The header to insert at the beginning of the rest of the new # source files $SRCHEAD2 (2) #include <nl_types.h> extern nl_catd _m_catd; # This is the header to insert at the beginning of the message # catalogues $CATHEAD (3) $ /* $ * X/OPEN message catalogue $ */ $quote " # This is how patterns that are matched will get rewritten. $REWRITE (4) catgets(_m_catd, %s, %n, %t) # Following is a list of the sort of strings we are looking for. # The regular expression syntax is based on regexp(3). $MATCH (5) # Match on strings containing an escaped " "[^\]*\"[^"]*" # Match on general strings "[^"]*" # Now reject some special C constructs. $REJECT (6) # the empty string ""0 # string with just one format descriptor "%." "%.." # string with just line control in "\." # string with just line control and one format descriptor in "%.\." "\.%." # ignore cpp include lines #[ ]*include[ ]*".*" #[ ]*ident[ ]*".*" # reject some common C functions and expressions with quoted # strings [sS][cC][cC][sS][iI][dD][][ ]*=[ ]*".*" open[ ]*([^,]*,[^)]*) creat[ ]*([^,]*,[^)]*) access[ ]*([^,]*,[^)]*) chdir[ ]*([^,]*,[^)]*) chmod[ ]*([^,]*,[^)]*) chown[ ]*([^,]*,[^)]*) # Reject any strings in single line comments /*.**/ # Print a warning for initialised strings. $ERROR initialised strings cannot be replaced (7) char[^=]*=[ ]*"[^"]*" char[^=]*=[ ]*"[^\]*\"[^"]*" char[ ]***[A-Za-z][A-Za- z0-9]*[[^]*][ ]*=[ {]*"[^"]*" char[ ]***[A-Za-z][A-Za-z0-9]*[[^]*][ ]*=[ {]*"[^\]*\"[^"]*" The default patterns file is divided into the following sections: In the $SRCHEAD1 section, the strmerge and extract commands place text in this section at the beginning of the first new source program, which is prefixed by nl_. These commands define the native language file descriptors that point to the message catalog. In the $SRCHEAD2 section, the strmerge and extract commands place text in this section at the beginning of the second and remaining source programs. These commands also define the native language file descriptors that point to the message catalog. $SRCHEAD2 contains the external declaration of the nl file descriptor. In the $CATHEAD section, the strmerge and extract commands place text in this section at the beginning of the message catalog. In the $REWRITE section, you specify how the strmerge and extract commands should replace the extracted strings in the new source program. You can supply three options to the catgets command: This option increments the set number for each source. This option applies only if you are using the strmerge command. For more informa- tion on set numbers, see the catgets(3) reference page. This option increments the message number for each string extracted. This option applies if you are using either the strmerge or extract commands. This option expands the text from the string extracted. The string can be a error message or the default string extracted and printed by the catgets command. For example, if you want an error message to appear when catgets is unable to retrieve the message from the message catalog, you would include the following line: catgets(_m_catd, %s, %n, "BAD STRING") When catgets fails, it returns the message BAD STRING. In the $MATCH section, you specify the patterns in the form of a regular expression that you want the strextract, strmerge, and extract commands to find and match. The regular expression follows the same syntax rules as defined in regexp(3) reference page. In the $REJECT section, you specify the matched strings that you do not want the strmerge and extract commands to replace in your source program. The regular expression follows the same syntax rules as defined in regexp(3) reference page. In the $ERROR section, the strextract, strmerge, and extract commands look for bad matches and notify you with a warning message. The regular expression follows the same syntax rules as defined in the regexp(3) reference page. RELATED INFORMATION
extract(1), strextract(1), strmerge(1), trans(1), regexp(3) Writing Software for the International Market delim off patterns(4)