Visit Our UNIX and Linux User Community


HELP Need in SED/PERL conditional line replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting HELP Need in SED/PERL conditional line replacement
# 1  
Old 02-01-2010
Java HELP Need in SED/PERL conditional line replacement

Hi ,

I need some help on perl/sed conditional replacement
The situation is like below .

I have a file contents like below .

AAA|BBB|CCC|DDD
AAA|BCF|CCC|HHH
AAA|BVF|JJJ|KKK

Here in the above file . I know my second column value (taking "|" as my delimited )

Basically I have to search for 2nd columm value and replace the 3rd column value in that pattern matching line to some valu i say .

say I know BBB in my first row .I have to replace CCC with whatever data I send .

Thank in advance

--rob
# 2  
Old 02-01-2010
Something like this?

Code:
awk -F"|" '$2=="BBB"{$3=var}1' OFS="|" var="NewValue" file

# 3  
Old 02-01-2010
One more problem now.

Is it possible to pass $values to awk command .
Code:
awk -F"|" '$2=="BBB"{$3=var}1' OFS="|" var="NewValue" file


say here I have to variable s="BBB" va="CCC" But I am not able to replace in awk like below

Code:
awk -F"|" '$2=="$s"{$3=var}1' OFS="|" var="$va" file

Is it possible to replace like above

Last edited by Franklin52; 02-01-2010 at 10:45 AM.. Reason: Please use code tags!
# 4  
Old 02-01-2010
Something like this:

Code:
awk -F"|" '$2==s{$3=var}1' OFS="|" s="BBB" var="NewValue" file


Previous Thread | Next Thread
Test Your Knowledge in Computers #19
Difficulty: Easy
Vint Cerf is generally referred to as the 'Father of the Internet'.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional replacement in CSV files

Hello, I have many CSV files with variable number of rows and columns. Sample of few problematic CSV files. ,,Price,Price,Price,Price,Price,Price,Price,Price,Price,Qty Date,Sl,AAA,BBB,CCC,DDD,EEE,FFF,GGG,HHH,PriQueue,%busy 30/07/2014,1,AAA,BBB,CCC,DDD,EEE,FFF,GGG,HHH,NA,0... (8 Replies)
Discussion started by: reddyr
8 Replies

2. Shell Programming and Scripting

sed conditional \n replace for each line

How could be removed \n only if appearing at position 80 in the line? (4 Replies)
Discussion started by: RomanF
4 Replies

3. Shell Programming and Scripting

sed replacement in file when line is in a variable

Hi, I have a file where I want to replace the 15th field separated by comma, only on specific lines matching lots of different conditions. I have managed to read the file line by line, within the loop my line is held in a variable called $line I assume this will be using sed (maybe... (5 Replies)
Discussion started by: jpt123
5 Replies

4. Shell Programming and Scripting

Perl: Conditional replace based on previous and current value in a line

I need to read the contents of a file. Then I need to grep for a keyword and replace part of the grepped line based on the condition of previous and present line. Example input file: V { port1 = P; port2 = 0; shift_port = P0; /* if next shift_port is P0 I need... (9 Replies)
Discussion started by: naveen@
9 Replies

5. Shell Programming and Scripting

Conditional replacement of a delimiter

Hello, I'm new to this forum but this seems like the place to ask this question. I have a pipe delimited data file with the fields except for the header being encased in double quotes. I found out that some of the fields have an trash pipe within the data itself. I'd like to conditionally... (4 Replies)
Discussion started by: samahs
4 Replies

6. Shell Programming and Scripting

conditional replacement

Hi all, I need a bash, sed, awk script or one liner to do the following task: This is the format of a text file: 2010-06-11 20:01 902656 HOP-W-100412-1.doc 2010-11-05 18:01 364447 NEX-W-101104-1 2010-07-06 10:01 64512 Cerintele 2010-07-06 10:01 599420 content 2010-07-19 14:01 1785344... (7 Replies)
Discussion started by: supervazi
7 Replies

7. Shell Programming and Scripting

Conditional tab replacement sed/awk

Hi I am struggling to find a solutions to this problem: I have a directory full of files and I wish to: read each line of each file and if any one line in those files is longer than 72 characters I want to replace any tab characters with a space character. Ive been... (3 Replies)
Discussion started by: benackland
3 Replies

8. Shell Programming and Scripting

sed xml file multiple line replacement

I have a file called config.xml, it's a simple xml file, and I need use sed/awk to erase some lines. <machine xsi:type="unix-machineType"> <name>server1</name> <node-manager> <name>server1</name> <listen-address>server1</listen-address> </node-manager> ... (3 Replies)
Discussion started by: cbo0485
3 Replies

9. Shell Programming and Scripting

sed conditional string replace for each line

Hi all, I appreciate the enormous amount of knowledge that flows in this forum. I am an average UNIX user. I have many files with lines like the below. I have separated each line with space for ease of reading. I need to replace the first occurance of "/00" with null on those lines that have... (6 Replies)
Discussion started by: Nanu_Manju
6 Replies

10. Shell Programming and Scripting

Replacement of sed with perl

Hi using the below cmd i am identifying wheether last character in each line in thousands of files as semicolon or not.If last character is semicolon i am removing semicolon .If last character is not semicolon then i am appending next line to present line . For example my input file consists of... (4 Replies)
Discussion started by: dbsurf
4 Replies

Featured Tech Videos