Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am facing some problem regarding my bash script.

Below is my bash code :


cd /root/Msgs/TESTNEW/new


var1=$(awk '(NR==30){print $2}' $file)
var2=$(awk 'NR>=38 && NR<=39' $file)
var3=${var2// /+}

curl "http://<server_ip>/power_sms/send_sms.php?username=<user>&password=<pass>phoneno=$var1&text=$var3"

This scripts purposed is for reading in a range of line in particular file(for ex:myfile.txt).Then it will put the content of the file into some variable(var1,var2). After that,the variable will be called into the curl functions.

The problems start when the content of the file have spacing in every new line.This making the curl not functioning as it do not accept white space character.I have manage to replace the spacing into plus symbols.But whenever there is new line,it will have spacing rather than having plus symbol.

Some of the output is as below:

hi+there.hopefully+you+can+get+this+email+which+are being+send+as+sms.

Can someone help me?Thanks.

share|improve this question

If you are using linux then this will substitute newlines to +

awk '{printf "%s+",$0} END {print ""}'

or with sed

sed ':a;N;$!ba;s/\n/+/g'

share|improve this answer
+1 for answering the real question – Petr Jun 13 at 12:39

You are doing this the wrong way. You need to URL-encode your text to get a truly universal solution:

When you do this and use a text like this:

This is 
a test 

you end up with this: This+is+%0d%0aa+test+%0d%0atext.

You can use curl to do this:

curl -G -d username=<user> -d password=<pass> -d phoneno=$var1 \
       --data-urlencode text@myfile.txt \

For more info, see man curl.

share|improve this answer
awk NF=NF RS= OFS=+


share|improve this answer

EDIT: For the solution with awk, you are looking for this:

awk 'BEGIN {RS=""}{gsub(/\n/,"",$0); print $0}' myfile.txt

Another method to strip all newlines from your file:

tr -d "\n"

An example:

tr -d "\n" myfile.txt > myfilewithoutnewlines.txt
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.