Changing the first occurrence after matching a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Changing the first occurrence after matching a pattern
# 1  
Old 10-03-2012
Changing the first occurrence after matching a pattern

Hi,

I got a file which looks like this:
Code:
Value A

Status: -
Other: -

Value B

Status: -
Other: -

Value C

Status: -
Other: -

I would like to change only the first line which includes the "Status:" string after matching the line containing "Value B", so the output file should look like this:
Code:
Value A

Status: -
Other: -

Value B

Status: PASSED
Other: -

Value C

Status: -
Other: -

Thanks in advance
# 2  
Old 10-03-2012
Code:
awk 's && /Status:/{$0="Status: PASSED";s=0}/Value B/{s=1}1' file

This User Gave Thanks to elixir_sinari For This Post:
# 3  
Old 10-03-2012
Can you try like..
Code:
sed '0,/Status: -/s//Status: PASSED/' test.txt

This User Gave Thanks to bmk For This Post:
# 4  
Old 10-03-2012
Thank you very much, but is it possible to read from a variable after which line it should be changed? For example I would like to determine in a variable that the "Status" of the "Value B" should be changed

Thanks in advance
# 5  
Old 10-03-2012
Code:
startpatt='Value B'
occur='Status:'
awk 's && $0~occ{$0=occ " PASSED";s=0}$0~sp{s=1}1' sp="$startpatt" occ="$occur" file

This User Gave Thanks to elixir_sinari For This Post:
# 6  
Old 10-03-2012
Thank you very much, it works perfectly!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to extract and print first occurrence of pattern in each line

I am trying to use awk to extract and print the first ocurrence of NM_ and NP_ with a : before in each line. The input file is tab-delimeted, but the output does not need to be. The below does execute but prints all the lines in the file not just the patterns. Thank you :). file tab-delimeted ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

3. Shell Programming and Scripting

Match from one pattern to second occurrence of second pattern

Given an XML file that contains (NOT "consists of"): </dict> <key>system.</key> <dict> <key>rule</key> <string>default</string> </dict> <key>system.burn</key> ... (9 Replies)
Discussion started by: jnojr
9 Replies

4. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

5. Shell Programming and Scripting

print only the first occurrence of a pattern

Hi, I have a file as below select or create proc /*comments*/ /*comments*/ /*comments*/ /*comments*/ ( variables4 datatypes1, variables1 datatypes2, variables2 datatypes3, variables3 datatypes2 ) some text some text ( sometext some text ) some text some text (3 Replies)
Discussion started by: manasa_vs
3 Replies

6. UNIX for Advanced & Expert Users

Unix: list out Pattern occurrence (count)

Need to search a pattern occurrence (count) in a specified file. Below is the details $ cat fruits apple apple ball ball apple ball ball ball apple apple apple cat cat cat cat cat apple apple Note: If I'll use the grep command with -c option then it'll count the 1st occurrence in... (6 Replies)
Discussion started by: tp2115
6 Replies

7. Shell Programming and Scripting

Select everything between first and last occurrence of same pattern

Greetings, I am writing a script which requires as a part, selecting all the lines between the first and last occurrence of a pattern. I have an nawk alternative that is working. But thre should be a generic script that should run on all os viz, linux, sun , aix. The awk script that i... (25 Replies)
Discussion started by: usha rao
25 Replies

8. UNIX for Dummies Questions & Answers

line number of the i-th occurrence of a pattern

Hi all, is there a simple way to obtain the line number of the i-th occurrence of a pattern? I have OCCURRENCE=`grep -io "${STRING_NAME}" ${1}-${8}${EXT}.out_bis| wc -l` which tells me how many occurency I have. I would like to go through them and determine the line number and assign... (6 Replies)
Discussion started by: f_o_555
6 Replies

9. Shell Programming and Scripting

Count the number of occurrences of a pattern between each occurrence of a different pattern

I need to count the number of occurrences of a pattern, say 'key', between each occurrence of a different pattern, say 'lu'. Here's a portion of the text I'm trying to parse: lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1 p-reserver IID 0xdb registrations: key 4156 4353 0000 0000 ... (3 Replies)
Discussion started by: slipstream
3 Replies

10. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies
Login or Register to Ask a Question