Linux – remove line break using AWK

awkbashlinux

I am facing some problem regarding my bash script.

Below is my bash code :

#!/bin/bash

cd /root/Msgs/TESTNEW/new

file=myfile.txt

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.

Best Answer

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'