Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Thursday, February 12, 2009

Join two lines in sed

I was trying to load a CSV file into an Oracle database using SQL*Loader today and came across an issue where some of the fields contained CR within the data. Luckily each field is enclosed with double quotes ". Knowing this I was able to run the folling sed command which finds any lines that do not end with a double quote and joins them to the next line.

sed -e :a -e '/[^\"]$/N; s/[^\"]\n//; ta' _
old_filename > new_filename

Just thought I'd share - someone might find it useful

Monday, November 10, 2008

Extract a line from a file

There are many times I need to extract a single line (or possibly group of lines) from a file (Usually when things have gone wrong).

The easiest way to do this is as follows:

awk 'NR==lineNumber{print;exit;} filename

Replacing:

  lineNumber - with the line you wish display

  filename - with the name of your file





I have also seen the following suggested:

sed 'lineNumberq;d' filename

Replacing:

  lineNumber - with the line you wish display

  filename - with the name of your file


After performance testing on a large file (~10,000,000+ lines) the awk command was found to be faster.

ls: Arg list too long

Ever seen the error "arg list too long" when issuing an ls with a wildcard? The simple way round this is as follows:


find . -name "regexp" -exec ls -l '{}' +

Where:

regexp is the regular expression you are searching for