Replace a string pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a string pattern
# 1  
Old 10-07-2013
Replace a string pattern

Hi,

I have a CSV with following type of data and would like to replace the timestamp information with 'null' string. Can you please suggest me on same?

Code:
8,1,'1','1',11,'2013-08-12 18:34:17.0','null',1,'2013-08-12 18:34:17.0','null','PROMOTIONAL','12','1','11','11',11,'0'

Thanks for your time.
# 2  
Old 10-07-2013
try bit lengthy

Code:
sed 's/....-..-.. ..:..:..../null/g' your_filename

Code:
$ echo "8,1,'1','1',11,'2013-08-12 18:34:17.0','null',1,'2013-08-12 18:34:17.0','null','PROMOTIONAL','12','1','11','11',11,'0'"  | sed 's/....-..-.. ..:..:..../null/g'

resulting

Code:
8,1,'1','1',11,'null','null',1,'null','null','PROMOTIONAL','12','1','11','11',11,'0'


Last edited by Akshay Hegde; 10-07-2013 at 07:05 AM..
# 3  
Old 10-07-2013
Perl.

Code:
echo "8,1,'1','1',11,'2013-08-12 18:34:17.0','null',1,'2013-08-12 18:34:17.0','null','PROMOTIONAL','12','1','11','11',11,'0'" | perl -nle '{s/\d{4}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01]) ([01][0-9]|2[0-3]):[012345][0-9]:[012345][0-9]\.\d?/NULL/g; print}'

# 4  
Old 10-07-2013
Thanks Akshay for replying.

Can you please suggest how can I include the "(" and ")" brackets inside this replacement string as some of the lines in CSV have date value inside braces.

Eg.
('2013-08-12 18:34:17.0') should be set to 'NULL'
# 5  
Old 10-07-2013
try

Code:
$ echo "('2013-08-12 18:34:17.0')" | sed 's/[()]//g; s/....-..-.. ..:..:..../null/g'
'null'

# 6  
Old 10-07-2013
The "[()]" is replacing all braces in CSV. The negative values are mentioned within () and it must be retained.

Code:
echo "(1), ('2013-08-12 18:34:17.0')" | sed 's/[()]//g; s/....-..-.. ..:..:..../null/g'
1, 'null'

Sorry for posting multiple queries. I am totally new to this project and had to try with uploading CSV to find out possible errors. I hope you will understand.
# 7  
Old 10-07-2013
You can split file and then upload
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to replace a string with pattern read from a file

I have two files blocks.txt and rules.txt. In blocks.txt i have the following entries Linux1 Linux2 Linux3 ..... Linux10 In rules.txt i have the lines where a filename pattern starts like 'blk-name.*' I want to replace 'blk-name' with the names read from blocks.txt file I tried... (2 Replies)
Discussion started by: Jag02
2 Replies

2. Shell Programming and Scripting

Replace String matching wildcard pattern

Hi, I know how to replace a string with another in a file. But, i wish to replace the below string pattern EncryptedPassword="{gafgfa}]\asffafsf312a" i.e EncryptedPassword="<any random string>" To EncryptedPassword="" i.e remove the random password to a empty string. Can you... (3 Replies)
Discussion started by: mohtashims
3 Replies

3. UNIX for Advanced & Expert Users

BASH Internal : Replace pattern with string without external command

Morning, I'm trying step up my scripting game .. :rolleyes::confused::D Is there a way to do the replacement with an or without using an external command ? I did try but no joy. var=${var//\(|\)/} #!/bin/bash var="lulus.UbiRwidgets.com (10.1.1.1)" var=${var//\(/}... (5 Replies)
Discussion started by: popeye
5 Replies

4. Shell Programming and Scripting

Replace string in line below specific pattern?

Hi, I'm trying to replace a string with sed, in a text file containing this pattern: location alpha value x location beta value y location gamma value y location delta value y location theta value z ... What I want to achieve is: Find location beta into text file... (1 Reply)
Discussion started by: TECK
1 Replies

5. Shell Programming and Scripting

[SOLVED] Replace a string in nextline after searching a pattern

Hi, I have a requirement where I need to replace a string in a line and this line will be identified by search criteria on previous line: E.g.: I have an xml file and Contents as below: <Root> <NameValue> <name>Global/Text/Data</name> <value>This is valid... (14 Replies)
Discussion started by: mailing2vamsi
14 Replies

6. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

7. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

8. Shell Programming and Scripting

Match pattern and replace with string

hi guys, insert into /*<new>*/abc_db.tbl_name this is should be replaced to insert into /*<new>*/${new}.tbl_name it should use '.' as delimiter and replace is there any way to do it using sed (6 Replies)
Discussion started by: sol_nov
6 Replies

9. Shell Programming and Scripting

replace character in a string pattern and save the change in same file

I am facing one problem, can any one please suggest me the command for the same in unix. I am using Ksh. I have a large file with the data that looks like below. "ROTO2-2007f","_US01","9/15/2007","9/21/2007",346492,"NICK, LCD WATCH"97,1,"NAPOLITJ ","BERGER,M Z & CO INC",0.01, ... (2 Replies)
Discussion started by: mihir0011
2 Replies
Login or Register to Ask a Question