Using sed to replace inside file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using sed to replace inside file
# 1  
Old 01-25-2016
Using sed to replace inside file

How can I change the comma sign (,) to plus sign (+) with the sed command or any regex?
I mean to change only the comma that beteen the quotation marks.

From this file:
Code:
A,B,C
A,"B,C",D
"A,B",C,D
A,B,"C,D"

To this file:
Code:
A,B,C
A,"B+C",D
"A+B",C,D
A,B,"C+D"

# 2  
Old 01-25-2016
Hello elior,

Could you please try following and let me know if this helps.
Code:
awk -F, '{num=split($0, A,",");for(i=1;i<=num;i++){if(A[i] ~ /\"/){X++};if(X==2){OFS="+";X=""} else {OFS=","};Q=Q?Q OFS A[i]:A[i];};print Q;Q=""}'   Input_file

Output will be as follows.
Code:
A,B,C
A,"B+C",D
"A+B",C,D
A,B,"C+D"

Also I have tested above command with your provided sample Input_file only which is having , as a field separator. Kindly post more details with exact sample expected output in case of any queries.


Thanks,
R. Singh
# 3  
Old 01-25-2016
Quote:
Originally Posted by elior
How can I change the comma sign (,) to plus sign (+) with the sed command or any regex?
By using the s-subcommand. I suggest reading the man page of sed and - if you are stuck - ask specific questions once they arise.

There are several good introductions to pattern matching here (for instance: here is one) and i suggeest you use the search function of the forum and see where it takes you.

If you need help with sed in general there are several good books (notably Dale Dougherty's phantastic "sed & awk") as well as countless threads about the specifics here.

I hope this helps.

bakunin
# 4  
Old 01-25-2016
Quote:
Originally Posted by elior
How can I change the comma sign (,) to plus sign (+) with the sed command or any regex?
I mean to change only the comma that beteen the quotation marks.

From this file:
Code:
A,B,C
A,"B,C",D
"A,B",C,D
A,B,"C,D"

To this file:
Code:
A,B,C
A,"B+C",D
"A+B",C,D
A,B,"C+D"

For the first instance:
Code:
perl -pe 's/("\w+),(\w+")/$1+$2/' elior.file
A,B,C
A,"B+C",D,"E,F"
"A+B","C,D",E
A,B,"C+D"

For every instance:
Code:
perl -pe 's/("\w+),(\w+")/$1+$2/g' elior.file
A,B,C
A,"B+C",D,"E+F"
"A+B","C+D",E
A,B,"C+D"

# 5  
Old 01-25-2016
Try this sed :
Code:
user@home:~$ cat input
A,B,C
A,"B,C",D
"A,B",C,D
A,B,"C,D"
user@home:~$ sed 's|\("[A-Z]\),\([A-Z]"\)|\1+\2|g' input
A,B,C
A,"B+C",D
"A+B",C,D
A,B,"C+D"

# 6  
Old 01-25-2016
Does it have to be sed? Otherwise try:
Code:
awk '{for(i=2; i<=NF; i+=2) gsub(",","+",$i)}1' FS=\" OFS=\" file

# 7  
Old 01-25-2016
You can do the whole thing from just sed alone.
Code:
/".*"/{
h
s/.*"\(.*\)".*/\1/
s/,/+/
G
s/\(.*\)\n\(.*\)".*"\(.*\)/\2"\1"\3/
}

First make sure the line contains two quotes, if not you don't want to perform this next code.
The h copies the line to hold space.
First s deletes everything outside the quotes, the next s makes your comma to plus sign change.
The G appends the hold space to pattern space, separated by new-line (\n).
The last s identifies the original pattern space as group 1, after the new line before the first quote is group 2, stuff originally between quotes is ignored (it's fixed and in group 1 now)--that also throws away the quotes, and stuff after the last quote is group 3. The groups are played back in 2, 1, 3 order and the quotes are reinserted on either side of group 1.

HTH

Last edited by wbport; 01-25-2016 at 03:44 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed inside the awk script to replace a string in the array

The requirement is i need to find an array value matching with pattern {5:{ , replace that with 5: and reassign that to same array index and print it. I write something like below and the issue is sed command is not working. If i replace " with "`" the script gives syntax error.how can i... (8 Replies)
Discussion started by: bhagya123
8 Replies

2. Shell Programming and Scripting

Replace text inside XML file based on condition

Hi All, I want to change the name as SEQ_13 ie., <Property Name="Name">SEQ_13</Property> when the Stage Type is PxSequentialFile ie., <Property Name="StageType">PxSequentialFile</Property> :wall: Input.XML <Main> <Record Identifier="V0S13" Type="CustomStage" Readonly="0">... (3 Replies)
Discussion started by: kmsekhar
3 Replies

3. Shell Programming and Scripting

how to find the pattern inside the file and replace it

hello everybody, I have a group of file eg- sample1 sample2 sample3 sample4 each file contain this :- cat sample1 SEQ_NUM,1,UPESI1 My requirement is to change the value-UPESI1 to UPE10 in file which contain this pattern -UPESI1. any help is appreciated. (2 Replies)
Discussion started by: abhigrkist
2 Replies

4. Shell Programming and Scripting

How to use sed to replace the a string in the same file using sed?

How do i replace a string using sed into the same file without creating a intermediate file? (7 Replies)
Discussion started by: gomes1333
7 Replies

5. Shell Programming and Scripting

Inline searc and replace inside file

Hello, I have a text file that i want to redirect into a new file , searching and replacing certain string during the opertaion. This should be done using shell script , so it should not be interactive. The script should get four parameters : source file target file source string target... (1 Reply)
Discussion started by: yoavbe
1 Replies

6. Shell Programming and Scripting

pattern replace inside text file using sed

Hi, I have a situation where I want to replace some occurrences of ".jsp" into ".html" inside a text file. For Example: If a pattern found like <a href="http://www.mysite.com/mypage.jsp"> it should be retained. But if a pattern found like <a href="../mypage.jsp"> it should be changed to... (4 Replies)
Discussion started by: meharo
4 Replies

7. Shell Programming and Scripting

Need help in sed command ( Replacing a pattern inside a file with a variable value )

Hello, The following sed command is giving error sed: -e expression #1, char 13: unknown option to `s' The sed command is echo "//-----" | sed "s/\/\/---*/$parChk/g" where parChk="//---ee-" How can i print the variable value from sed command ? And is it possible to replace a... (2 Replies)
Discussion started by: frozensmilz
2 Replies

8. Shell Programming and Scripting

Sed , Replace a "variable text" inside of a statement

Please Help... I am trying to manipulte the following line Before : <user:Account_Password>002786</user:Account_Password> the password is the "variable", i need to delete / omit the password in the file, (it occurs several thousand times) so the tag line looks like After:... (4 Replies)
Discussion started by: jackn7
4 Replies

9. Shell Programming and Scripting

how to replace a text inside a file based on a xml key

<c-param> <param-name>Number</param-name> <param-value>22</param-value> <description>my house number</description> </c-param> <c-param> <param-name>Address</param-name> ... (4 Replies)
Discussion started by: reldb
4 Replies

10. UNIX for Dummies Questions & Answers

running sed inside script file

i am substituting some text in the xml file using sed, on shell directly it works fine, but when i run it inside script file, it say, the function cant be parsed, i think the prob is due to xml file, kindly help (4 Replies)
Discussion started by: bajaj111
4 Replies
Login or Register to Ask a Question