While using vi, add marks to work with a chunk of lines.
To replace beginning of all lines, between the (inclusive) marks a and b, with ZZZ; we could write
:'a,'bs/^/ZZZ/
Where a has lower line number than b.
Thursday, July 29, 2010
AWK - IF OR condition
OR operator : ||
AND operator: &&
awk -F, '{if ($1=="abc" || $1=="abd") print $0}' InputFile
-F,
InputFile is comma-separated (Field separator F is comma)
if ($1=="abc" || $1=="abd") print $0
Select and print entire record ($0) when first field ($1) is either "abc" or "abd"
AND operator: &&
awk -F, '{if ($1=="abc" || $1=="abd") print $0}' InputFile
-F,
InputFile is comma-separated (Field separator F is comma)
if ($1=="abc" || $1=="abd") print $0
Select and print entire record ($0) when first field ($1) is either "abc" or "abd"
Saturday, May 15, 2010
Sort on a field
empfile
12345,Mary J,HR
34512,J Smith,Admin
34700,A Ryan,Admin
34900,B Wilson,HR
59000,C Diaz,HR
We want to sort the empfile by department (3rd field)
$sort -t, -k3 empfile
34512,J Smith,Admin
34700,A Ryan,Admin
12345,Mary J,HR
34900,B Wilson,HR
59000,C Diaz,HR
Option -t is to indicate field separator which here is comma
Option -k is the key indicating the position to sort on
12345,Mary J,HR
34512,J Smith,Admin
34700,A Ryan,Admin
34900,B Wilson,HR
59000,C Diaz,HR
We want to sort the empfile by department (3rd field)
$sort -t, -k3 empfile
34512,J Smith,Admin
34700,A Ryan,Admin
12345,Mary J,HR
34900,B Wilson,HR
59000,C Diaz,HR
Option -t is to indicate field separator which here is comma
Option -k is the key indicating the position to sort on
Join two files based on a column
empfile
12345,Mary J,HR
34512,J Smith,Admin
34700,A Ryan,Admin
34900,B Wilson,HR
59000,C Diaz,HR
mgrfile
12345,HR
34700,Admin
So if we need to get name of the managers for each department then we would need to join the mgrfile with empfile on employee number.
$join -t , -o '2.2 2.3' mgrfile empfile
Mary J,HR
A Ryan,Admin
Option -t is to specify the field separator in the files.
Option -o is to list the fields that we need in the output. "2.3" means 3rd field of 2nd file.
By default join command joins on the first field of the files. So if we needed to get the manager for each employee, we would join the two files on department type.
To do this correctly both the files should be sorted** on the field being used for join.
$join -t, -13 -22 -o '1.2 1.3 2.1' empfilesorted mgrfilesorted
J Smith,Admin,34700
A Ryan,Admin,34700
Mary J,HR,12345
B Wilson,HR,12345
C Diaz,HR,12345
We have two new entries on the cmd above : -13 and -22. These can also be written as -1 3 and -2 2. These indicates the fields we are joining on.
-1 3 means 3rd field of 1st file
**See here on how to sort on a field.
12345,Mary J,HR
34512,J Smith,Admin
34700,A Ryan,Admin
34900,B Wilson,HR
59000,C Diaz,HR
mgrfile
12345,HR
34700,Admin
So if we need to get name of the managers for each department then we would need to join the mgrfile with empfile on employee number.
$join -t , -o '2.2 2.3' mgrfile empfile
Mary J,HR
A Ryan,Admin
Option -t is to specify the field separator in the files.
Option -o is to list the fields that we need in the output. "2.3" means 3rd field of 2nd file.
By default join command joins on the first field of the files. So if we needed to get the manager for each employee, we would join the two files on department type.
To do this correctly both the files should be sorted** on the field being used for join.
$join -t, -13 -22 -o '1.2 1.3 2.1' empfilesorted mgrfilesorted
J Smith,Admin,34700
A Ryan,Admin,34700
Mary J,HR,12345
B Wilson,HR,12345
C Diaz,HR,12345
We have two new entries on the cmd above : -13 and -22. These can also be written as -1 3 and -2 2. These indicates the fields we are joining on.
-1 3 means 3rd field of 1st file
**See here on how to sort on a field.
Friday, May 14, 2010
PL /SQL Data Type - Constant
PL SQL has data type called constant. As the name implies, the field with this data type is immutable after declaration.
In the code below, un-commenting the meow line results in error.
In the code below, un-commenting the meow line results in error.
declare
const_ruchi constant varchar2(5) :='Ruchi' ;
begin
--const_ruchi :='meow';
dbms_output.put_line('Schema Owner is ' || const_ruchi) ;
end;
Thursday, May 13, 2010
Hello PL/SQL
--Started learning PL/SQL using this simple undaunting tutorial.
declare
luckynbr number(2);
abcdate date;
begin
select sysdate into abcdate from dual;
select dbms_random.value(0,100) into luckynbr from dual;
dbms_output.put_line('Your lucky number on ' || abcdate || ' is ' || luckynbr );
end;
/
Tuesday, May 11, 2010
Unix Fold
Fold is not just a formatting tool for word wrapping but can come in handy for text editing as well.
check this code
cat apple | fold -1 | sort | sed -n '/^[aeiouAEIOU]/p'
When we pass the content of apple to fold command, it breaks the content to 1 character per line as defined by the width parameter of fold.
$cat apple
apple
$cat apple | fold -1 (use fold -w1 for ksh)
a
p
p
l
e
And when we add sort to the above command and look lines beginning for vowels in
$cat apple | fold -1 | sort | sed -n '/^[aeiouAEIOU]/p'
a
e
Isn't that neat.
Base code from here.
check this code
cat apple | fold -1 | sort | sed -n '/^[aeiouAEIOU]/p'
When we pass the content of apple to fold command, it breaks the content to 1 character per line as defined by the width parameter of fold.
$cat apple
apple
$cat apple | fold -1 (use fold -w1 for ksh)
a
p
p
l
e
And when we add sort to the above command and look lines beginning for vowels in
$cat apple | fold -1 | sort | sed -n '/^[aeiouAEIOU]/p'
a
e
Isn't that neat.
Base code from here.
Subscribe to:
Posts (Atom)