Replace char between chars - help needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace char between chars - help needed
# 1  
Old 07-06-2012
Question Replace char between chars - help needed

Hello,

I have a csv file with "^" as text delimiters and "|" as field delimiters. It's converted from a xls file. One record looks like this:

Code:
 ^Tablete Internet^|Archos|501838|^Tableta Internet ARCHOS 80 G9
...| ...
(more lines)
... "501|838"^|330.00|USD|sl|12|0|Link|^router wireless 150 Mbps^|^5 ron^|more|^other info^|

I have bolded the "|" which is delimiting the actual fields. I need to replace the "|" and newlines which can apear multiple times in the 4th field which is also delimited by the text delimiters "^".

The output must be a csv file with | as delimiters and without any new line in the 4th field so I can insert the fields into a mysql database.

My best result by now is:

Code:
awk -F '[\^\^]' '{gsub("\|",";",$2); $2; print}'

but that does not remove the new lines... and also changes some "|" needed in the last fields.

Any ideas?

I forgot to mention that i've also tried with sed and perl but with no luck.
# 2  
Old 07-06-2012
why can't u just try to remove all the ^ symbols??

won't that looks like a file with just | being the delimiter? just a clarification!
# 3  
Old 07-06-2012
Quote:
Originally Posted by PikK45
why can't u just try to remove all the ^ symbols??

won't that looks like a file with just | being the delimiter? just a clarification!
Because I'll have too many fields then... the 4th field will be splitted in many...
I'll do that only after I have replaced the "|" from the 4th field...

Last edited by go0ogl3; 07-06-2012 at 04:33 PM.. Reason: Clarification
# 4  
Old 07-06-2012
Considering your 4th field to be

Code:
 ^Tableta Internet Archos ....^

I don't think this would cause any issues!!! Please correct me if I am wrong
# 5  
Old 07-06-2012
Quote:
Originally Posted by PikK45
Considering your 4th field to be

Code:
 ^Tableta Internet Archos ....^

I don't think this would cause any issues!!! Please correct me if I am wrong
My 4th field is:

Code:
|^Tableta Internet ARCHOS 80 G9
...| ...
(more lines)
... "501|838"^|

don't forget that this is only one record which splits on multiple lines...
# 6  
Old 07-06-2012
And how do you make out the end/beginning of any record?
# 7  
Old 07-06-2012
Quote:
Originally Posted by elixir_sinari
And how do you make out the end/beginning of any record?
All records end with a new line. Each record has 12 fields separated by "|". If the field contains multiple words with spaces or new lines, then is enclosed with "^".
My problem is I'm not so experienced with regular expresions. I never neded to parse a field from a record wich splits multiple lines...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Replace \n char in Data

File is pipe delimited with 17 fields. We may get \n char (1 or more \n in one field or multi fileds) in data in any field. Need to replace \n in data with space and not the Ture \n that is line separator. I tried below awk command it did not work as expected. awk '(NR-1)%2{$1=$1}1' RS=\|... (9 Replies)
Discussion started by: rajeshkumare
9 Replies

2. UNIX for Dummies Questions & Answers

Grep or sed to search, replace/insert chars!

HI All Im trying to come up with an approach to finding a string, using a portion of that string to insert it on lines starting with the value "GOTO" appending to end of line after removing PT's ( See example below! ) EXAMPLE: 1. I would like to search for the line that starts with "TLAXIS/"... (7 Replies)
Discussion started by: graymj
7 Replies

3. UNIX for Dummies Questions & Answers

Replacing multiple special chars with single char

Hi I've a string . And i need to replace set of characters with a single character Means .. or . or ... and so on should be replaced with single % character Irrespective of number of dots in between the characters , those should be replaced with single % All the above strings should be... (3 Replies)
Discussion started by: smile689
3 Replies

4. Shell Programming and Scripting

Find and replace all extended char.

Hi Guys, I wand find and replace all Extended ASCII Codes from all my log files. My Log files: /home/Kalr/PPool/Output i have logs file in sub dir. /home/Kalr/PPool/Output/X /home/Kalr/PPool/Output/Y /home/Kalr/PPool/Output/Z My Abc.log file input: Extended ASCII Codes :– ... (4 Replies)
Discussion started by: asavaliya
4 Replies

5. Shell Programming and Scripting

Replace char on text file

Hi , I have problem on replace char on text file #!/bin/bash echo "Enter Third value : " read a sed '2 s/192.160.1.1/cut -d"." -f3/$a/g' tcpip.txt > a.txt I want replace line 2 on tcpip.txt with value a that I input but it's not run Please help me (2 Replies)
Discussion started by: phillipss
2 Replies

6. Shell Programming and Scripting

Help needed to remove a char from a specified sub-string

Hi! I'm having trouble usind sed to remove the char ' from within a database's varchar register's sql. For example, on the following sql: INSERT INTO patrimonio_municipal.patrimonio_municipal_airc_tmp Values('|Estação Elevatória|',|16723|,'|Grandes Reparações|', '|2010-03-26... (3 Replies)
Discussion started by: cremat0rio
3 Replies

7. Shell Programming and Scripting

How to replace any char with newline char.

Hi, How to replace any character in a file with a newline character using sed .. Ex: To replace ',' with newline Input: abcd,efgh,ijkl,mnop Output: abcd efgh ijkl mnop Thnx in advance. Regards, Sasidhar (5 Replies)
Discussion started by: mightysam
5 Replies

8. Shell Programming and Scripting

Replace Junk chars (Sed)

I know this has been asked previously on this forum...But I think I have a different scenario to present. I ahve a file tht looks like this (note:there are control Z and other chars tht are not visible on line with anme bowers) BB7118450 6004718 BIANCALANA =HEI BZ5842819 ... (4 Replies)
Discussion started by: alfredo123
4 Replies

9. Shell Programming and Scripting

replace chars,

:rolleyes: Hi, I want to replace the particular text in the middle of the line. Ex. In the line 40-50 wanted to replace the char as 'x' (7 Replies)
Discussion started by: Jairaj
7 Replies

10. Shell Programming and Scripting

replace ascii chars without loosing it.

Hi, Can some one tell, how to replace ascii non printable TAB from the while to something, then later on replace it back to TAB. Basciallz we do bulk data processing, our processin treats TAB as new field , So I thought we can replace it with something and later on revert it. TIA (4 Replies)
Discussion started by: braindrain
4 Replies
Login or Register to Ask a Question