awk to combine lines from line with pattern match to a line that ends in a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to combine lines from line with pattern match to a line that ends in a pattern
# 1  
Old 02-20-2016
awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions:
1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text.
2. If condition1 is met then continue to combine lines through a line that ends with a semicolon.
3. Ignore case when matching patterns and remove any leading spaces from line when joining.

I have tried to code this using awk or sed without success.

Input file:
Code:
libname &wrk_schema DB2 database = %sysget( DB2DBDFT ) schema = &wrk_schema read_isolation_level = ur ;
libname schema db2 datasrc=%sysfunc(sysget(DB2DBDFT)) schema=&qmt_schema read_isolation_level=ur;
libname db2lib db2 datasrc=crd_prod ;
libname server db2 datasrc=%sysfunc(sysget(DB2DBDFT)) ril=ur;
libname server db2 datasrc=%sysfunc(sysget(DB2DBDFT));
libname server db2 datasrc=%sysfunc(sysget(DB2DBDFT))
            schema = &build_schema
            access = readonly
            connection = globalread
            read_isolation_level=ur;
libname server2 db2 datasrc=%sysfunc(sysget(DB2DBDFT))
             schema = &build_schema2
             access = readonly
             connection = globalread
             read_isolation_level=ur;

Desired Output file:
Code:
libname &wrk_schema DB2 database = %sysget( DB2DBDFT ) schema = &wrk_schema read_isolation_level = ur ;
libname schema db2 datasrc=%sysfunc(sysget(DB2DBDFT)) schema=&qmt_schema read_isolation_level=ur;
libname db2lib db2 datasrc=crd_prod ;
libname server db2 datasrc=%sysfunc(sysget(DB2DBDFT)) ril=ur;
libname server db2 datasrc=%sysfunc(sysget(DB2DBDFT));
libname server db2 datasrc=%sysfunc(sysget(DB2DBDFT)) schema = &build_schema access = readonly connection = globalread read_isolation_level=ur; 
libname server2 db2 datasrc=%sysfunc(sysget(DB2DBDFT)) schema = &build_schema2 access = readonly connection = globalread read_isolation_level=ur;


Last edited by Wes Kem; 02-20-2016 at 08:55 PM..
# 2  
Old 02-21-2016
What awk and sed code have you tried to solve this problem?
# 3  
Old 02-21-2016
This is awk I tried by it is looping theought the last line of the file.

awk 'BEGIN{IGNORECASE=1} /libname/&&/DB2 datasrc/ {print;while($0!~ /:;/){getline;print;}}' file
# 4  
Old 02-21-2016
Your code doesn't make an exception for lines containing libname, db2 and datasrc that already end with a semicolon. It doesn't verify that libname is at the start of a line, doesn't verify that db2 is in the 3rd field, and looks for a colon immediately followed by a semicolon (which never appears in your sample input) to end the set of lines being joined.

You might want to try something more like:
Code:
#!/bin/ksh
awk '
$1 ~ "^[Ll][Ii][Bb][Nn][Aa][Mm][Ee]$" && $3 ~ "^[Dd][Bb]2$" &&
$4 ~ /^[Dd][Aa][Tt][Aa][Ss][Rr][Cc]=/ && $0 !~ /;$/ {
	printf("%s ", $0)
	j = 1
	next
}
j {	for(i = 1; i < NF; i++)
		printf("%s ", $i)
	printf("%s%s", $NF, (j = ($NF !~ /;$/)) ? " " : "\n")
	next
}
1' file

The awk IGNORECASE variable works in some versions of awk, but it is not in the standards and several standards-conforming versions of awk (including the awk on BSD and OS X systems) do not provide that extension. The code above works with any standards-conforming version of awk, but obviously needs more complicated regular expressions to perform case-insensitive matches.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xgp4/bin/awk or nawk.
# 5  
Old 02-22-2016
Would this help?
Code:
awk '
tolower(T = $0) ~ "^libname [^ ]+ db2 datasrc" \
        {while (! /;$/) {getline X
                         sub (/^ +/, " ", X)
                         $0 = $0 X
                        }
        }
1
' file
libname &wrk_schema DB2 database = %sysget( DB2DBDFT ) schema = &wrk_schema read_isolation_level = ur ;
libname schema db2 datasrc=%sysfunc(sysget(DB2DBDFT)) schema=&qmt_schema read_isolation_level=ur;
libname db2lib db2 datasrc=crd_prod ;
libname server db2 datasrc=%sysfunc(sysget(DB2DBDFT)) ril=ur;
libname server db2 datasrc=%sysfunc(sysget(DB2DBDFT));
libname server db2 datasrc=%sysfunc(sysget(DB2DBDFT)) schema = &build_schema access = readonly connection = globalread read_isolation_level=ur;
libname server2 db2 datasrc=%sysfunc(sysget(DB2DBDFT)) schema = &build_schema2 access = readonly connection = globalread read_isolation_level=ur;

This User Gave Thanks to RudiC For This Post:
# 6  
Old 02-23-2016
Don -Thanks for the code! It works great!

I did modify to use IGNORECASE=1 and it handled case differences OK.

I also added the code to be in a loop so it will made the code changes to all code beginning with a given prefix.

Code:
 
 #!/bin/ksh
 #Combine lines that start with "libname VALUE db2 datasrc" and does not end in semicolon
for f in pre*; do
awk 'BEGIN{IGNORECASE=1} /libname/&&/DB2/ && $0 !~ /;$/ {
 printf("%s ", $0)
 j = 1
 next
}
j { for(i = 1; i < NF; i++)
  printf("%s ", $i)
 printf("%s%s", $NF, (j = ($NF !~ /;$/)) ? " " : "\n")
 next
}
1' "$f" > fifo &&
mv fifo $f
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. UNIX for Beginners Questions & Answers

awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. UNIX for Beginners Questions & Answers

awk script for pattern match and line break

Hi, I have input which reads like 9089.00 ----- kl jkjjljk lkkk; (909099) 9097.00 ----- HGJJHHJ jcxkjlkjvhvlk jhdkjksdfkhfskd 898.00 ----- HHHH I am trying to do something like this - As soon as I found pattern match "XYZ.00-----" it will insert a line break to the input and will go to... (3 Replies)
Discussion started by: Indra2011
3 Replies

4. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

5. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

6. Shell Programming and Scripting

awk print pattern match line and following lines

Data: Pattern Data Data Data Data Data Data Data Data Data ... With awk, how do I print the pattern matching line, then the subsequent lines following the pattern matching line. Varying number of lines following the pattern matching line. (9 Replies)
Discussion started by: dmesserly
9 Replies

7. Shell Programming and Scripting

Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts, I want to remove first and last line after pattern match "vg" : I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data. - So it should check for the pattern vg if present , then it should delete the line ,... (5 Replies)
Discussion started by: rveri
5 Replies

8. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

9. Shell Programming and Scripting

How to insert line with between two consecutive lines that match special pattern?

I have following pattern in a file: 00:01:38 UTC abcd 00:01:48 UTC 00:01:58 UTC efgh 00:02:08 UTC 00:02:18 UTC and I need to change something like the following 00:01:38 UTC abcd 00:01:48 UTC XXXX 00:01:58 UTC efgh 00:02:08 UTC XXXX (6 Replies)
Discussion started by: jjnight
6 Replies

10. Shell Programming and Scripting

Concatenating multiple lines to one line if match pattern

Hi all, I've been working on a script which I have hit a road block now. I have written a script using sed to extract the below data and pumped into another file: Severity............: MAJORWARNING Summary: System temperature is out of normal range. Severity............: MAJORWARNING... (13 Replies)
Discussion started by: phixsius
13 Replies
Login or Register to Ask a Question