Sponsored Content
Top Forums Shell Programming and Scripting If first pattern is found, look for second pattern. If second pattern not found, delete line Post 302825589 by DGPickett on Monday 24th of June 2013 05:21:27 PM
Old 06-24-2013
In a variety of tools from sed on up, your strategy must be to capture the lines starting with the header in a buffer until you find a next header or EOF, then if the first header is not to your liking, delete all but the second header line and return to get another line. In sed. this is /pattern/ match, b and : for loop, N to add another line to the buffer, $ to test EOF. The last group is irregular as it has no second header line. You might h the line, remove the second header if any, decide if it is a keeper, and the g the original buffer back. You might add a dummy final header to stdin using "( cat file ; echo dummy )|sed", but of course you need to remove it. You can d or "s/...//" what you dislike or "sed -n" and p what you like, but remember there is a second header in the buffer!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete a block of text delimited by blank lines when pattern is found

I have a file which contains blocks of text - each block is a multi-lines text delimited by blank lines eg. <blank line> several lines of text ... pattern found on this line several more lines of text ... <blank line> How do you delete the block of text (including the blank lines) when... (17 Replies)
Discussion started by: gleu
17 Replies

2. Shell Programming and Scripting

Finding Last occurance of another pattern when a pattern is found.

Hi, I have two files viz, rak1: $ cat rak1 rak2: $ cat rak2 sdiff rak1 rak2 returns: I want the lines that got modified, changed, or deleted preceding with the section they are in. I have done this so far: (1 Reply)
Discussion started by: rakeshou
1 Replies

3. Shell Programming and Scripting

change line found by pattern using sed

I want to change a line like CPM_THRESHOLD 0.8 // to a new value using sed I am trying sed -i "s/CPM_THRESHOLD/CPM_THRESHOLD\t$COH\t\t\/\//" $INPUT_4 but how can i substitute the whole line begining with CPM_THRESHOLD and substitute it? (2 Replies)
Discussion started by: larne
2 Replies

4. Shell Programming and Scripting

How to replace a line below where the pattern found

Hi All, I have a file say abc.xml. In this file, I need to search for a pattern “SAP_GATEWAY_HOST”; if this pattern found and the next line also contain the pattern “nwprc03.cos” then I need to replace this pattern “nwprc03.cos” with some other pattern “nwdrc03.apjp”. $ cat abc.xml... (3 Replies)
Discussion started by: Ritesh.patni84
3 Replies

5. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

6. Shell Programming and Scripting

Delete line if pattern not found

I thought that this was going to be quit simple using sed but i wasn't able to find a way to delete the second line of a text file if my pattern was not found in the line With awk i am completly useless :rolleyes: Any ideas? (2 Replies)
Discussion started by: jepeto
2 Replies

7. Shell Programming and Scripting

To find the line no, where the particular pattern is not found

Hi, suppose i have a txt file containing thye following data 2012156|sb3|nwknjps|BAYONNE|NJ|tcg 201221|094|mtnnjprc:HACKENSACK|NJ|tcg 201222|wn3|mtnnjtc|HACKENSACK|NJ|tcg 2018164|ik4|mtnntc|JERSEY CITY|NJ|tcg 20123482|ik4|mtnnjpritc,JERSEY CITY|NJ|tcg... (3 Replies)
Discussion started by: priyanka3006
3 Replies

8. UNIX for Dummies Questions & Answers

Replace line with found unknown pattern

Hi, I have a file with the following content: --------- a 3242 tc_5 gdfg4 random text a 3242 tc_6 gdfg4 random text a 3242 tc_7 gdfg4 random text a 3242 tc_4 gdfg4 --------- I want to replace the lines containing tc_? (tc_5, tc_6 etc. even with unknown numbers) with the found... (5 Replies)
Discussion started by: joas
5 Replies

9. Shell Programming and Scripting

prompt to delete each record when pattern is found

Hello!. I am working on a very simple program and I have been trying different things. This is so far what I have done and there is one small detail that still does not work. It finds all the records in a phonebook per say: ./rem Susan More than one match; Please select the one to remove: ... (3 Replies)
Discussion started by: bartsimpsong
3 Replies

10. Shell Programming and Scripting

Append text on particular line after pattern found

hi, i have /etc/inittab, I want to add another line after that when i find a pattern "l6:6:wait:/etc/rc.d/rc 6". original l6:6:wait:/etc/rc.d/rc 6 after-change l6:6:wait:/etc/rc.d/rc 6 /sbin/if-pp-to-cng (3 Replies)
Discussion started by: learnbash
3 Replies
smfi_chgheader()(3N)													      smfi_chgheader()(3N)

NAME
smfi_chgheader() - changes or deletes a sendmail message header SYNOPSIS
PARAMETERS
ctx Specifies the opaque context structure. headerf Specifies the header name, which is a non-NULL, null-terminated string. hdridx Specifies the header index value (1-based). A hdridx value of 1 modifies the first occurrence of a header named If hdridx is greater than the number of times appears, a new copy of is added. DESCRIPTION
changes the value of a header for the current message. can be called only from the routine. Notes o While can be used to add new headers, it is more efficient to use o A filter that calls must set the flag in the structure passed to o The filter order is important for Filter placed later in the sequence will observer the changes already done by earlier filters. o The name and the value of the header are not checked for standards compliance. However, each line of the header must be under 2048 characters. If longer headers are needed, multi-line header must be used. To make a multi-line header, insert a linefeed (ASCII 0x0a, or in C) followed by at least one whitespace character, such as, a space (ASCII 0x20) or tab (ASCII 0x09, or in C). The linefeed must not be preceded by a carriage return (ASCII 0x0d); the MTA adds this automatically. It is the responsibility of the filter writer to ensure that standards are not violated. RETURN VALUE
returns due to the following reasons: o headerf contains a NULL value. o Modifying headers in the current connection state is invalid. o Memory allocation fails. o Network error occurs. o is not set when is called. returns on success. EXAMPLES
AUTHOR
was developed by the Sendmail Inc. SEE ALSO
smfi_addheader(3N), smfi_insheader(3N), smfi_addrcpt(3N), smfi_delrcpt(3N), smfi_replacebody(3N). on smfi_chgheader()(3N)
All times are GMT -4. The time now is 01:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy