Solved

replace column/record content in a *csv file based on a regular expression

Posted on 2016-11-01
6
63 Views
Last Modified: 2016-11-02
Dear experts,
I have a csv file to process, for each column for each record there is to check if a certain pattern is found, if the pattern is found the pattern itself and the next n (staic value) characters following the pattern of the record has to be replaced by a defined string.
The sring sequence: £@£1£@£ is stable and is always the prefix of the string part to be replaced, to be replaced: £@£1£@£+ next 18 characters
The to be replaced pattern can occur in any column of the csv file and at any position within a column.
Excemple csv file
record1 £@£1£@£[234234-23423-234] column1, record1 column2
record2 column1, £@£1£@£[634567-56743-432] record2 column2

expected result
record1 REPLACEMENTSTRING column1, record1 column2
record2 column1, REPLACEMENTSTRING  record2 column2

The unix OS is AIX

Many thanks for your help
0
Comment
Question by:mruff
  • 4
  • 2
6 Comments
 
LVL 4

Expert Comment

by:Abhimanyu Suri
ID: 41868367
/SURI>cat sed.txt
record1 #@#1#@#[234234-23423-234] column1, record1 column2
record2 column1, #@#1#@#[634567-56743-432] record2 column2

/SURI>sed -e 's/\(#@#1#@#\).*\{18\}/REPLACEMENT/g'  sed.txt
record1 REPLACEMENT column1, record1 column2
record2 column1, REPLACEMENT record2 column2
0
 

Author Comment

by:mruff
ID: 41868495
Dear Abhimanyu
Executing the command I get:
sed: -e expression #1, char 35: Invalid preceding regular expression
0
 
LVL 4

Accepted Solution

by:
Abhimanyu Suri earned 500 total points
ID: 41868947
Try this please

sed -r 's/(#@#1#@#).{18}/REPLACEMENT/g'  sed.txt

What is your OS ?
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:mruff
ID: 41869657
Dear Abhimanyu,
many thanks this does the job, the os is AIX
now I have another pattern
sed1.txt
record1 $#$1$#$[234234-23423-234] column1, record1 column2
record2 column1, $#$1$#$[634567-56743-432] record2 column2
sed -r 's/($#$1$#$).{18}/REPLACEMENT/g'  sed1.txt >sed1_.txt

does not do the replacement, what am I missing?
many thanks!
0
 

Author Comment

by:mruff
ID: 41869664
got it had to excape the $ and # by \$\#
0
 

Author Closing Comment

by:mruff
ID: 41870599
Many thanks works perfect
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question