Substitute newline with double quotes

sed & awk, two text manipulation programs that are mainstays of the UNIX programmer's toolbox.

Substitute newline with double quotes

Postby e633 » Tue Mar 02, 2010 6:15 am

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: Select all
cat pathlist | tr '\n' '" "' > pathlist2

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

becomes:
Code: Select all
/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: Select all
"/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.
User avatar
e633
 
Posts: 16
Joined: Sun Nov 01, 2009 8:59 am
Location: Try to guess!

Re: Substitute newline with double quotes

Postby fredrik.eriksson » Wed Mar 03, 2010 5:09 am

Most likely it is doable in Bash... but this is alot easier done in perl. So I'm gonna go offtrack a bit.

Code: Select all
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
fredrik.eriksson
 
Posts: 221
Joined: Mon Nov 17, 2008 7:25 am

Re: Substitute newline with double quotes

Postby e633 » Wed Mar 03, 2010 12:23 pm

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
User avatar
e633
 
Posts: 16
Joined: Sun Nov 01, 2009 8:59 am
Location: Try to guess!

Re: Substitute newline with double quotes

Postby e633 » Wed Mar 03, 2010 5:04 pm

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!
User avatar
e633
 
Posts: 16
Joined: Sun Nov 01, 2009 8:59 am
Location: Try to guess!

Re: Substitute newline with double quotes

Postby thobbs » Sat Mar 06, 2010 12:25 am

Ahh... the magic of perl one-liners. This should fix it:
Code: Select all
ls | perl -e 'foreach(split(/\n/,join("",(<>)))){print "\"$_\" "}'


- thobbs
User avatar
thobbs
 
Posts: 73
Joined: Sat Jun 13, 2009 8:53 pm
Location: Texas!

Re: Substitute newline with double quotes

Postby e633 » Sat Mar 06, 2010 3:55 pm

Found a less obscure way for that:

Code: Select all
#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 :) )
User avatar
e633
 
Posts: 16
Joined: Sun Nov 01, 2009 8:59 am
Location: Try to guess!

Re: Substitute newline with double quotes

Postby m4n » Mon Mar 15, 2010 2:07 am

Code: Select all
-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
m4n
 
Posts: 1
Joined: Mon Mar 15, 2010 1:53 am

Re: Substitute newline with double quotes

Postby crouse » Sat Apr 24, 2010 7:40 pm

Just got to love how simple that was :)
User avatar
crouse
Site Admin
 
Posts: 561
Joined: Sun May 15, 2005 9:36 pm
Location: Des Moines, Iowa


Return to Sed & Awk

Who is online

Users browsing this forum: No registered users and 1 guest