Register
It is currently Thu Oct 02, 2014 12:25 am

Replace characters


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Fri Jun 27, 2014 2:25 am   

Joined: Fri Jun 27, 2014 2:04 am
Posts: 6
Hello.
Here my very basic script
I have a list filled with random URLs
Example
http://www.bbc.com/news/world-europe-27266922
http://www.weeklystandard.com/articles/their-911-role_794957.html
http://www.theguardian.com/global-development/2014/jun/10/supermarket-prawns-thailand-produced-slave-labour

I need to place each URL in separate txt file with name contains the same URL (I know about filesystem limitation)
So I need to replace slashes with underscore
Code:
#!/bin/bash

#This script create a multiple txt files with names and contents, which imported from file with URLs
#echo "Usage url2txt.sh < filename_with_URL.stxt"
while read line ; do
   touch 1.txt
   echo $line > 1.txt;
   sed  's/\//_/g' $line  > $line;
   mv 1.txt $line.txt
   rm 1.txt;
done

But i stuck with sed.
If I manual sanitize file with URL - all works. File created, sanitized URL placed into file, but i need unsanitized URL in file :)


Top
 Profile  
 PostPosted: Fri Jun 27, 2014 3:43 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 549
hi,

here, sed is useless, and temporary file too :
Code:
while read -r url
do
   echo "$url"> "${url//\//_}"
done


Top
 Profile  
 PostPosted: Fri Jun 27, 2014 5:06 am   

Joined: Fri Jun 27, 2014 2:04 am
Posts: 6
Thanks. It works.
And i figured out, that #!/bin/bash and #!/bin/sh have different behavior in this case
With #!/bin/sh - was "Bad substitution" :)

And if I want to replace : from URI to _ also - how change the string?


Top
 Profile  
 PostPosted: Fri Jun 27, 2014 9:53 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 549
using bash,
Code:
echo "$url" > "${url//[\/-]/_}"

strictly POSIX shells, indeed, don't have this type of parameters expansion.

this is POSIX:
Code:
urlecho "$url" > "$(echo "$url" | sed 's/[/-]/_/g')"


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP