10-26-2008
awk help required to group output and print a part of group line and original line
Hi,
Need awk help to group and print lines to format the output as shown below
INPUT FORMAT
set echo on
set heading on
set spool on
/* SCHEMA1 */ CREATE TABLE T1;
/* SCHEMA1 */ CREATE TABLE T2;
/* SCHEMA1 */ CREATE TABLE T3;
/* SCHEMA1 */ CREATE TABLE T4;
/* SCHEMA1 */ CREATE TABLE T5;
/* SCHEMA2 */ CREATE TABLE T1;
/* SCHEMA2 */ CREATE TABLE T2;
/* SCHEMA2 */ CREATE TABLE T3;
/* SCHEMA3 */ CREATE TABLE T1;
/* SCHEMA3 */ CREATE TABLE T2;
/* SCHEMA3 */ CREATE TABLE T3;
/* SCHEMA4 */ CREATE TABLE T1;
set feedback on
set spool off
DESIRED OUTPUT
set echo on
set heading on
set spool on
connect SCHEMA1
/* SCHEMA1 */ CREATE TABLE T1;
/* SCHEMA1 */ CREATE TABLE T2;
/* SCHEMA1 */ CREATE TABLE T3;
/* SCHEMA1 */ CREATE TABLE T4;
/* SCHEMA1 */ CREATE TABLE T5;
connect SCHEMA2
/* SCHEMA2 */ CREATE TABLE T1;
/* SCHEMA2 */ CREATE TABLE T2;
/* SCHEMA2 */ CREATE TABLE T3;
connect SCHEMA3
/* SCHEMA3 */ CREATE TABLE T1;
/* SCHEMA3 */ CREATE TABLE T2;
/* SCHEMA3 */ CREATE TABLE T3;
connect SCHEMA4
/* SCHEMA4 */ CREATE TABLE T1;
set feedback on
set spool off
I am looking forward to add the connect lines where all the SQL lines start with /* SCHEMAn */ values as a consistent pattern
i was able to sort the distict values of the pattern using the below awk
nawk '/^\/\* /{dup[$2]++;if (dup[$2]>=1){name[$2]=$2}};END{for(i in name)print name[i]} ' inputfile
Output
-------
SCHEMA1
SCHEMA2
SCHEMA3
SCHEMA4
but i am not sure how to place these lines in before the patterns and skip till the next pattern is found.
Thanks in advance,
Rajan
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
how to print any required line by its line no using awk and its NR variable
for eg:
------------
121343
adfdafd
21213sds
dafadfe432
adf.adf%adf
---------------
requied o/p if give num=3 it print:
21213sds
-------------------------------------- (2 Replies)
Discussion started by: RahulJoshi
2 Replies
2. Shell Programming and Scripting
Hi @ all
I'm trying to achive to this problem,
I've a 2-column composed file as the following:
192.168.1.2 2
192.168.1.3 12
192.168.1.2 4
192.168.1.4 3
cpc1-swan1-2-3-cust123.swan.cable.ntl.com 4
192.168.1.3 5
192.168.1.2 10
192.168.1.4 8... (8 Replies)
Discussion started by: m4rco-
8 Replies
3. Shell Programming and Scripting
I've this file and need to sort the data in each group
File would look like this ...
cat file1.txt
Reason : ABC
12345-0023
32123-5400
32442-5333
Reason : DEF
42523-3453
23345-3311
Reason : HIJ
454553-0001
I would like to sort each group on the last 4 fileds and print them... (11 Replies)
Discussion started by: prash184u
11 Replies
4. Shell Programming and Scripting
Hello,
I am currently a little bit lost with my new shell-script.
I am trying to output the results of the "id"-command as a "one per line" statement. For example:
I have the user "test" with the group-memberships "group1, group2, group3". When I enter "id test" it will show me the output... (2 Replies)
Discussion started by: henryford
2 Replies
5. Shell Programming and Scripting
Hi,
My Oracle query is returing below o/p
----------------------------------------------------------
Ins trnas value
a lkp1 x
a lkp1 y
b lkp1 a
b lkp2 x
b lkp2 y ... (7 Replies)
Discussion started by: gvk25
7 Replies
6. Red Hat
Hi,
In the following output you can see the the user "richard" is a member on the team/group "developers":
# id richard
uid=10247(richard) gid=100361(developers) groups=100361(developers),10053(testers)
but in the following details of the said group (developers), the said user... (3 Replies)
Discussion started by: indiansoil
3 Replies
7. Shell Programming and Scripting
Hi,
I have an input file like this
line1
line2
line3 hello unix how are you
This is what I am expecting my output to be
line1
line2
#line3 hello unix how are you
line3 hello (3 Replies)
Discussion started by: jacobs.smith
3 Replies
8. Shell Programming and Scripting
My code is something like below.
#/bin/bash
for i in `ps -ef | grep pmon | grep -v bash | grep -v grep | grep -v perl | grep -v asm | grep -v MGMT|awk '{print $1" "$8}'`
do
echo $i
ORACLE_SID=`echo $line | awk '{print $2}'`
USERNAME=`echo $line | awk '{print $1}'`
done
=============
But... (3 Replies)
Discussion started by: tapia
3 Replies
9. Shell Programming and Scripting
Hello,
I've been trying to find the answer to this with Google and trying to browse the forums, but I haven't been able to come up with anything. If this has already been answered, please link me to the thread as I can't find it.
I've been asked to write a script that pulls a list of our CPE... (51 Replies)
Discussion started by: rwalker
51 Replies
10. UNIX for Beginners Questions & Answers
Hi All,
i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field.
i am using centos 7 Operating system.
want to read below file.
# cat /tmp/d5
NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies
LEARN ABOUT CENTOS
create_tablespace
CREATE
TABLESPACE(7) PostgreSQL 9.2.7 Documentation CREATE TABLESPACE(7)
NAME
CREATE_TABLESPACE - define a new tablespace
SYNOPSIS
CREATE TABLESPACE tablespace_name [ OWNER user_name ] LOCATION 'directory'
DESCRIPTION
CREATE TABLESPACE registers a new cluster-wide tablespace. The tablespace name must be distinct from the name of any existing tablespace in
the database cluster.
A tablespace allows superusers to define an alternative location on the file system where the data files containing database objects (such
as tables and indexes) can reside.
A user with appropriate privileges can pass tablespace_name to CREATE DATABASE, CREATE TABLE, CREATE INDEX or ADD CONSTRAINT to have the
data files for these objects stored within the specified tablespace.
PARAMETERS
tablespace_name
The name of a tablespace to be created. The name cannot begin with pg_, as such names are reserved for system tablespaces.
user_name
The name of the user who will own the tablespace. If omitted, defaults to the user executing the command. Only superusers can create
tablespaces, but they can assign ownership of tablespaces to non-superusers.
directory
The directory that will be used for the tablespace. The directory should be empty and must be owned by the PostgreSQL system user. The
directory must be specified by an absolute path name.
NOTES
Tablespaces are only supported on systems that support symbolic links.
CREATE TABLESPACE cannot be executed inside a transaction block.
EXAMPLES
Create a tablespace dbspace at /data/dbs:
CREATE TABLESPACE dbspace LOCATION '/data/dbs';
Create a tablespace indexspace at /data/indexes owned by user genevieve:
CREATE TABLESPACE indexspace OWNER genevieve LOCATION '/data/indexes';
COMPATIBILITY
CREATE TABLESPACE is a PostgreSQL extension.
SEE ALSO
CREATE DATABASE (CREATE_DATABASE(7)), CREATE TABLE (CREATE_TABLE(7)), CREATE INDEX (CREATE_INDEX(7)), DROP TABLESPACE (DROP_TABLESPACE(7)),
ALTER TABLESPACE (ALTER_TABLESPACE(7))
PostgreSQL 9.2.7 2014-02-17 CREATE TABLESPACE(7)