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


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
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,

In this file, if there is a comma in the string enclosed within double quote, then that comma should be replaced with white space. For e.g. as shown above highlighted text, comma after NICK should be replaced with space.

I wrote a command to search for comma within double quote, but I am not able to replace the comma with space in the same file.

The commands I tried are as follow.

awk -F'"' '{print "Line # ", NR, $10}' /usr/users/1shahm/salesiq.dat | grep ","

- This find the string and shows on the screen, it is working fine. But instead of replacing value it is just displaying line and line number on screen.

To replace the value, I tried following commands. But they are not working.

1> awk -F'"' '{tr "," " " }' salesiq.dat |grep ","

2> awk -F'"' '{sed -e "s/\,/ /g"}' salesiq.dat |grep ","

Can you suggest me the appropriate command which I can fit in to replace those commas. Please reply to this mail at your earliest.
# 2  
With GNU sed:

Code:
sed -i.orig 's/\("[^",]\{1,\}\),\([^",]\{1,\}"\)/\1 \2/g' filename

for other seds use this:

Code:
sed 's/\("[^",]\{1,\}\),\([^",]\{1,\}"\)/\1 \2/g' filename>filename.tmp&&cp filename filename.orig&&mv filename.tmp filename

If you want "NICK, LCD WATCH" to become "NICK[ ]LCD WATCH", and
not "NICK, LCD WATCH" -> "NICK[ ][ ]LCD WATCH" ([ ] is space, note the double space b/w K and L):

Code:
sed -i.orig 's/\("[^",]\{1,\}\), *\([^",]\{1,\}"\)/\1 \2/g' filename

# 3  
Hi,
Thanks for the solution. That is really working.
I tried second option. Can you please explain how that script is working. And what is the role of all those files named *.tmp , *.orig and salesiq.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #567
Difficulty: Medium
The ternary if-then-else operator generally has the following syntax: E ? X : Y where E is a Boolean expression and if true, the statement is assigned the value Y.
True or False?

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. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

3. UNIX for Dummies Questions & Answers

Replace character string in txt file using input file(S)

Hi I have a large txt file on my AIX server and I need to replace some text using two other files. So filename1 has about 500 lines similar to: txtcode SYStem100 I have the string I want to change in string2 and the new stringname in string3. Does anyone know a way of doing this? I have... (1 Reply)
Discussion started by: Grueben
1 Replies

4. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

5. 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

6. Shell Programming and Scripting

How to change last character in string with a variable value.

Hey Guys, I have text such as this. 28003,ALCORN,2 28009,BENTON,2 28013,CALHOUN,2 28017,CHICKASAW,2 47017,CARROLL,2 05021,CLAY,0 The last digit after the final "," is a variable value. This is the base file. I have to do further execution on this file later and I need to update the... (7 Replies)
Discussion started by: chagan02
7 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. UNIX for Dummies Questions & Answers

Replace a string within 2 points and save it

I've got this xml file <file1> some text here </file1> <file2> some text here </file2> How do I change the text in element file1 to a sentence that I want, defined by variable $sentence. using ksh here. (2 Replies)
Discussion started by: alienated
2 Replies

9. UNIX for Dummies Questions & Answers

Change case of single character in pattern

Using UNIX tools, but not using GAWK (NAWK is OK), is there a more elegant way to achieve this: sed ' s/_a/_A/1 s/_b/_B/1 s/_c/_C/1 rest of alpahabet ' I want to change the case of a single character in each string of a text file. The character will be matched by regex '_' and only... (2 Replies)
Discussion started by: uiop44
2 Replies

Featured Tech Videos