Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

drop trailing spaces?

Status
Not open for further replies.

awkusr345

Technical User
Jul 2, 2000
1
US
I've tried to design a filter that drops trailing spaces (if any) from an input line.<br><br>for example, I have an imput similar to &quot;this is a test.&nbsp;&nbsp;&quot; and convert it to &quot;this is a test.&quot;&nbsp;&nbsp;I alredy tried using something like tr '&nbsp;&nbsp;' '??' but tr replaces all spaces =-/.<br><br>I'm only looking for an example in awk at the moment.<br><br>Thanks.<br>
 
awk ' {print $1,$2,$3,$4}' inputfile &gt; outputfile<br><br>or you could use the cut command.<br> <p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>
 
nawk 'BEGIN{FS=&quot;&quot;}{gsub(/\ +$/,&quot;&quot;)} {print}' infile &gt; outfile<br><br>This will work if you have nawk, gawk, or<br>MKS awk.&nbsp;&nbsp;Standard awk does not have the<br>gsub function that you need to replace any<br>multiple spaces using a regular expression<br>to limit which spaces get deleted.<br><br><br>Hope this helps you.<br><br><br> <p>flogrr<br><a href=mailto:flogr@yahoo.com>flogr@yahoo.com</a><br><a href= > </a><br>
 
There is a sed command for that:<br>Trailing space: sed 's/[ \t]*$//'<br>Leading space: sed 's/^[ \t]*//'<br>Both leading and trailing:<br>sed 's/^[ \t]*//;s/[ \t]*$//'<br><br>Hope that helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top