Archive

Archive for April, 2012

Text file lines manipulation.

2012-04-24 Leave a comment

A few simple little ruby scripts that will do some text file manipulation. If you couldn’t tell I was working on a project that required all these.

Convert all lines in a text file to lowercase (downcase) and save them in a new file, I could have saved them to the same file (see remove line number below) but I wanted to preserve the original file as well.

def fileToLower(inFile, outFile)
    File.open(outFile, 'a') do |fout|
        File.open(inFile) do |fin|
            fin.each_line do |line|
                fout.puts line.downcase
            end
        end
    end
end

Remove a specific line number from a text file.

def removeLine(fileName, lineNum)
    lines = File.readlines(fileName)
    count = 1
    File.open(fileName, "w") do |fout|
        lines.each do |line|
            fout.puts(line) unless count == lineNum
            count += 1
        end
    end
end

Get a single line from a text file.

def getSingleLine(fileName, lineNum)
   File.open(fileName) do |file|
      curLine = 1
      file.each_line do |line|
         return line if lineNum == curLine
         curLine += 1
      end
   end
end
Categories: Programming, Ruby

Basename Changer from listed text file

2012-04-18 Leave a comment
#!/bin/bash
#####
## Program:
##    Basename Changer from List
## Author:
##    Kyle Rizzo
##    lifeforce0 {at} gmail {dot} com
##    https://lifeforce4.wordpress.com
## Summary:
##    
##    
##    
#####

## Coloring Schemes
NC='\033[0;37m'
RED='\033[1;31m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
CYAN='\033[1;36m'
WHITE='\033[1;37m'

files=($(ls *.mp3))
i=0
## read the file namelist.txt which has on each line
## a different file in the order that the users wishes
## the files located 
cat namelist.txt | while read j

## A loop to cycle though the 
do
    echo mv "${files[ $i ]}" "$j"
    (( i = i + 1 ))
done
Categories: Bash, Linux, Programming

Read lines from a file and split by delimiter.

2012-04-18 Leave a comment

This reads a file line by line and then will split the lines based on the delimiter in this case a tab.

#!/usr/bin/perl
use strict;
use warnings;

## Open the file or exit with an error
open FILE, 'exampleFile.txt' or die $!;
## Make $line equal to one line in the file at a time until the end
foreach my $line (<FILE>)
{
    ## remove the newline from $line.
    chomp($line);
    
    ## Split the line with a regex (which is just the tab char)
    my @lineElements = split(/\t/, $line);
    
    ## Loop through the lineElements array and print each one
    foreach my $element (@lineElements)
    {
        print "$element\n";
    }
}
Categories: Perl, Programming

CentOS 6 Single User Auto Login

2012-04-12 4 comments

To have Centos auto login to Gnome with a single user account make the following modification.

Add these two lines in /etc/gdm/custom.conf under the [daemon] section.

AutomaticLoginEnable=true
AutomaticLogin=<accountName>
Categories: Linux