Complicated string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Complicated string
# 1  
Old 06-30-2014
Complicated string

I have a data file example


Code:
10302|77747373|442422442|290209|244242|"234|2352"|92482892

It has about 5000 rows the same way in that field.

Needs to look like this.... I need to remove the quotes but the more difficult thing is to remove the pipe between the quotes because there is a pipe in between? Any ideas?

Code:
10302|77747373|442422442|290209|244242|2342352|92482892


Last edited by Don Cragun; 06-30-2014 at 04:53 PM.. Reason: Add CODE tags.
# 2  
Old 06-30-2014
Try:
Code:
awk '{sub(/[|]/,x,$2)}1' FS=\" OFS= file

or something like:
Code:
sed 's/"\([^|]*\)|\([^"]*\)"/\1\2/g' file

# 3  
Old 06-30-2014
Note the two solutions (awk and sed) are subtly different. Particularly in how they deal with quoted strings that don't contain a pipe.

The sed version only removes the quotes (and pipe) if they contain a pipe and does this for all instances on the line (g option), where the awk removes the first set of quotes on the line (and pipe if present), regardless of them containing a pipe.
# 4  
Old 06-30-2014
Not quite. The awk only removes the first set of quotes, IF they contain a pipe symbol. This was done, since the input specification says there is only one field per line. If there are more sets of quotes on the line, then they are also going to be removed, but if that is the case then we also need a different solution.

With the sed at first I left out the g-flag, so that it did the same, but decided to put it in so that if the OP's input file was a little bit different than stated (namely only one field with quotes) he could see the difference and report back...
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 06-30-2014
Interesting, I didn't know sub worked that way, in contrast to this awk '{$2=$2}1' FS=\" OFS= file
# 6  
Old 06-30-2014
It is not sub that works this way, but rather it is that $2 only gets changed by sub when the substitution is succesful. If not, then the record does not get recalculated and thus the field separators (the double quotes) remain unchanged.
# 7  
Old 06-30-2014
Yes, output is not written unless the RE matches. For some reason I thought it was equivalent to this$2=gensub(/[|]/,x,"",$2)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Solaris? Is it really this complicated?

Hello Everybody, I am a mid-level Linux administrator trying to get a hold of Solaris. So far, my experience has been painful. I think it's confusing and it's just a big mess... To be more specific, I have noticed that whenever I install a package... it never followed a specific pattern.... (4 Replies)
Discussion started by: EssenceNY
4 Replies

2. Shell Programming and Scripting

complicated alias command

hi guys i m making one alias which will set variable , invoke sqlplus and also set prompt of sqlplus,,i have made successfully upto invoking sqlplus in unix but cant pass command in sqlplus here is the command alias sett='export ORACLE_SID=devdb2;sqlplus system/system@test' now this... (3 Replies)
Discussion started by: tapia
3 Replies

3. Linux

Complicated Join command!

Hi I have a serious issue when trying to join to files so I have two files, one for meals and one for people Meal1:Turkey:Potato Chips:Twinkie:Coke:5.95 Meal2:Ham & Cheese:Doritos:Cookie:Sprite:6.49 Meal3:Vegetarian:Cheese Crackers:Brownie:Pepsi:5.75 Meal4:Tuna:Cheese Puffs:Eclair:Diet... (1 Reply)
Discussion started by: ehshi1992
1 Replies

4. Shell Programming and Scripting

complicated search within file

Hi, I have following problem. I have a file with time stamps and some data describing what happened between time stamps. Something like this: 10:00 meeting with K meeting with L 11:00 lunch 12:00 work with K 13:00 From this file I have to get a file with... (7 Replies)
Discussion started by: mmike
7 Replies

5. Shell Programming and Scripting

Seems Complicated but would b happy if its possible

Hey guys, I m here again with (may b) a silly query n sorry for that, I am new to UNIX n all so don't kno much @ it. Thro' a script I am generating an output (It is a TXT file) After generating it, I get it to windows n mails it by dragging n dropping it in lotus mail new mail window. Is there... (6 Replies)
Discussion started by: anushree.a
6 Replies

6. Shell Programming and Scripting

Sort complicated two fields

Hi experts, I am trying sort command with my data but still not getting the expected results. For example, I have 5 fields data here c,18:12:45,c,c,c d,12:34:34,d,d,d a,13:50:10,a,a,a b,13:50:50,b,b,b a,13:50:50,a,a,a b,14:10:01,b,b,b c,10:12:45,c,c,c I want to get ... (3 Replies)
Discussion started by: lalelle
3 Replies

7. UNIX for Dummies Questions & Answers

complicated(?) grep

Hi all, I need help to figure out this grep. I would like to search all files in a directory for the occurrence of abc AND xyz. I started with this: grep -i abc * | grep xyz But this only returns lines which contain both values on the same line. So I am thinking that I need to do... (5 Replies)
Discussion started by: khearnen
5 Replies

8. Shell Programming and Scripting

More complicated log parsing

Hey Guys, I am trying to grep within a file to find and output certain parts of lines to other file(s). The output files need to have a dynamic file name based on a field in the main log. The problem is that every line of the log is not the same, and often not even similar. To explain... (25 Replies)
Discussion started by: sjug
25 Replies

9. Shell Programming and Scripting

Very complicated script..

I have a script that I need to create tha involves moving files and renaming them(see previous post) Are there any websites with user made shell scripts? (5 Replies)
Discussion started by: rocinante
5 Replies

10. Shell Programming and Scripting

Complicated string searching in a file

Hi folks, Following a part of opmn.xml file: <process-type id="OC4J_RiGHTv_PLATOR81" module-id="OC4J"> <environment> <variable id="LD_LIBRARY_PATH" value="/home/ias/v10.1.2/lib" append="true"/> <variable id="SHLIB_PATH"... (5 Replies)
Discussion started by: nir_s
5 Replies
Login or Register to Ask a Question