Editing the next line of the pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Editing the next line of the pattern
# 1  
Old 04-09-2013
Editing the next line of the pattern

Hi All,

I am trying to edit the next line when there is a success for a pattern.
For example:

Code:
<nvname>abc</nvname>
<nvvalue>1</nvvalue>
<nvname>def</nvname>
<nvvalue>2</nvvalue>

once there is a hit for the string "abc" the value of the nvvalue should be replaced with the value we already have.

the new file should be

Code:
<nvname>abc</nvname>
<nvvalue>4</nvvalue>
<nvname>def</nvname>
<nvvalue>2</nvvalue>

I tried several options of sed but I am not able to achieve the same.
The arguments I tried are:
c
d
n
s
g

Last edited by joeyg; 04-09-2013 at 10:46 AM.. Reason: Please wrap data and commands inside CodeTags
# 2  
Old 04-09-2013
Code:
awk '/>abc</{f=1}f&&!/>abc</{sub(/>.*</,4);f=0}1' inputfile

# 3  
Old 04-09-2013
Hi Yoda I have one issue here I dont have abc as a static string.
I have a variable for this and if I have ' ' quotes then we are not able to pick the value of the variable
also I am a little new to Unix if possible can you please briefly explain the approach you have followed.
# 4  
Old 04-09-2013
If you have string in variable, then assign it to an awk variable:
Code:
VAR="abc"

awk -v V="$VAR" '                                       # Assign V = "$VAR"
                {
                        if ( $0 ~ ">" V "<" )           # If pattern ">" V "<" (>abc<) in current record.
                                f = 1                   # If found, set f = 1
                        if ( f && $0 !~ ">" V "<" )     # If f == 1 and pattern ">" V "<" (>abc<) not in current record.
                        {
                                sub(/>.*</, ">4<")      # Substitute everything with in >.*< by ">4<"
                                f = 0                   # Reset f = 0
                        }
} 1 ' file

This User Gave Thanks to Yoda For This Post:
# 5  
Old 04-09-2013
Hi Yoda I got that thanks
However I have one more issue I am using a command
Code:
sed s/$original/$new/g

in this case my new variable is like this
Code:
new=<a>123</a>\n<b>345</b>

I want the two xml tags to be in sep line and so I have inserted a \n
this is working fine in echo
however when I do the sed stuff I can see a n in the file itself.
Please let me know if we can actually add a new line

Last edited by vbe; 04-09-2013 at 12:32 PM.. Reason: code tags
# 6  
Old 04-09-2013
I'm not sure if I got your requirement right.

Let say you have below line in file:
Code:
<a>123</a><b>345</b>

To get them into separate lines:
Code:
$ awk '/>123</{sub(/></,">\n<")}1' file
<a>123</a>
<b>345</b>

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

3. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

4. Shell Programming and Scripting

Editing the first line of file

I have a sample file as below :- RECORD_COUNT|2660|28606946.86|20110701122349694| adkad|wwsgesg|mkmk|FFFF|FAFAF|FFAF|FAFFFFA|5894858| I have to replace the second coulmn in the first row which is the count of no of lines in the file from a new variable . Also I have to delete the 3rd... (2 Replies)
Discussion started by: Sanjeev Yadav
2 Replies

5. UNIX for Dummies Questions & Answers

Line editing

I have some trouble editing a line. My aim is to edit the first line of a file.It will look the code and if there are dummy characters at the begininng of line before < it will erase. It is a php code.For instance: for instance if the first line is like %^<?php>sadsa</php>, it will change it to... (6 Replies)
Discussion started by: king87
6 Replies

6. Shell Programming and Scripting

awk line editing

Hello I am trying to make an awk script that always replaces the same characters in a file, with white space in between. Can anyone write a script that prompts for the 3 numbers and then replaces them in the file? eg. enter a b c: 'it then puts a b c in the file. The file format is always ... (12 Replies)
Discussion started by: gav2251
12 Replies

7. Shell Programming and Scripting

find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: FRM CHK 0000 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (4 Replies)
Discussion started by: nickg
4 Replies

8. UNIX for Dummies Questions & Answers

find pattern delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: W/D FRM CHK 00 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (1 Reply)
Discussion started by: nickg
1 Replies

9. Shell Programming and Scripting

Editing last line of a file

Hi All, Could you please help me out with this problem? I need to edit the last line of my file. Ex: The last line in my file will be say 000056000045 8 I need to subtract some number from the number 45 and replace the new number in its place. ... (1 Reply)
Discussion started by: Anitha Chandran
1 Replies

10. UNIX for Dummies Questions & Answers

editing bash command line with vi

Is there a way using bash that I can edit a command line using vi. I.e. if I have a long command line and I want to edit it.....by typing vi and then having the command open in an editing window.... I beleive this can be done in k shell by pressing v....however can find out how this can be... (3 Replies)
Discussion started by: peter.herlihy
3 Replies
Login or Register to Ask a Question