Need help in creating a file in required format form another existing file

I have a text file with contents like this:

a,b,c,
d~e,f,g,h~i,j
,k,l,m~n,o,p,q~

I need to convert this file into this format unix shell script commands:

a,b,c,d~
e,f,g,h~
i,j,k,l,m~
n,o,p,q~

as you may have noticed, I need to retain the ~ signs at the end.

Any help is greatly appreciated.

Any attempt from your side?

---------- Post updated at 21:20 ---------- Previous update was at 21:13 ----------

Anyhow, try

awk '{X=X $0} END{gsub(/~/,"&\n",X); printf "%s", X}' file
a,b,c,d~
e,f,g,h~
i,j,k,l,m~
n,o,p,q~
1 Like

I tried these steps:

  1. Used the below command to roll all the lines into a single line
sed -i -e :a -e 'N;s/\n//;ta' input_file.txt 
  1. Then converted the ~ to a new line
sed -i 's/~/~\n/g' input_file.txt 

This approach is working, but I am concerned about the record size after the first step - if my initial filw size is huge, then would I run into a probelm if I roll up all the lines into a single line?

Or, for long lines:

awk '/^\n$/ {next} {gsub(/\n/,""); $0=$0"~"} 1' RS="~"  file

---------- Post updated at 21:47 ---------- Previous update was at 21:44 ----------

or

awk '{gsub(/\n/,"")} $0 {print $0 RS}' RS="~"  file
1 Like