Register
It is currently Thu Apr 24, 2014 12:16 am

Substitute newline with double quotes


All times are UTC - 6 hours


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 PostPosted: Tue Mar 02, 2010 6:15 am   
User avatar

Joined: Sun Nov 01, 2009 8:59 am
Posts: 23
Location: Try to guess!
Hi, i'm having a very hard time trying to substitute \n with " " (i.e, substitute newline with double quotes-space-double quotes). Call it "escaping hell" if you get the pun :))
I've been through many man pages and google searches but still i can't get this straight. I tried perl, tr, bare regex - you name it!
This command is the best working yet:
Code:
cat pathlist | tr '\n' '" "' > pathlist2

but it just adds one single double quote:
Code:
/home/e633/Desktop/uni
/home/e633/Desktop/network
/home/e633/Desktop/SSH config notes
/home/e633/Desktop/apache config notes

becomes:
Code:
/home/e633/Desktop/uni"/home/e633/Desktop/network"/home/e633/Desktop/SSH config notes"/home/e633/Desktop/apache config notes"

instead i want it to be:
Code:
"/home/e633/Desktop/uni" "/home/e633/Desktop/network" "/home/e633/Desktop/SSH config notes" "/home/e633/Desktop/apache config notes"

So i can use this string for creating a 7zip archive. BTW that newline-separated string is from $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS, as i'm writing a nice user-friendly ubuntu script.


Top
 Profile  
 PostPosted: Wed Mar 03, 2010 5:09 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Most likely it is doable in Bash... but this is alot easier done in perl. So I'm gonna go offtrack a bit.

Code:
ls | perl -e 'foreach(split(/\n/,join("",(<>)))){print "$_\" \""}'

Code explained:
It basically reads STDIN, which in perl gives you an array, joins the array, then splits it by \n (which removes it) and then just prints every element in the array.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Wed Mar 03, 2010 12:23 pm   
User avatar

Joined: Sun Nov 01, 2009 8:59 am
Posts: 23
Location: Try to guess!
Whoa... that one liner is impressive to say the least! Thanks a lot i'm gonna figure it out and try it ASAP!
(that's surely gonna cost me some bad headache) x_x


Top
 Profile  
 PostPosted: Wed Mar 03, 2010 5:04 pm   
User avatar

Joined: Sun Nov 01, 2009 8:59 am
Posts: 23
Location: Try to guess!
Almost there! Thought you'd like to know it's not yet correct:

From my shell:
e633@mypc:~/Desktop/network$ ls | perl -e 'foreach(split(/\n/,join("",(<>)))){print "$_\" \""}'
C05.PDF" "C 06.PDF" "C07.PDF" "C 0 8.PDF" "C09.PDF" "C10.PDF" "C11.PDF" "C12.PDF" "C13.PDF" "C 14.PDF" "C15.PDF" "C16.PDF" "C17.PDF" "C19.PDF" "C20.PDF" "C21.PDF" "checklist.doc" "INDEX.PDF" "[/color]
Red parts are wrong , output should begin and end with a single double quote :))

I'm trying to correct it by myself but if you're surely faster than me!


Top
 Profile  
 PostPosted: Sat Mar 06, 2010 12:25 am   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
Ahh... the magic of perl one-liners. This should fix it:
Code:
ls | perl -e 'foreach(split(/\n/,join("",(<>)))){print "\"$_\" "}'


- thobbs


Top
 Profile  
 PostPosted: Sat Mar 06, 2010 3:55 pm   
User avatar

Joined: Sun Nov 01, 2009 8:59 am
Posts: 23
Location: Try to guess!
Found a less obscure way for that:

Code:
#This is an excerpt. I'm processing each argument one by one:
for (( i = 1; i <= $argsN; i++ )); do
   export params="$params\"$1\" "
   shift
done
length=${#params}
let "length -= 1"
params=${params:0:$length}     #Some final adjustment...
#Now $params contains a nice double-quote separated list of files.


EDIT: i've just realized $1 contains just the filename and not the absolute path. So i'm gonna need $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS and that ominous perl enchantment. :B
Just kidding, thanks a lot everybody! (again :) )


Top
 Profile  
 PostPosted: Mon Mar 15, 2010 2:07 am   

Joined: Mon Mar 15, 2010 1:53 am
Posts: 1
Code:
-bash-3.2$ cat t
/home/e633/Desktop/uni
/home/e633/Desktop/network
/home/e633/Desktop/SSH config notes
/home/e633/Desktop/apache config notes
-bash-3.2$ while read line; do echo -n "\"$line\" "; done < t
"/home/e633/Desktop/uni" "/home/e633/Desktop/network" "/home/e633/Desktop/SSH config notes" "/home/e633/Desktop/apache config notes"


does this suit your requirement?

Jemshad


Top
 Profile  
 PostPosted: Sat Apr 24, 2010 7:40 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 657
Location: Des Moines, Iowa
Just got to love how simple that was :)


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 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