title: How to output field padding in file Scala spark?
body: <p>I have a text file. Now, I want output field padding in file as <code>Exp1</code> &amp; <code>Exp2</code>.
  What should I do?
  This is my input:</p>
  
  <pre><code>a
  a a
  a a a
  a a a a
  a a a a a
  </code></pre>
  
  <p>Exp1. Fill the remaining fields with the <code>-</code> character when each record in the file does not fit into the <code>n=4</code> field.</p>
  
  <pre><code>a _ _ _
  a a _ _
  a a a _
  a a a a
  a a a a a
  </code></pre>
  
  <p>Exp2. Same as above. Delete the fields after the <code>n=4</code> field when the number of fields in the record exceeds <code>n</code>.</p>
  
  <pre><code>a _ _ _
  a a _ _
  a a a _
  a a a a
  a a a a
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>val df = spark.read.text("data.txt")
  val result = df.columns.foldLeft(df){(newdf, colname) =&gt; 
     newdf.withColumnRenamed(colname, colname.replace("a", "_"))
  } 
  result .show 
  </code></pre>