Sed conditional replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed conditional replace
# 1  
Old 12-07-2010
Sed conditional replace

Given this row:

|lastname1|middlename1|firstname1|lastname2|middlename2|firstname2

produce this result:

|lastname|middlename|firstname

where the resultant names are based on the presence of the #2 names above. I.e., if a #2 name is passed (usually will be null,) use that - otherwise use the #1 counterpart.

Example:

|Smith|Frederick|John|null|null|Johnny

result:

|Smith|Frederick|Johnny

I'm just curious if this is something that can be done conditionally in sed - admittedly more for my curiosity than anything else. I'm geared up to just crank out the modest perl to get this done - but I'm looking for something not so "business as usual." :-)

Thanks in advance, Al
# 2  
Old 12-07-2010
Code:
 
awk -F '|' '{if($4=="null") print $1"|"$2"|"$3; else print $4"|"$5"|"$6;}' inputFile

OR
Code:
 
awk -F '|' '{if($4=="null") print $1,$2,$3; else print $4,$5,$6;}' OFS='|' inputFile

for inputFile containing
Code:
 
fdfdf|dfdf|ttrtr|null|null|gfg
fsdfd|hghg|ere|qw12|rer|rrer

Result is
Code:
 
fdfdf|dfdf|ttrtr
qw12|rer|rrer

Is it what you are looking for?
# 3  
Old 12-07-2010
Is it really "null" or is it just an empty field. If the latter is the case change "null" to "" :
Code:
awk -F\| '{for(i=2;i<=4;i++)if($(i+3)!="null")$i=$(i+3);NF-=3}1' OFS=\| infile

# 4  
Old 12-07-2010
Could be done with shell either and notations like

Code:
IFS='|' read l1 m1 f1 l2 m2 f2
lastname=${l2:-$l1} 
firstname=${f2:-$f1}
middlename=${m2:-$m1}

# 5  
Old 12-07-2010
With the proper while loop and printf statement yes, if the [lfm]2 values are not literally "null" that is..
# 6  
Old 12-07-2010
@ctsgnb
I always get [lfm]2 even if they are null
@Scrutinizer
I guess loop should execute for i=1 to 3
Code:
 
for(i=1;i<=3;i++)

# 7  
Old 12-07-2010
@anurag.singh

Quote:
Originally Posted by anurag.singh
@Scrutinizer
I guess loop should execute for i=1 to 3
Code:
 
for(i=1;i<=3;i++)

In this case, because of the leading | , $1 is always empty.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

Conditional Search/Replace

I'm looking for an awk or (preferably) sed solution to search a pipe delimited file for any occurrence of an email address that does not include a designed domain, and replace the email address with a blank. E.g. hello|smith@designateddomain.com|jones@anotherdomain.edu|1234| turns into: ... (2 Replies)
Discussion started by: tiggyboo
2 Replies

3. Programming

Conditional replace after reading in a file

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... (7 Replies)
Discussion started by: naveen@
7 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 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

6. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: robin.r888
3 Replies

7. Shell Programming and Scripting

Replace a column with a value conditional on a value in col1

Hi, Perhaps a rather simple problem...? I have data that looks like this. BPC0013 ANNUL_49610 0 0 1 1 BPC0014 ANNUL_49642 0 0 2 1 BPC0015 ANNUL_49580 0 0 1 1 BPC0016 ANNUL_49596 0 0 2 1 BPC0017 VULGO_49612 0 0 1 1 BPC0018 ANNUL_49628 0 0 1 1 BPC0019 ANNUL_49692 0 0 2 1 170291_HMG... (4 Replies)
Discussion started by: genehunter
4 Replies

8. Shell Programming and Scripting

Conditional edit for a field using sed

Hi I want to repalce a field in a txt file on solaris with say 100 records and each record having a total of 10 fields separated by a ~ . based on the following condition the record should be edited or else the record should be written as it is to a if the seventh field is 'XX' and if... (2 Replies)
Discussion started by: acharania2011
2 Replies

9. Shell Programming and Scripting

awk help to do conditional find and replace

Hi, I have a Line input for awk as follows DROP MATERIALIZED VIEW MCR.COMM_STACK; CREATE MATERIALIZED VIEW "MCR"."COMM_STACK" ON PREBUILT TABLE WITHOUT REDUCED PRECISION USING INDEX REFRESH FAST ON DEMAND START WITH sysdate+0 NEXT SYSDATE + 7 WITH PRIMARY KEY USING DEFAULT... (3 Replies)
Discussion started by: rajan_san
3 Replies

10. 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
Login or Register to Ask a Question