Counting lines, words and characters with wc

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Linux, Shell, Utilities | No comments yet.

There is a simple Linux utility for counting characters, lines and words in files called wc. This allows you to give a file as an argument or a stream and get the counts of the aforementioned items.

wc <filename>

or

cat <filename> | wc

The options that wc accepts

-c,–bytes print the byte counts
-m,–chars print the character counts
-l,–lines print the line counts
-L,–max-line-length print the length of the longest line
-w,–words print the word counts

The ouput with no arguments will be three numbers (words,lines,characters. Otherwise the the number will reflect the argument you have passed.

And if you pass multiple filenames the output displays counts for all files and totals.

 >wc -l HelloWorld.java sample.txt
    8 HelloWorld.java
   25 sample.txt
   33 total

Tags: ,

HTML: Basic HTML5 page

digg del.icio.us TRACK TOP
By Eric Downing | Filed in html | No comments yet.

This is where I start coding my web pages. This is a simple framework for an HTML5 web page. The DOCTYPE has been simplified and is supported by all major browsers even if they do not fully support the current HTML5 tags.

<!DOCTYPE html>
<html>
<head>
<title>My Sample HTML5 web page</title>
</head>
<body>
This is my sample web page.
</body>
</html>

Tags: ,

Postgres: Get table and field names from a given schema

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Programming, SQL | No comments yet.

I work with a large Postgres install that has many hundreds of schemas. there are times that I need to find a list of tables and fields from a specific schema for a web application that I am developing.

This will return the list of table names within the giving schema:

select relname 
from pg_stat_user_tables 
WHERE schemaname='schema1';

Then to get the given field names from the table that I have chosen:

select column_name 
from information_schema.columns 
where table_name='mytable';

Tags:

How to include Javascript on a web page

digg del.icio.us TRACK TOP
By Eric Downing | Filed in html, Programming, Web Design | No comments yet.

There are three ways to include Javascript files on a website. You can use scripts placed in the head element, scripts placed within the body element or you can include an external file.

The <script> tag is used to contain the Javascript code when you are placing it within the web page.

Example of code within the <head> tag:

 <!DOCTYPE html>
<html>
<head>
<title>Javascript in Head tag</script>
<script language="javascript" type="text/javascript" >
     document.write("Hello, World!");
</script>
</head>
<body>
</body>
</html>

When you are expecting to output directly into the page it is helpful to place the script in the
<body> tag.

<!DOCTYPE html>
<html>
<head>
<title>Javascript in Body tag</script>
<script>

</script>
</head>
<body>
<script language="javascript" type="text/javascript" >
  document.write("Hello, World!");
</script>
</body>
</html>

And when you are including an external script like jQuery or a custom script, you will use an
tag in the head with a src identifier to specify the file. Where you might reference the file in an onload or some other scripting within the page.

<!DOCTYPE html>
<html>
<head>
<title>Javascript in External file</script>
<script language="javascript" type="text/javascript" src="somefile.js" > </script>
</head>
<body onload="callsomefile();" >
</body>
</html>

There is also the possibility that someone will have turned off Javascript and this is where the <noscript> tag can be used to either display the site designed with no scripting involved or inform the user that they cannot use the site unless Javascript is enabled.

Tags: , ,

Hello world!

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Uncategorized | No comments yet.

Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!

Awk: Using awk to Truncate fields

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Linux, Utilities | No comments yet.

Working with data feeds is sometimes frustrating when you have over 50 columns and are trying to find the field that is too long for your insert into your database system of choice. I have found processing the files with different Linux utilities useful and one that I regularly use it “awk”. I was trying to get the substring of all fields in a product feed the other day and used the following trick.

awk -F 't' '{for(i = 1;i<NF;i++) printf("%s,",substr($i,0,50));print;};

The for loop iterates over all the fields separated by a tab in each line and the prints out the field with a length of 50 characters or less. The extra print is for the newline at the end of each line.

Tags: ,

Screen: Send a Ctrl-A to a terminal

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Linux, Utilities | No comments yet.

When you are using the Linux tool screen, there are times
that I need to send a ctrl-a character. This is a special command key in
screen that drives most of the other commands. But I use the ctrl-a to go
to the beginning of some of my commands and in order for the terminal to
get a ctrl-a you need to use the following key sequence:

ctrl-a  a

So to explain, you need to hold ‘control’ or ‘ctrl’ key and the ‘a’ key together
and then release the ‘ctrl’ key and press ‘a’ key and it will send a ctrl-a to
the terminal.

Tags: ,

grep -e :searching multiple terms

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Software, Utilities | No comments yet.

When you have to process log files, it can be very useful to highlight the terms you are seeking. One way to highlight a term is to use grep with the –color option. The drawback to using this method is that if you are chaining the grep command after another command then the last grep is all that would be highlighted.
I found that you could use the following to highlist all terms:

grep --color -e 'search|search2' 

So to break down the command line, first we have the grep command followed by the –color option which (if your terminal allows it) will highlight the search term within the screen results. This means that if you put this into a file, then the color highlighting will be lost. The ‘-e’ option states that the next argument is going to be a regular expression based argument.
The full syntax of regular expressions is beyond the scope of this article but basically is a pattern to match strings. The reason I am using a -e option is to support the ‘|’ option that allows me to search for more than one term. The reason that the pipe character is escaped is that otherwise it would be considered as a character to search for instead of a separator. This also will color either term within the output. So I am searching for search or search2 anywhere within the files that I pass in.

My Favorite Vi(m) commands

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Editors, Software, Utilities | No comments yet.

I have found myself using Vim as my Linux editor of choice.  It is not dependent on X11 being available and I can edit pretty much anything fairly quickly.  I have been using a Redhat flavored linux(Redhat,Fedora, Centos) for quite sometime and because I couldn’t guarantee that Emacs would be installed I just got used to using Vi.

When I first attempted to use Vi after using Emacs from my college days, I was initially very frustrated with modes and key sequences.  But after a very short time I found I could edit files very rapidly and make changes quite easily.

Quick key list:

Esc The escape key is the way to get out of editor mode and clear
Ctrl-c will exit the editor mode
:q! Exits without saving
u Undo
cw changes the following word
dd deletes the line
D deletes the line to the end
J removes the end of the line
:e or :edit reload the file from disk

1) The thing that I do quite often is to replace all Ctrl-M (Windows Returns) within a file.  (Yes, I could do this with a call to dos2unix, but this is a Vi list).   I found the following key sequence works quite well.

:%s/Ctrl-v<enter>//g

breaking this sequence down

2) Another sequence I use quite often is to reformat source code.

:1 <enter> <shift-v> <shift>-g =

breaking the sequence down

:1<enter>   – states go to the first line of the file

<shift>-v    – Turns on visual line mode which selects the entire current line

<shift>-g    -Makes the cursor go to the last line in the file and with the last command

selects the entire file

=                   – Tells the editor to reformat the selection area

This page will be updated as I figure out more useful commands. This is only a small amount of the power of VI!

Awk: Separating delimited files on the command line

digg del.icio.us TRACK TOP
By Eric Downing | Filed in Linux, Scripting, Utilities | No comments yet.

I have had to process data files from customers that have come in as delimited files. The files are usually in CSV formats and the delimiters might change depending on the content of the files. Sometimes the delimiters are usually tabs”t”, pipes “|”, comma “,” or semi-colon “;”. A useful tool for processing this type of file is to use awk.

Example command line:

awk -F 't' '{OFS="|"; if (NR != 1) print $3,$2,$1}'

The ‘-F’ argument is followed by the character that you have the file separated on. The example above uses a tab escape sequence ‘t’ but you could have any character. One potential problem is if the data contains the same character that you are attempting to split on. As in a comma separated list using commas within a long text sequence.
The Second sequence of quoted text above starts the processing of the file. The if statement uses the NF keyword which stands for Number of Row. This tells the script that it should not fire for the first row. The print statement allows you to pick and choose the columns(numbered from 1) to use or allows you to reorder them.
The ‘OFS’ part of the command line that helps with the print statement describes the Output Field Separator. In the case above it is using a pipe character ‘|’ to separate the text as it is being printed out. This allows you comma separate the fields that you want instead of having to use a more sophisticated printf statement.

The input file is of the form

Label,name,address

And the output is going to be of the form:

address,name,Label