Sponsored Content
Top Forums Shell Programming and Scripting join on a file with multiple lines, fields Post 302267808 by crimper on Sunday 14th of December 2008 12:51:21 AM
Old 12-14-2008
join on a file with multiple lines, fields

I've looked at the join command which is able to perform what I need on two rows with a common field, however if I have more than two rows I need to join all of them.

Thus I have one file with multiple rows to be joined on an index number:

Code:
1 randomtext1
2 rtext2
2 rtext3
3 rtext4
3 rtext5
3 rtext6
.
.
.

I need this:
Code:
1 randomtext1
2 rtext2 rtext3
3 rtext4 rtext5 rtext6
.
.
.

where the repeated index number may have an arbitrary number of rtext's to be pivoted to columns for that index number. The number suffixes on rtext are only there for clarity.

I've considered using uniq to extracting the repeated text and using the results from that to remove that specific line in awk, then just reiterate this process in a script and use 'join', but I'm convinced there is a complete, easier solution.

updates: I have progressed to a solution with gawk... now I am having a problem with losing the order of rows as columns.
Code:
BEGIN {
FS=" ";
maxFLD=2;

getline var_tempnew
split(var_tempnew, array_temp, " ")
}
{

if ($1 == array_temp[1]){

	do
	{
	$0 = $0 " " array_temp[2]
	getline var_tempnew
	split(var_tempnew, array_temp, " ")
	} while ($1 == array_temp[1]) 
	print $0
	
} else {
    print var_tempnew
	var_tempnew = $0
	split(var_tempnew, array_temp, " ")
}
}
END {
print var_tempnew
}


Last edited by crimper; 12-14-2008 at 04:20 AM.. Reason: progress updates
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

join on multiple fields

Is it possible to do a join on multiple fields of two files? I am trying to do something like join -t, -1 2,3 -2 2,3 -o 2.1,2.2,2.3,1.3 filea fileb I want the join to be on columns 2 and 3 of filea and columns 2 and 3 of fileb. What is hapenning is that the second file that I want to do the join... (1 Reply)
Discussion started by: reggiej
1 Replies

2. Shell Programming and Scripting

Awk Join multiple lines

Hi, I have data with broken lines: Sample data: "12"|"25"|"a"|"b"|"c"|"d"|"e"|"f"|"2453748"|"08:10:50" "16"|"25"|"a"|"b"|"c"|"d"|"e"|"f"|" 2453748"|"08:15:50" "16"|"25"|"a"|"b"|" c"|"d"|"e"|"f"|"2453748"|"08:19:50" "16"|"25"|"a"|"b"|"c"|"d"|"e"|"f"|"2453748"|"08:19:50" In the... (5 Replies)
Discussion started by: hitmansilentass
5 Replies

3. Shell Programming and Scripting

How to use SED to join multiple lines?

Hi guys, anyone know how can i join multiples lines using sed till the end of a file and output to another file in a single line? The end of each line will be replaced with a special char "#". I am using the below SED command, however it seems to remove the last 2 lines. Also not all lines... (12 Replies)
Discussion started by: DrivesMeCrazy
12 Replies

4. UNIX for Dummies Questions & Answers

Need help with Join on multiple fields

Hi, I need help with the join command I have 2 files that I want to join on multiple fields. I want to return all records from file 1 I also want empty fields in my joined file if there isn't a match in file 2 I have already sorted them so I know they are in the same order. file1 ... (0 Replies)
Discussion started by: shunter0810
0 Replies

5. Shell Programming and Scripting

Join fields from files with duplicate lines

I have two files, file1.txt: 1 abc 2 def 2 dgh 3 ijk 4 lmn file2.txt 1 opq 2 rst 3 uvw My desired output is: 1 abc opq 2 def rst 2 dgh rst 3 ijk uvw (2 Replies)
Discussion started by: xan.amini
2 Replies

6. Shell Programming and Scripting

Join multiple lines

Hi I have a source file ( written i C ) where a funtion call is spread over multiple lines, for example : func( a, b, c ); I want this to be joined into one single line : func(a,b,c); How can this be done with awk and sed ? Regards. Hench (2 Replies)
Discussion started by: hench
2 Replies

7. Shell Programming and Scripting

Join common patterns in multiple lines into one line

Hi I have a file like 1 2 1 2 3 1 5 6 11 12 10 2 7 5 17 12 I would like to have an output as 1 2 3 5 6 10 7 11 12 17 any help would be highly appreciated Thanks (4 Replies)
Discussion started by: Harrisham
4 Replies

8. Shell Programming and Scripting

Join fields in a same file based on condition

I have an input file like this... All iI want to do is If the lines are identical except for the last field i want to merge them into single line input_file I feel something is nothing I feel something is everything apple mango banana apple mango grapes I want to get output like this:... (3 Replies)
Discussion started by: raj_k
3 Replies

9. Shell Programming and Scripting

Join files on multiple fields

Hello all, I want to join 2 tabbed files on the first 2 fields, and filling the missing values with 0. The 3rd column in each file is constant for the entire file. file1 12658699 ST5 XX2720 0 1 0 1 53039541 ST5 XX2720 1 0 1.5 1 file2 ... (6 Replies)
Discussion started by: sheetalk
6 Replies

10. Shell Programming and Scripting

Join multiple lines from text file

Hi Guys, Could you please advise how to join multiple details lines into single row, with HEADER 1 as the record separator and comma(,) as the field separator. Input: HEADER 1, HEADER 2, HEADER 3, 11,22,33, COLUMN1,COLUMN2,COLUMN3, AA1, BB1, CC1, END: ABC HEADER 1, HEADER 2,... (3 Replies)
Discussion started by: budz26
3 Replies
HTML::FormFu::Deflator::CompoundSplit(3pm)		User Contributed Perl Documentation		HTML::FormFu::Deflator::CompoundSplit(3pm)

NAME
HTML::FormFu::Deflator::CompoundSplit - CompoundSplit deflator SYNOPSIS
--- element: - type: Multi name: address elements: - name: number - name: street deflator: - type: CompoundSplit # set the default $form->get_field('address')->default( $address ); DESCRIPTION
Deflator to allow you to set several field's values at once. For use with a HTML::FormFu::Element::Multi group of fields. A default value passed to the Multi field will be split according to the "split" setting, and its resulting parts passed to its child elements. METHODS
split Arguments: $regex Default Value: "qr/ +/" Regex used to split the default value. Defaults to a regex matching 1 or more space characters. join Arguments: $string Default Value: ' ' If spliting the value results in more parts than there are fields, any extra parts are joined again to form the value for the last field. The value of "join" is used to join these values. Defaults to a single space. For example, if the Multi element contains fields "number" and "name", and is given the value "10 Downing Street"; when split this results in 3 parts: 10, "Downing" and "Street". In this case, the 1st part, 10 is assigned to the first field, and the 2nd and 3rd parts are re- joined with a space to give the single value "Downing Street", which is assigned to the 2nd field. field_order Arguments: @order If the parts from the split value should be assigned to the fields in a different order, you must provide an arrayref containing the names, in the order they should be assigned to. --- element: - type: Multi name: address elements: - name: street - name: number deflator: - type: CompoundSplit field_order: - number - street AUTHOR
Carl Franks LICENSE
This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2012-01-23 HTML::FormFu::Deflator::CompoundSplit(3pm)
All times are GMT -4. The time now is 10:40 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy