How to replace particular occurrence of character in between a delimiter?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to replace particular occurrence of character in between a delimiter?
# 1  
Old 07-19-2013
How to replace particular occurrence of character in between a delimiter?

Hi,
Hi,

I have a file with following format

Code:
1|" "text " around " |" fire "guest"|" "
2| "xyz"" | "no guest"|"3"
3| """ test3""| "one" guest"|"4"

My requirement is to replace all occurrences of " to ' which are occurring between |" "|delimiter
so my output should look like this

Code:
1|" 'text ' around " |" fire ' guest"|" "
2|"xyz' "|"no guest"|"3"
3|" ' ' test3' "| "one ' guest"|"4"

Any help regarding this will be higly appreciated.

Thanks,
Himani

Last edited by Scrutinizer; 07-19-2013 at 04:34 PM.. Reason: Removed rogue formatting; code tags
# 2  
Old 07-19-2013
Something like this?
Code:
awk '
        {
                gsub ( /\|[ ]*"/, "|\" ");
                gsub ( /"[ ]*\|/, " \"|");

                for ( i = 1; i <= NF; i++ )
                {
                        if ( $i !~ /\|"/ && $i !~ /"\|"/ && i != NF )
                                gsub ( /"/, "\x27", $i )
                }
                print
        }
' file

# 3  
Old 07-19-2013
Another one you could try:
Code:
awk '{for(i=2; i<NF; i++) gsub(/"/, "'\''", $i)}1' FS='^ *"|" *$' OFS=\" RS=\| ORS=\| file

# 4  
Old 07-22-2013
Thanks for your reply but the script is creating double spaces in every field means now and also its not removing the double quotes for record no 3
After running the script my output file look like this

Code:
1 |" ' text ' around " |" fire ' guest"|" "
2 |" xyz ' "|" no gues t"|" 3 "
3 |" ' ' test 3 " "| " one ' guest "|" 4 "

can you please let me know how these extra spaces an d double quotes( after test3" " |) can be removed if I use above scripts

Thanks

Last edited by H_bansal; 07-22-2013 at 04:54 PM.. Reason: code tags, removed spurious formatting
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or other way to find out number of occurrence of 7th character

Hi all, I am looking for to filter out based on 7th character and list the number of occurrence based on the 7th character if p , d , o or m 1. if 7th character is p , Output should be: p_hosts = N 2. if 7th character is d , Output should be: d_hosts = N 3. if 7th character is o , Output... (10 Replies)
Discussion started by: rveri
10 Replies

2. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Awk: count occurrence of each character for every field

Hi, let's say an input looks like: A|C|C|D A|C|I|E A|B|I|C A|T|I|B as the title of the thread explains, I am trying to get something like: 1|A=4 2|C=2|B=1|T=1 3|I=3|C=1 4|D=1|E=1|C=1|B=1 i.e. a count of every character in each field (first column of output) independently, sorted... (4 Replies)
Discussion started by: beca123456
4 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Replace first occurrence after match

hey guys, i have been trying to work this thing out with sed with no luck :confused: i m looking for a way to replace only the first occurrence after a match for example : Cat Realized what you gotta do Dog Realized what you gotta do Sheep Realized what you gotta do Wolf Realized... (6 Replies)
Discussion started by: boaz733
6 Replies

5. Shell Programming and Scripting

Find last occurrence of a character in a string

Hello how to find last occurence of a string for example in the following I want last occurence of '-' i.e. position 12 str="aa-bbb-cccc-ddd-ee" my pupose is to get the string 'ee' Thanks and Regards Chetanz (5 Replies)
Discussion started by: Chetanz
5 Replies

6. Shell Programming and Scripting

Remove last occurrence of character (_) and rest of the string in UNIX (sed)

Hi I need help on this ..!! Input : xx_abc_regA xx_def_regB xx_qwe_regC Now i required the output as the below abc def qwe Need to remove last occurrence of character (_) and rest of the string in Unix (sed). Thanks in Advance ..!!! -Nallachand (3 Replies)
Discussion started by: Nallachand
3 Replies

7. Shell Programming and Scripting

Sed to print a string until the second occurrence of a character

Hi, I am totally new to shell scripting. I have a String "c:\working\html\index.txt.12-12-2009.bkp" I want to check if the string has more than one "." character. If it does I would like to retrieve only "c:\working\html\index.txt" i.e, discard the second occurrence of "." and the rest of the... (7 Replies)
Discussion started by: imr
7 Replies

8. Shell Programming and Scripting

SED replace string by occurrence

hi all, I have a text file with following content PAGENUMBER asasasa asasasa PAGENUMBER sasasasasa PAGENUMBER using sed i want to replace PAGENUMBER by occurrence count eg 1 asasasa asasasa 2 sasasasasa 3 (4 Replies)
Discussion started by: uttamhoode
4 Replies

9. Shell Programming and Scripting

Replace second occurrence only

HPUX /bin/sh (posix) I have a file as such cat dog mouse deer elk rabbit mouse rat pig I would like to replace the second occurrence of mouse in this file with mouse2. The rest of the file has to stay exactly as is. I'm not sure exactly where mouse might be (could be first,second,third... (5 Replies)
Discussion started by: lyoncc
5 Replies

10. UNIX for Dummies Questions & Answers

Search and replace to first occurrence of string

Hi all, I have a very large; delimited file. In vi I would like to replace: CSACT_DY;AVG_UEACT1;uesPerActiveLinkSetSize_1;#;A CSACT_DY;AVG_UEACT2;uesPerActiveLinkSetSize_2;#;A CSACT_DY;AVG_UEACT3;uesPerActiveLinkSetSize_3;#;A with: CSACT_DY;AVG_UEACT1;Average... (7 Replies)
Discussion started by: gilmord
7 Replies
Login or Register to Ask a Question