Saturday, April 3, 2010

A quick perl script to solve that same problem

#!/usr/bin/perl


## check for existence of a file ##
if (-e "./test2.txt") {
print "File exists! \n";
}else {
print "File does not exist";
}


print "\n ";

#use strict;
open(MYDATA, "test2.txt") or
die("Error: cannot open file 'data.txt'\n");
my $line;
my $lnum = 1;

while( $line = ){
chomp($line);
# chop;
($fee, $fi, $fo, $fum) = (split(/,/, $line));
print "COLUMN 2 is: $fi \n ";
if ( $fi =~ /^\./ ) {
print "found one \n";
}
print "$lnum| $line\n";
$lnum++;
}


close MYDATA;

More on records that have a period at the beginning

#!/bin/bash

## one possible method to find all of the columns that start with period ##
cat $1 | cut -d"|" -f2 | grep '^\.' > badrecords.txt

## another possible method
cat $1 | cut -d"|" -f2 | grep '^\.' > badrecords.txt

## find all records that have a pipe followed by a period
grep '|\.' test1.txt
~

### the quick sed command to make global switches ##
:.,$s/|/,|g ## translates all pipes to comas
:.,$s/,/|/g ## translates all comas to pipes:w

Thursday, April 1, 2010

Finding records in which some words start with a period

This is a real quickie. How do you find any record in which some of the words begin with a "."?
Here's the answer:
grep '.\.' file.txt.

If you want to loop through more than one file try this:

#!/bin/bash
for file in `ls *.txt`
do
echo "searching $file"
grep '.\.' $file
done


to find all of the records that don't have words starting with a "." try
grep -v '.\.' file.txt