Thursday, 11 July 2013

[Shell Script #1] Read/Write data to/from files using shell scripts

Shell scripts are very handful in reading or writing data to/from files.
The below shell script will take input file, which contains numbers in a column and output two files, containing these numbers in following order.
Example:
input.txt
11
22
33
44
55
66

output:
output1.txt  output2.txt
11                  22
33                  44
55                  66

Here is the script file: script1.sh

  1. #!/bin/bash
  2. FILENAME=$1
  3. count=0

  4. while read LINE
  5. do
  6. let count++
  7. if [ "$count" -eq 1 ]; then
  8. echo $LINE >> "output1.txt"
  9. else
  10. count=0
  11. echo $LINE >> "output2.txt"
  12. fi
  13. done < $FILENAME



No comments:

Post a Comment

You might also like

Related Posts Plugin for WordPress, Blogger...