Replace space within quotes

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

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:

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:

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.

Hello SkySmart,

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

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

1 Like

Try:

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

1 Like