awk help required to group output and print a part of group line and original line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk help required to group output and print a part of group line and original line
# 1  
Old 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
# 2  
Old 10-26-2008
This should work for you:
Code:
awk '/SCHEMA/ && a != $2{a=$2;print "connect "a}1' file

# 3  
Old 10-26-2008
Worked perfect.. Thanks a lot....
# 4  
Old 10-27-2008
nawk '{
if(index($2,"SCHEMA")!=0 && temp!=$2)
{
print "connect",$2
temp=$2
print $0
}
else
print
}' file
# 5  
Old 10-27-2008
Hi Danmero! i got a doubt here what does '1' do in the below code, what all i can use in place of 1. I am using solaris 10, and the above is not working in my system.
"awk '/SCHEMA/ && a != $2{a=$2;print "connect "a}1' file"
# 6  
Old 10-27-2008
On Solaris use /usr/xpg4/bin/awk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

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

2. Shell Programming and Scripting

Grep echo awk print all output on one line

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

3. Shell Programming and Scripting

Print awk output in same line ,For loop

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

4. Shell Programming and Scripting

sed - Print Edited Line Along With Original Line

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

5. Red Hat

User is a Part of a Group But Group Details Do Not Show the User

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

6. Shell Programming and Scripting

Joining multi-line output to a single line in a group

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

7. Shell Programming and Scripting

ID - output group-memberships one per line

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

8. Shell Programming and Scripting

Sort the file contents in each group....print the group title as well

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

9. Shell Programming and Scripting

awk - sort, then print the high value for each group

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

10. Shell Programming and Scripting

print any required line by its line no using awk and its NR variable

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
Login or Register to Ask a Question