Wednesday, November 19, 2014

Sed Shell Script To Remove All Blank Spaces From a Text File

sed (Stream EDitor) a Unix utility which parses text files and implements a programming language which can apply textual transformations to such files. It reads input files line by line (sequentially), applying the operation which has been specified via the command line (or a sed script), and then outputs the line.
The following example shows a typical use of sed:
  1. #!/bin/bash
  2. # Write a shell script which reads the contents in a text file and removes
  3. # all the blank spaces in them and redirects the output to a file.
  4. # -------------------------------------------------------------------------
  5. # Copyright (c) 2001 nixCraft project <http://cyberciti.biz/fb/>
  6. # This script is licensed under GNU GPL version 2.0 or above
  7. # -------------------------------------------------------------------------
  8. # This script is part of nixCraft shell script collection (NSSC)
  9. # Visit http://bash.cyberciti.biz/ for more information.
  10. # -------------------------------------------------------------------------
  11. out="output.$$"
  12. echo -n "Emter a file name : "
  13. read file
  14.  
  15. if [ ! -f $file ]
  16. then
  17. echo "$file not a file!"
  18. exit 1
  19. fi
  20.  
  21. sed -e 's/[\t ]//g;/^$/d' $file > $out
  22. echo "Output written to $out file"

0 comments: