Script to search a string which is in between "" and replace it with another character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to search a string which is in between "" and replace it with another character
# 1  
Old 02-12-2010
Script to search a string which is in between "" and replace it with another character

Hi,

I am trying to search a string from a text file which is in between "" (Double Quotes) (Eg: "Unix"), and replace it with a | where ever it is appearing in the text file and save the file.

Please help me.

-kkmdv
# 2  
Old 02-12-2010
Can you show us the code you have so far, and tell us how it misbehaves?
# 3  
Old 02-12-2010
Hi Franklin,

here is my code which I tried,

Code:
#!/bin/bash
sed '/"/,/"/ !d' sample.txt > sample.txt



but it is deleting the entire data from the file.

Last edited by Franklin52; 02-12-2010 at 07:18 AM.. Reason: Please use code tags!
# 4  
Old 02-12-2010
I don't believe you can direct the output back into your input file. Direct it to something other than sample.txt.
# 5  
Old 02-12-2010
To replace the word Unix between double quotes this should be sufficient:

Code:
sed 's/Unix/|/g' file > tempfile
mv tempfile file

If your sed version supports the -i option:

Code:
sed -i 's/Unix/|/g' file

To replace every word between double quotes you could use:

Code:
awk -F"\"" '{for(i=2;i<NF;i+=2){$i="|"}}1' OFS="\""  file > tempfile
mv tempfile file

# 6  
Old 02-12-2010
Franklin,

THanks for your promt response. your script is working fine but, my problem is,

the script needs to search something like in between double quotes and replace it with | . doesnt matter what ever there in between double quotes.

Eg: if the text file contains something like "<Some text>",after executing the script, it should show like "|".

Thanks,
-kkmdv
# 7  
Old 02-12-2010
Quote:
Originally Posted by kkmdv
Franklin,

THanks for your promt response. your script is working fine but, my problem is,

the script needs to search something like in between double quotes and replace it with | . doesnt matter what ever there in between double quotes.

Eg: if the text file contains something like "<Some text>",after executing the script, it should show like "|".

Thanks,
-kkmdv
The awk command should work in that case. Have you tried it?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

3. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

4. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

5. Shell Programming and Scripting

sed escape character for comment string "/*"

Good afternoon all, I'm hoping my newbie question can help bolster someone's street_cred.sh today. I'm trying to "fingerprint" SQL on its way into the rdbms for a benchmarking process (so I can tie the resource allocation back to the process more precisely). To do this, I'm essentially... (4 Replies)
Discussion started by: toeharp
4 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

Awk to Search and Replace inside the pipe "|"

Hi, Anyone can help me on how to replace the qoutes inside the pipe | in my Text File like belows; "AAAA"|"Test "1-A""|"Test AAAA"|"This is A" "BBBB"|"Test "1-B""|"Test BBBB"|"This is B" "CCCC"|"My Test C"|"Test "CCCC""|"This is C" The output I need like belows; "AAAA"|"Test 1-A"|"Test... (12 Replies)
Discussion started by: fspalero
12 Replies

8. Shell Programming and Scripting

Interesting question - Search and replace the word after sign "="

Hi Guys, Req your help in searching and replacing the word that comes after equals(=) symbol I would like to replace the sting in bold with a string in variable. d=ABCDF8C44C22 # grep -i NIM_MASTERID ${_NIMINFO} export NIM_MASTERID=00CDF8C44C00 I'm looking to replace any word that... (4 Replies)
Discussion started by: ajilesh
4 Replies

9. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

10. UNIX for Dummies Questions & Answers

search excat string in another string (grep "fails")

hello, i have an statement which i have to correct because it shows the wrong result. i want to search an excat string in another string, command "grep" shows the wrong result: example: STRINGS="string1 string2 string3" search_string="string" incorrect: if then echo "not... (0 Replies)
Discussion started by: bora99
0 Replies
Login or Register to Ask a Question