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.

Replace space within quotes

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago   -   Original Discussion by SkySmart
SkySmart SkySmart is offline
Registered User
 
Join Date: Dec 2006
Last Activity: 19 November 2017, 10:22 AM EST
Posts: 897
Thanks: 735
Thanked 3 Times in 3 Posts
Replace space within quotes

i have an output that i receive and it looks like this:


Code:
echo '/var/FTPROOT/px/sci/archive/20171102070057904-DY_DC04_Daily Inventory Sync-en-us.csv' '/var/FTPROOT/px/sci/archive/20171102070058291-DY_DC07_Daily Inventory Sync-en-us.csv'

what i want to do is replace the spaces in the file names with asterisks "*".

end result should be:


Code:
echo '/var/FTPROOT/px/sci/archive/20171102070057904-DY_DC04_Daily*Inventory*Sync-en-us.csv' '/var/FTPROOT/px/sci/archive/20171102070058291-DY_DC07_Daily*Inventory*Sync-en-us.csv'

i tried with this:


Code:
echo '/var/FTPROOT/px/sci/archive/20171102070057904-DY_DC04_Daily Inventory Sync-en-us.csv' '/var/FTPROOT/px/sci/archive/20171102070058291-DY_DC07_Daily Inventory Sync-en-us.csv' | sed 's~ ~*~g'

but of course, this will replace all the spaces with asterisks. which is not what i want. i only want the spaces within the single quotes to be replaced with an asterisk.
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago   -   Original Discussion by SkySmart
RavinderSingh13 RavinderSingh13 is offline Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 23 November 2017, 2:09 AM EST
Location: Chennai
Posts: 2,670
Thanks: 588
Thanked 1,272 Times in 1,145 Posts
Hello SkySmart,

Could you please try following and let me know if this helps.(test with only provided sample).

Code:
echo '/var/FTPROOT/px/sci/archive/20171102070057904-DY_DC04_Daily Inventory Sync-en-us.csv' '/var/FTPROOT/px/sci/archive/20171102070058291-DY_DC07_Daily Inventory Sync-en-us.csv' | awk -v RS=" /" -F"/" '{gsub(/ /,"*",$NF);print}' ORS=" /" OFS="/"
OR
echo '/var/FTPROOT/px/sci/archive/20171102070057904-DY_DC04_Daily Inventory Sync-en-us.csv' '/var/FTPROOT/px/sci/archive/20171102070058291-DY_DC07_Daily Inventory Sync-en-us.csv' |
awk -v RS=" /" -F"/" '{gsub(/ /,"*",$NF);print}' ORS=" /" OFS="/"

Thanks,
R. Singh
The Following User Says Thank You to RavinderSingh13 For This Useful Post:
SkySmart (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago   -   Original Discussion by SkySmart
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 November 2017, 8:17 PM EST
Location: Amsterdam
Posts: 11,625
Thanks: 518
Thanked 3,381 Times in 2,980 Posts
Try:

Code:
echo "'/var/FTPROOT/px/sci/archive/20171102070057904-DY_DC04_Daily Inventory Sync-en-us.csv' '/var/FTPROOT/px/sci/archive/20171102070058291-DY_DC07_Daily Inventory Sync-en-us.csv'" | 
awk '{for(i=2; i<=NF; i+=2) gsub(" ","*",$i)}1' FS=\' OFS=\'

You need the double quotes around the line with the single quoted paths, otherwise the single quotes will be interpreted and removed by the shell and never reach sed or awk.


---
That is, if you need to specify the paths on a single line

Last edited by Scrutinizer; 1 Week Ago at 03:22 AM..
The Following User Says Thank You to Scrutinizer For This Useful Post:
SkySmart (1 Week Ago)
Sponsored Links
Reply

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
Replace Double quotes within double quotes in a column with space while loading a CSV file mlavanya Shell Programming and Scripting 6 05-12-2015 01:05 AM
Replace delimeter between quotes with \| using sed or awk BrahmaNaiduA Shell Programming and Scripting 4 05-04-2015 05:55 PM
Replace newline character between a double quotes to a space ajahuja Shell Programming and Scripting 8 08-02-2012 11:43 AM
Replace a value in the file that start with quotes chandu123 UNIX for Advanced & Expert Users 6 06-26-2012 05:44 PM
Remove two delimiters, space and double quotes SteveDWin UNIX for Dummies Questions & Answers 4 01-11-2012 08:35 PM



All times are GMT -4. The time now is 03:42 AM.