Conditional replacements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional replacements
# 1  
Old 04-26-2011
Conditional replacements

Hi,
I have a requirement as below
Input
Code:
  Jacuzzi,"Jet Rings, Pillows",Accessory,Optional,,9230917,69094,,P556805,69094,FALSE,1,0,,
  Jacuzzi,"Jet Rings, Pillows, Skirt/Apron",Accessory,Optional,,9230917,69094,,P556805,69094,FALSE,1,0,,


Output
Code:
  Jacuzzi,"Jet Rings!@% Pillows",Accessory,Optional,,9230917,69094,,P556805,69094,FALSE,1,0,,
  jacuzzi,"Jet Rings!@% Pillows!@% Skirt/Apron",Accessory,Optional,,9230917,69094,,P556805,69094,FALSE,1,0,,


i.e. commas present within the column which has double quotes has to be replaced by the !@%.


To achieve this I have used sed.
Code:
sed -e 's/\("[^"][^,]*\),\([^"]*\),\(
[^"]*[^,]"\)/\1!@%\2!@%\3/g' -e 's/\("[^"][^,]*\),\([^"]*[^,]"\)/\1!@%\2/g' "$file"



But now the requirement has changed. Previously I was expecting only 1 or 2 commas i.e. a pattern like "abc,gbf" or "abc,kil,jik" can only come but now I have to generalize it for any no of commas i.e 1,2,3 ---



Any ideas about how to achieve the above.


Thanks for your help in advance.

Last edited by Franklin52; 04-27-2011 at 04:09 AM.. Reason: Please use code tags
# 2  
Old 04-27-2011
Try this sed script

Code:
 
:up
s/\("[^,]*\),\(.*"\)/\1!@%\2/
t up

This User Gave Thanks to 116@434 For This Post:
# 3  
Old 04-28-2011
Many thanks for posting the solution. It is working great .

---------- Post updated at 12:14 PM ---------- Previous update was at 11:50 AM ----------

Can you please explain the working ; I can understand that it is searching the entire part between quotes but not sure about the "t" & ":UP" & "UP"
# 4  
Old 04-28-2011
How about this,
Code:
 perl -nle 'if(/(.*)(".+?")(.*)/){$vr=$3;printf $1;($repl=$2)=~ s/,/!@%/g; print $repl,$vr;}' inputfile

This User Gave Thanks to pravin27 For This Post:
# 5  
Old 04-28-2011
Found the answer thanks to Google.
Pasting it below for ready reference in case someone is not aware of Sed Branching like me
Code:
$ sed ':label command(s) t label'

:label - specification of label.
commands - Any sed command(s)
label - Any Name for the label
t label - jumps to the label only if the last substitute command modified the pattern space. If label is not specified, then jumps to the end of the script. 
b label - jumps to the label with out checking any conditions. If label is not specified, then jumps to the end of the script.

---------- Post updated at 10:19 PM ---------- Previous update was at 01:11 PM ----------

The code using sed :up
s/\("[^,]*\),\(.*"\)/\1!@%\2/
t upwill not work properly in case the input record is of this type
Jacuzzi,"Jet Rings!@% Pillows",Accessory,"abc,def",Optional,,9230917,69094,,P556805,69094,FALSE,1,0,,
i.e if there are multiple columns with double quotes the sed command will fail.
any ideas how to over come this limitation.

Thanks for your help in advance.

Last edited by Franklin52; 04-28-2011 at 05:08 AM.. Reason: Please use code tags
This User Gave Thanks to kewk For This Post:
# 6  
Old 04-28-2011
Use this in that case:

Code:
 
:up1
s/\("[^,]*\),\(.*"\),/\1!@%\2,/
t up1
:up2
s/\("[^,]*\),\(.*"\)$/\1!@%\2/
t up2

If you notice carefully, one double quotes either ends with comma or with end of line. Based on that we can pair up the double quotes. The above code is based on that logic.
This User Gave Thanks to 116@434 For This Post:
# 7  
Old 04-28-2011
Bug

Input
Code:
yuzzi,"Jet Rings, Pillows, Skirt/Apron",Accessory,"abc,reg",,9230917,69094,,P556805,69094,FALSE,1,0,,

with
Code:
:up1
s/\("[^,]*\),\(.*"\),/\1!@%\2,/
t up1

the output was becoming
Code:
yuzzi,"Jet Rings!@% Pillows!@% Skirt/Apron"!@%Accessory!@%"abc!@%reg",,9230917,69094,,P556805,69094,FALSE,1,0,,

I have modified the code a little bit now it is working fine

Code:
:up1
  s/\("[^,"]*\)[^"],\(.*",\)/\1!@%\2/
  t up1


output
Code:
yuzzi,"Jet Ring!@% Pillow!@% Skirt/Apron",Accessory,"ab!@%reg",,9230917,69094,,P556805,69094,FALSE,1,0,,

Many thanks for helping me out Smilie

Last edited by Franklin52; 04-29-2011 at 03:54 AM.. Reason: Please use code tags, thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help building a variable string from a keyword - character replacements!

Hello scripting geniusii! I come to kneel before the alter of your wisdom! I am looking to take a keyword and replace characters within that keyword and add them to a string variable. I would like this to only go through however many characters the word has, which may vary in size. ... (10 Replies)
Discussion started by: ghaniba
10 Replies

2. Shell Programming and Scripting

Optimizing find with many replacements

Hello, I'm looking for advice on how to optimize this bash script, currently i use the shotgun approach to avoid file io/buffering problems of forks trying to write simultaneously to the same file. i'd like to keep this as a fairly portable bash script rather than writing a C routine. in a... (8 Replies)
Discussion started by: f77hack
8 Replies

3. Shell Programming and Scripting

BASH script to read external file to perform text replacements?

Hi all, I have a moderate size (300 lines) BASH Shell script that performs various tasks on different source reports (CSV files). One of the tasks that it performs, is to use SED to replace 'non-conforming' titles with conformant ones. For example "How to format a RAW Report" needs to become... (3 Replies)
Discussion started by: richardsantink
3 Replies

4. Shell Programming and Scripting

awk delete newline after other replacements

Dear All, could you please help me to remove \n characters after all other replacements have been done as in the code below: { #remove punctuation and starting whitespaces gsub("]"," "); $1=$1; } { #print lines containing 'whatever' if ($1=="whatever") {print} #print... (3 Replies)
Discussion started by: shivacoder
3 Replies

5. UNIX for Dummies Questions & Answers

Selective Replacements: Using sed or awk to replace letters with numbers in a very specific way

Hello all. I am a beginner UNIX user who is using UNIX to work on a bioinformatics project for my university. I have a bit of a complicated issue in trying to use sed (or awk) to "find and replace" bases (letters) in a genetics data spreadsheet (converted to a text file, can be either... (3 Replies)
Discussion started by: Mince
3 Replies

6. Shell Programming and Scripting

How to get count of replacements done by sed?

Hi , How can i get count of replacements done by sed in a file. I know grep -c is a method. But say if sed had made 10 replacement in a file, can i get number 10 some how? (8 Replies)
Discussion started by: abhitanshu
8 Replies

7. UNIX for Dummies Questions & Answers

How to use sed to do multiple replacements all at once?

I have a text file where I want to use sed to do multiple replacements all at once (i.e. with a single command) . I want to convert all AA's to 0, all AG's to 1 and all GG's to 2. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

8. Shell Programming and Scripting

Gen random char then sed replacements

Hey guys, I need to first generate some random characters, which I am already doing perfectly as follows: randomize=`cat /dev/urandom | tr -dc "a-z0-9" | fold -w 6 | head -n 1` This is where I am stuck...I need to sed replace some static values with those random characters, but I need each... (4 Replies)
Discussion started by: holyearth
4 Replies

9. Shell Programming and Scripting

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (12 Replies)
Discussion started by: abhinavsinha
12 Replies

10. UNIX for Dummies Questions & Answers

conditional

conditional is not wworking can any one figure out what goes wrong xx1=`$ORACLE_HOME/bin/sqlplus -s apps/ostgapps1 2>/dev/null << EOF WHENEVER SQLERROR EXIT 1 set head off feedback off ; WHENEVER SQLERROR EXIT SQL.SQLCODE; select count(*) from CMS_INVOICE_ALL... (2 Replies)
Discussion started by: u263066
2 Replies
Login or Register to Ask a Question