Concatenate lines with unique string AND number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenate lines with unique string AND number
# 1  
Old 01-17-2012
Concatenate lines with unique string AND number

In Bash using AWK or sed I need to convert the following file:
Code:
...
numitem_tab0[] =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p><p>5 KEYWORD</p>...<p>25 KEYWORD</p>
subitem_tab0[] =<p>[ KEYWORD ]</p><p>[ KEYWORD ]</p>
...
numitem_tab6[] =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p>
numitem_tab7[] =<p>100 KEYWORD</p><p>101 KEYWORD</p><p>102 KEYWORD</p>...<p>115 KEYWORD</p>
...
subitem_tab66[] =<p>[ KEYWORD ]</p><p>[ KEYWORD ]</p><p>[ KEYWORD ]</p>
subitem_tab67[] =<p>[ KEYWORD ]</p><p>[ KEYWORD ]</p><p>[ KEYWORD ]</p><p>[ KEYWORD ]</p><p>[ KEYWORD ]</p>
...
<record>
<title>user name: xxxxx</title>
num_items: numitem_tab0[] =
<reference>yyyyy</reference>
</record>
...
<record><title>user name: xxxxx</title>
sub_items: subitem_tab0[] =
<reference>yyyyy</reference>
</record>
...
<record>
<title>user name: xxxxx</title>
num_items: numitem_tab7[] =
<reference>yyyyy</reference>
</record>
...
<record>
<title>user name: xxxxx</title>
num_items: numitem_tab6[] =
<reference>yyyyy</reference>
</record>
...
<record>
<title>user name: xxxxx</title>
sub_items: subitem_tab66[] =
<reference>yyyyy</reference>
</record>
...
<record>
<title>user name: xxxxx</title>
sub_items: subitem_tab67[] =
<reference>yyyyy</reference>
</record>
...

into:
Code:
...
<record>
<title>user name: xxxxx</title>
num_items: numitem_tab0[] =numitem_tab0[] =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p><p>5 KEYWORD</p>...<p>25 KEYWORD</p>
<reference>yyyyy</reference>
</record>
...
<record><title>user name: xxxxx</title>
sub_items: subitem_tab0[] =subitem_tab0[] =<p>[ KEYWORD ]</p><p>[ KEYWORD ]</p>
<reference>yyyyy</reference>
</record>
...
<record>
<title>user name: xxxxx</title>
num_items: numitem_tab7[] =numitem_tab7[] =<p>100 KEYWORD</p><p>101 KEYWORD</p><p>102 KEYWORD</p>...<p>115 KEYWORD</p>
<reference>yyyyy</reference>
</record>
...
<record>
<title>user name: xxxxx</title>
num_items: numitem_tab6[] =numitem_tab6[] =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p>
<reference>yyyyy</reference>
</record>
...
<record>
<title>user name: xxxxx</title>
sub_items: subitem_tab66[] =subitem_tab66[] =<p>[ KEYWORD ]</p><p>[ KEYWORD ]</p><p>[ KEYWORD ]</p>
<reference>yyyyy</reference>
</record>
...
<record>
<title>user name: xxxxx</title>
sub_items: subitem_tab67[] =subitem_tab67[] =<p>[ KEYWORD</p><p>[ KEYWORD ]</p><p>[ KEYWORD ]</p><p>[ KEYWORD ]</p>
<reference>yyyyy</reference>
</record>
...

That is, every "numitem_tab*[] =..." line must be cut and concatenated with corresponding "num_items: numitem_tab*[] =" line below. The tab number in "numitem_tab*" must match.

Similarly, every "subitem_tab*[] =..." line must be cut and concatenated with the corresponding "sub_items: subitem_tab*[] =" line below.

If there is no match for a "numitem_tab*[] =..." line or a "subitem_tab*[] =..." line it must be left untouched.

There will be a maximum of 600 "numitem_tab*[] =..." + "subitem_tab*[] =..." lines per file.
# 2  
Old 01-17-2012
something to start with - assuming 'numitem_tabN[]' and 'subitem_tabN[]' all precede the <record> definitions):
Code:
nawk -F'[=:]' '$1~/^(sub|num)item_tab[0-9][0-9]*[[]] */ {item[" "$1]=$2;next} $1~/^(sub|num)_items/{print $0, ($2 in item)?$2"="item[$2]:"";next}1' myFile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 01-18-2012
Just what I wanted. Thank You! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

3. Shell Programming and Scripting

Replace all string matches in file with unique random number

Hello Take this file... Test01 Ref test Version 01 Test02 Ref test Version 02 Test66 Ref test Version 66 Test99 Ref test Version 99 I want to substitute every occurrence of Test{2} with a unique random number, so for example, if I was using sed, substitution would be something... (1 Reply)
Discussion started by: funkman
1 Replies

4. Shell Programming and Scripting

Get number of lines after certain string

Hello people, I have a file like that: start something 1 xxx yyy 2 zzz xxx 3 ccc vvv 4 mmm nnn stop something 1 aaa bbb 2 ccc ddd 3 eee fff 4 qqq www start something 1 rrr ttt 2 yyy uuu 3 iii ooo 4 ppp sss And I need only line starting with 1 and 3 and only after start... (7 Replies)
Discussion started by: apenkov
7 Replies

5. Shell Programming and Scripting

[Solved] Script to concatenate 2 files with the same number of lines

Hi everyone, I have two files, namely: file1: file1Col1Row1;file1Col2Row1;file1Col3Row1 file1Col1Row2;file1Col2Row2;file1Col3Row2 file1Col1Row3;file1Col2Row3;file1Col3Row3file2: file2Col1Row1;file2Col2Row1;file2Col3Row1 file2Col1Row2;file2Col2Row2;file2Col3Row2... (0 Replies)
Discussion started by: gacanepa
0 Replies

6. Shell Programming and Scripting

Awk, sed - concatenate lines starting with string

I have a file that looks like this: John Smith http://www.profile1.com http://www.profile2.com http://www.profile3.com Marc Olsen http://www.profile4.com http://www.profile5.com http://www.profile6.com http://www.profile7.com Lynne Doe http://www.profile8.com http://www.profile9.com... (3 Replies)
Discussion started by: locoroco
3 Replies

7. UNIX for Dummies Questions & Answers

perl filter unique and concatenate

Hi experts, I have some input like below, TEST A function W TEST A function X TEST B function Y TEST C function Z TEST C function ZY i would like to have below output, TEST A function W&X TEST B function Y TEST C function Z&ZY Please kindly help on this, i am cracking my head... (2 Replies)
Discussion started by: mingfatty
2 Replies

8. Shell Programming and Scripting

Extracting several lines of text after a unique string

I'm attempting to write a script to identify users who have sudo access on a server. I only want to extract the ID's of the sudo users after a unique line of text. The list of sudo users goes to the EOF so I only need the script to start after the unique line of text. I already have a script to... (1 Reply)
Discussion started by: bouncer
1 Replies

9. Shell Programming and Scripting

get part of file with unique & non-unique string

I have an archive file that holds a batch of statements. I would like to be able to extract a certain statement based on the unique customer # (ie. 123456). The end for each statement is noted by "ENDSTM". I can find the line number for the beginning of the statement section with sed. ... (5 Replies)
Discussion started by: andrewsc
5 Replies

10. UNIX for Dummies Questions & Answers

Number of lines containing a certain string

Hey I want to know if there is an option to know the number of lines containing a certain string (bit for example) in a file? Say I want to know number of lines containing only the string BIT in file xyz. I know how to get number of lines in a file by using wc -l but how do you get number of lines... (1 Reply)
Discussion started by: #moveon
1 Replies
Login or Register to Ask a Question