Using sed to replace special characters

Tags
shell scripts

 
Thread Tools Search this Thread
# 1  
Old 08-11-2010
Using sed to replace special characters

Hi everyone
I have file1 contains:
'7832'
' 8765
6543
I want a sed command that will format as:
'7832' , '8765' , '6543'

I tried
Code:
sed -e s/\'//g -e 's/^[ ^t]*//;s/[ ^]*$//' file1 > file2
sed -e :a -e '$!N; s/\n/ /; ta' file2

which gives: 7832 8765 6543

I need some help to continue with this. Thanks
# 2  
Old 08-11-2010
Code:
awk -vORS=" , " '{gsub("[^0-9]","");print"#"$0"#"}' file1 | tr "#" "'"

# 3  
Old 08-11-2010
Thanks bartus11. I forgot to mention that the line "may" also start with a letter; how do I modify the awk command?
# 4  
Old 08-12-2010
It should work in that case too.
# 5  
Old 08-12-2010
Code:
tr "\n" "," <file|sed "s/,/','/g;s/''/'/g;s/,'[ \t]*/,'/g;s/','$/'/"

# 6  
Old 08-12-2010
Modified bartus11 code.

Code:
awk -vORS=" , " '{gsub("[^0-9a-zA-Z]","");print"#"$0"#"}' file1 | tr "#" "'"

OR
Code:
awk -vORS=" , " '{gsub("[[:alnum:]]","");print"#"$0"#"}' file1 | tr "#" "'"


|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
How to replace special characters? Ravi.K UNIX for Advanced & Expert Users 4 04-22-2017 04:04 AM
Help to replace the string with special characters nikhil jain Shell Programming and Scripting 3 02-13-2015 08:16 AM
Replace special characters with backslash and character temp_user Shell Programming and Scripting 3 11-25-2014 12:47 AM
How to replace special characters? ken002 Shell Programming and Scripting 25 11-05-2012 10:10 AM
Replace special characters with Escape characters? laknar Shell Programming and Scripting 8 01-06-2012 12:40 AM
Replace special characters ratheeshjulk Shell Programming and Scripting 0 12-22-2011 09:18 PM
How to replace special characters in vi? rogerben Solaris 2 08-03-2011 06:53 AM
Replace new line with <br /> & escape special characters ted_chou12 Shell Programming and Scripting 2 02-07-2011 09:25 PM
SED with Special characters mdbinder Shell Programming and Scripting 5 01-06-2011 05:57 AM
Replace special characters in multiple files - perl RMSoares Shell Programming and Scripting 1 09-01-2009 07:09 PM
sed with many special characters lakanino Shell Programming and Scripting 1 08-13-2009 05:05 PM
sed with special characters itzz.me Shell Programming and Scripting 4 04-06-2009 10:34 PM
Find and replace special characters in a file sujithchandra UNIX for Dummies Questions & Answers 2 10-24-2008 07:59 AM
Help with find and replace w/string containing special characters CAGIRL UNIX for Dummies Questions & Answers 4 10-07-2008 08:13 PM
Replace Special characters in a file solai UNIX for Dummies Questions & Answers 1 07-13-2006 11:36 AM