Home Man
Today's Posts

BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Using sed to replace special characters

shell scripts

Login to Reply

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

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

I tried
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
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
tr "\n" "," <file|sed "s/,/','/g;s/''/'/g;s/,'[ \t]*/,'/g;s/','$/'/"

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

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

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

Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to replace special characters? Ravi.K UNIX for Advanced & Expert Users 4 04-22-2017 03:04 AM
How to replace value of password tag in xml with blanks when special characters are there? saroopkris85 How to Post in the The UNIX and Linux Forums 7 10-27-2015 03:35 PM
Help to replace the string with special characters nikhil jain Shell Programming and Scripting 3 02-13-2015 07:16 AM
How to replace special characters? ken002 Shell Programming and Scripting 25 11-05-2012 09:10 AM
Replace special characters with Escape characters? laknar Shell Programming and Scripting 8 01-05-2012 11:40 PM
Replace special characters ratheeshjulk Shell Programming and Scripting 0 12-22-2011 08:18 PM
How to replace special characters in vi? rogerben Solaris 2 08-03-2011 05:53 AM
Replace new line with <br /> & escape special characters ted_chou12 Shell Programming and Scripting 2 02-07-2011 08:25 PM
Find and replace special characters in a file sujithchandra UNIX for Dummies Questions & Answers 2 10-24-2008 06:59 AM
Replace Special characters in a file solai UNIX for Dummies Questions & Answers 1 07-13-2006 10:36 AM

All times are GMT -4. The time now is 01:44 PM.

Unix & Linux Forums Content Copyright1993-2018. All Rights Reserved.
Show Password