Shell Programming and Scripting

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

👤 Login to reply

    #1  
Old 08-11-2010
nimo nimo is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 30 April 2014, 8:23 AM EDT
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 08-11-2010
bartus11's Unix or Linux Image
bartus11 bartus11 is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 3 August 2016, 11:03 AM EDT
Posts: 3,733
Thanks: 7
Thanked 1,153 Times in 1,123 Posts
Code:
awk -vORS=" , " '{gsub("[^0-9]","");print"#"$0"#"}' file1 | tr "#" "'"

Sponsored Links
    #3  
Old 08-11-2010
nimo nimo is offline
Registered User
 
Join Date: Aug 2009
Last Activity: 30 April 2014, 8:23 AM EDT
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
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
bartus11's Unix or Linux Image
bartus11 bartus11 is offline
Registered User
 
Join Date: Apr 2009
Last Activity: 3 August 2016, 11:03 AM EDT
Posts: 3,733
Thanks: 7
Thanked 1,153 Times in 1,123 Posts
It should work in that case too.
Sponsored Links
    #5  
Old 08-12-2010
kurumi kurumi is offline
Resident Ruby Wrangler
 
Join Date: Dec 2009
Last Activity: 6 February 2016, 7:08 AM EST
Posts: 602
Thanks: 2
Thanked 83 Times in 78 Posts
Code:
tr "\n" "," <file|sed "s/,/','/g;s/''/'/g;s/,'[ \t]*/,'/g;s/','$/'/"

Sponsored Links
    #6  
Old 08-12-2010
pravin27 pravin27 is offline Forum Advisor  
Advisor
 
Join Date: Sep 2009
Last Activity: 21 June 2017, 2:44 PM EDT
Location: ./India/Bangalore
Posts: 1,271
Thanks: 70
Thanked 299 Times in 290 Posts
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 "#" "'"

Sponsored Links
👤 Login to reply

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
Single/Multiple Line with Special characters - Find & Replace in Unix Script r_sarnayak Shell Programming and Scripting 0 06-02-2010 02:34 AM
Replace special characters in multiple files - perl RMSoares Shell Programming and Scripting 1 09-01-2009 06:09 PM
Find and replace special characters in a file sujithchandra UNIX for Dummies Questions & Answers 2 10-24-2008 06:59 AM
Help with find and replace w/string containing special characters CAGIRL UNIX for Dummies Questions & Answers 4 10-07-2008 07:13 PM
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 11:12 PM.

Unix & Linux Forums Content Copyright1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?