SED: joining specific lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED: joining specific lines
# 1  
Old 09-27-2008
SED: joining specific lines

I have a .txt file containing lines like:
Code:
ZAN ZAN name = AI_EVENT desc (further text)
ZAN ZAN name = AI_EVENT desc ... 
ZAN ZAN1 name = AI_EVENT desc ...
ZAN ZAN1 name = AI_EVENT desc ...
WUR WUR name = AI_EVENT desc ...
WUR WUR name = AI_EVENT desc ...
VEN VEN name = AI_EVENT desc ...
VEN VEN name = AI_EVENT desc ...
VEN VEN1 name = AI_EVENT desc ...
VEN VEN1 name = AI_EVENT desc ...
VEN VEN2 name = AI_EVENT desc ...
VEN VEN2 name = AI_EVENT desc ...
etc (more lines sorted in a reverse alphabetical order)

I am seeking to join the lines which start with the same three letter combination, how would I go about doing this? I only have limited experience with SED and I cannot figure out a way to get this done.
# 2  
Old 09-27-2008
if you have Python
Code:
d={}
for lines in open("file"):    
    d.setdefault( lines[:3],"" )    
    d[ lines[:3] ] = d[ lines[:3] ] +" "+ lines[3:].strip()
    
for i,j in d.iteritems():
    print i,j

# 3  
Old 10-02-2008
As I understand your question, using Perl (and your sample data file) this code should do what you need:

#!/usr/bin/perl
my @array1;
my $key1;
open INPUT, "<datfile";
open OUTPUT, ">outfile";
while(<INPUT>)
{
chomp;
my @fields = split;
$key1 = $fields[0];
shift @fields;
my $x = join " ", @fields;
$array1{$key1} .= $x;
}
while ( ($key, $value) = each %array1)
{
print OUTPUT "$key $value\n";
}
close INPUT;
close OUTPUT;
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding lines of specific size in files using sed

i am using sed to detect any lines that are not exactly 21. the following gives me the lines that ARE exactly 21. i want the opposite , i want the two lines that are not size 21 (shown in bold) type a.a 000008050110010201NNN 000008060810010201NNN 21212000008070110010201NNN... (5 Replies)
Discussion started by: boncuk
5 Replies

2. Shell Programming and Scripting

Joining broken lines and removing empty lines

Hi - I have req to join broken lines and remove empty lines but should NOT be in one line. It has to be as is line by line. The challenge here is there is no end of line/start of line char. thanks in advance Source:- 2003-04-34024|04-10-2003|Claims|Claim|01-13-2003|Air Bag:Driver;... (7 Replies)
Discussion started by: Jackceasar123
7 Replies

3. Shell Programming and Scripting

ksh sed - Extract specific lines with mulitple occurance of interesting lines

Data file example I look for primary and * to isolate the interesting slot number. slot=`sed '/^primary$/,/\*/!d' filename | tail -1 | sed s'/*//' | awk '{print $1" "$2}'` Now I want to get the Touch line for only the associate slot number, in this case, because the asterisk... (2 Replies)
Discussion started by: popeye
2 Replies

4. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

5. Shell Programming and Scripting

Sed - merge lines bw 2 specific characters

Hi, I have a bash script and I am looking for a command that will merge specific lines together. Sample Data: registration time = 1300890272 Id = 1 setd = 0 tagunt = 26 tagId=6, length=8, value= tagId=9, length=5, value= tagId=7, length=2, value= tagId=16, length=2, value= tagId=32,... (8 Replies)
Discussion started by: Winsarc
8 Replies

6. Shell Programming and Scripting

Joining lines in a text file using AWK or SED

Hi All I'm struggling a bit here :( I need a way of joining lines contained in a text file. I've seen numerous SED and AWK examples and none of them seem to be working for me. The text file has 4 lines: DELL1427 DOC 30189342 79 Now bear with me on this one as I'm actually... (4 Replies)
Discussion started by: huskie69
4 Replies

7. Shell Programming and Scripting

Selecting specific 'id's from lines and columns using 'SED' or 'AWK'

Hello experts, I am new to this group and to 'SED' and 'AWK'. I have data (text file) with 5 columns (C_1-5) and 100s of lines (only 10 lines are shown below as an example). I have to find or select only the id numbers (C-1) of specific lines with '90' in the same line (of C_3) AND with '20' in... (6 Replies)
Discussion started by: kamskamu
6 Replies

8. Shell Programming and Scripting

Sed or Awk to remove specific lines

I have searched the forum for this - forgive me if I missed a previous post. I have the following file: blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah alter table "informix".esc_acct add constraint (foreign key (fi_id) references "informix".fi ... (5 Replies)
Discussion started by: Shoeless_Mike
5 Replies

9. Shell Programming and Scripting

Sed one-liner to print specific lines?

I need to print specific lines from a file, say 2-5, 8, 12-15, 17, 19, 21-27. How do I achieve this? (2 Replies)
Discussion started by: Ilja
2 Replies

10. UNIX for Dummies Questions & Answers

How to use sed modify specific lines

Could anybody tell me how I can use sed to modify lines following specific lines? the file is as following: "TEST/SI1573.lab" 3670 8920 h# 8920 9530 hh 9530 10694 ih . "TEST/DR1/FAKS0/SI2203.lab" 9730 9580 h# 9580 9840 dh 9840 10652 ix 10652 11997 r ........ I want to modify the... (5 Replies)
Discussion started by: Jenny.palmy
5 Replies
Login or Register to Ask a Question