Unix/Linux Go Back    


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

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-11-2010   -   Original Discussion by nimo
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 Unix and Linux 08-11-2010   -   Original Discussion by nimo
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 Unix and Linux 08-11-2010   -   Original Discussion by nimo
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 Unix and Linux 08-12-2010   -   Original Discussion by nimo
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 Unix and Linux 08-12-2010   -   Original Discussion by nimo
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 Unix and Linux 08-12-2010   -   Original Discussion by nimo
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
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 03:34 AM
Replace special characters in multiple files - perl RMSoares Shell Programming and Scripting 1 09-01-2009 07:09 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



All times are GMT -4. The time now is 01:46 AM.