Awk to Search and Replace inside the pipe "|"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk to Search and Replace inside the pipe "|"
# 8  
Old 02-17-2011
Code:
awk -F\| -v s="\"" '{gsub(s,""); for (i=1;i<=NF;i++) $i=s $i s}1' OFS=\| infile

# 9  
Old 02-18-2011
Quote:
Originally Posted by ctsgnb
@fspalero @pravin27 @kurumi @DC Slick:

You missed the " which is before the CCCC

Code:
sed 's/"//g;s:|:"|":g;s:.*:"&":' infile

hi ctsgnb,

can you help me please to include on how to replace the 4th line, it was |"My Test D|1"| would be come |"My Test D1"|.

My Data :

"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"
"DDDD"|"My Test D|1"|"Test "DDDD"|"This is D"


Output :

"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"
"DDDD"|"My Test D 1"|"Test "DDDD"|"This is D"

Thanks and in Advance,
FSPalero
# 10  
Old 02-18-2011
Code:
awk '{for(i=1;i<=NF;i++){gsub(/\|/," ",$i);gsub(/"/,x,$i)}$1="\""$1;$NF=$NF "\""}1' FS='"\\|"' OFS='"|"' infile

# 11  
Old 02-18-2011
Quote:
Originally Posted by Scrutinizer
Code:
awk '{for(i=1;i<=NF;i++){gsub(/\|/," ",$i);gsub(/"/,x,$i)}$1="\""$1;$NF=$NF "\""}1' FS='"\\|"' OFS='"|"' infile

Hi Scrutinizer,

Excellent, it works well. Thanks in a millions.

Regards,
FSPalero
# 12  
Old 02-18-2011
Code:
sed 's:|\([^"]\): \1:g;s:\([^"]\)|:\1 :g' infile

# 13  
Old 02-18-2011
Quote:
Originally Posted by ctsgnb
Code:
sed 's:|\([^"]\): \1:g;s:\([^"]\)|:\1 :g' infile

could be included in the initial run :
Code:
sed 's/"//g;s:|:"|":g;s:.*:"&":;s:|\([^"]\): \1:g;s:\([^"]\)|:\1 :g' infile

Hi Ctsgnb,

Great! it work well, many many thanks for the helps you guys. You save me a lot.

Regards,
FSPalero
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

4. Shell Programming and Scripting

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 (6 Replies)
Discussion started by: kkmdv
6 Replies

5. UNIX for Dummies Questions & Answers

sed for "/7.*" search and replace

Hi , I have this file having thousands of records in it . I want to remove every 9 digit number starting with 7 in it . The few lines from the sample file are "/-GEO-Prizm-Dashes-Covers-Caps-Trim/720000100-200742232-600022761.car"... (8 Replies)
Discussion started by: capri_drm
8 Replies

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

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. Shell Programming and Scripting

awk's getline < "-" seems not work for pipe

Hi all, I have an gawk script to get user's input, So I use getline name < "-" (or getline name < "/dev/stdin") in my script They both work fine when my script deals with files. But it is broken for pipes. When I try "some command | my awk script", the variable name just gets an empty... (17 Replies)
Discussion started by: qiulang
17 Replies

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

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question