Merge lines in text file based on pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge lines in text file based on pattern
# 1  
Old 02-01-2010
Question Merge lines in text file based on pattern

Hello,

I have searched forum trying to find a solution to my problem, but could not find anything or I did not understand the examples....

I should say, I am very inexperienced with text processing.

I have a text file with approx 60k lines in it.
I need to merge lines based on the number on the end of the "master line".

Example1:
Code:
Word|1
(1)|Wordel|One Word

So, here I need to delete one pipe character and number 1 after the word 'Word' and merge first line with second line. Delete number 1 and this "(" and this ")" characters. Result should look like this:
Code:
Word|Wordel|One Word

Example 2:
Code:
Eye|4
(1)|Human Eye|Animal Eye
(2)|My Eye|Your Eye|His Eye|Her Eye
(3)|Second Eye|Third Eye
(4)|So Much About Eye

Here I need to delete one pipe character and number 4 after the word 'Eye' and merge first line with four following lines. Delete number 1 and 2 and 3 and 4 and this "(" and this ")" characters. Result should look like this:
Code:
Eye|Human Eye|Animal Eye|My Eye|Your Eye|His Eye|Her Eye|Second Eye|Third Eye|So Much About Eye

So, if the txt file looks like this before processing:
Code:
Word|1
(1)|Wordel|One Word
Eye|4
(1)|Human Eye|Animal Eye
(2)|My Eye|Your Eye|His Eye|Her Eye
(3)|Second Eye|Third Eye
(4)|So Much About Eye

I need it look like this after processing:
Code:
Word|Wordel|One Word
Eye|Human Eye|Animal Eye|My Eye|Your Eye|His Eye|Her Eye|Second Eye|Third Eye|So Much About Eye

Could somebody help me with this please?
# 2  
Old 02-01-2010
Quote:
Originally Posted by Bertik
So, if the txt file looks like this before processing:
Code:
Word|1
(1)|Wordel|One Word
Eye|4
(1)|Human Eye|Animal Eye
(2)|My Eye|Your Eye|His Eye|Her Eye
(3)|Second Eye|Third Eye
(4)|So Much About Eye

I need it look like this after processing:
Code:
Word|Wordel|One Word
Eye|Human Eye|Animal Eye|My Eye|Your Eye|His Eye|Her Eye|Second Eye|Third Eye|So Much About Eye

Could somebody help me with this please?
Code:
awk 'BEGIN{FS=OFS="|"}/^[A-Z]/{printf (NR==1?_:RS)$1;next}{$1=_;printf}' infile

# 3  
Old 02-01-2010
Thank you danmero for your help.

Unfortunately, it is giving me this:
Code:
awk: line 1: no arguments in call to printf

and not processing anything.

I forget to mention that the txt file uses unicode characters.
Also, I am running on Ubuntu 9.10
# 4  
Old 02-01-2010
Try gawk on Ubuntu.
# 5  
Old 02-01-2010
Now it is giving me this:
Code:
zhudlařitgawk: (FILENAME=1.txt FNR=2) fatal: printf: no arguments

The word zhudlařit is first word on the first line in 1.txt file

Whole first line looks like this:
Code:
zhudlařit|1

# 6  
Old 02-01-2010
Quote:
Originally Posted by Bertik
Whole first line looks like this:
Code:
zhudlařit|1

Can you post a sample of real data enclosed in [code] tags.
# 7  
Old 02-01-2010
Sure, I can
Code:
zhudlařit|1
(1)|zkazit <co>|zpackat
vrch|4
(1)|kopec|pahorek|vršek|vyvýšenina
(2)|pahorkatina|vrchovina
(3)|vrchol
(4)|povrch|hořejšek
škorpit se|1
(1)|hašteřit se|hádat se|škádlit se
spurtovat|2
(1)|zrychlit běh
(2)|finišovat
oplést|1
(1)|ovinout <co čím>|omotat|obtočit
báze|2
(1)|základ|základna|podklad|východisko
(2)|zásada|alkálie
žehnat se|1
(1)|loučit se <s kým>|rozžehnávat se
zahnat|3
(1)|odehnat <koho>|zapudit|zaplašit
(2)|ukojit (hlad)|utišit|zažehnat
(3)|odnést <co kam> (bouře)
rozohňovat se|1
(1)|rozvášňovat se|rozpalovat se|uklidňovat se
připamatovat|1
(1)|připomenout|vzpomenout|upamatovat|zapomenout
propít|1
(1)|utratit (pitím)|prohýřit|prochlastat
dopustit se|1
(1)|spáchat (zločin)|provést
klepnout|4
(1)|ťuknout|kliknout (tlačítkem)|cvaknout
(2)|zasáhnout <koho>|praštit
(3)|zabít|ranit (mrtvice)
(4)|popovídat si|poklepat si|zdrbnout si
zotročený|2
(1)|utlačený|porobený
(2)|nesvobodný

Thank you for all your time danmero.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with order lines from a file based on a pattern

Hi I need to order these lines from a txt file my file looks like this IMSI ........................ 1234567890 APN ......................... INTERNET.COM APN ......................... MMS.COM APN ......................... WAP.COM APN ......................... BA.COM IMSI... (4 Replies)
Discussion started by: alone77
4 Replies

2. Shell Programming and Scripting

Need to merge lines based on pattern

Hi, I have a requirement to merge multiple lines based on search pattern. The search criteria is : it will search for CONSTRAINT and when it found CONSTRAINT, it will merge all lines to 1 line till it founds blank line. For Example: CREATE TABLE "AMS_DISTRIBUTOR_XREF" ( "SOURCE"... (5 Replies)
Discussion started by: satyaatcgi
5 Replies

3. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

4. Shell Programming and Scripting

How to merge lines based off of text?

Hello Everyone, I have two files, similar to the following: File 1: 8010 ITEM01 CODE1 FLAG1 filler filler 7020 OBJECT CODE2 FLAG2 filler 6010 THING1 CODE4 FLAG4 6011 ITEM20 CODE7 FLAG7 File 2 contains: 6020 ITEM01 CODEA FLAGA filler filler filler 7000 OBJECT CODEB... (2 Replies)
Discussion started by: jl487
2 Replies

5. Shell Programming and Scripting

Merge file lines based off of keyword

Hello Everyone, I have two files I created in a format similar to the ones found below (character position is important): File 1: 21 Cat Y N S Y Y N N FOUR LEGS TAIL WHISKERS 30 Dog N N 1 Y Y N N FOUR LEGS TAIL 33 Fish Y N 1 Y Y N N FINS 43 CAR Y N S Y Y N N WHEELS DOORS... (7 Replies)
Discussion started by: jl487
7 Replies

6. UNIX for Dummies Questions & Answers

print multiple lines from text file based on pattern list

I have a text file with a list of items/patterns: ConsensusfromCGX_alldays_trimmedcollapsedfilteredreadscontiglist(229095contigs)contig12238 ConsensusfromCGX_alldays_trimmedcollapsedfilteredreadscontiglist(229095contigs)contig34624... (1 Reply)
Discussion started by: Oyster
1 Replies

7. UNIX for Dummies Questions & Answers

merge lines within a file that start with a similar pattern

Hello! i have a text file.. which contains the data as follows i want to merge the declarations lines pertaining to one datatype in to a single line as follows i've searched the forum for help.. but couldn't find much help.. how can i do this?? (1 Reply)
Discussion started by: a_ba
1 Replies

8. Shell Programming and Scripting

Merge lines from one file if pattern matches

I have one comma separated file (a.txt) with two or more records all matching except for the last column. I would like to merge all matching lines into one and consolidate the last column, separated by ":". Does anyone know of a way to do this easily? I've searched the forum but most talked... (6 Replies)
Discussion started by: giannicello
6 Replies

9. Shell Programming and Scripting

Merge two file data together based on specific pattern match

My input: File_1: 2000_t g1110.b1 abb.1 2001_t g1111.b1 abb.2 abb.2 g1112.b1 abb.3 2002_t . . File_2: 2000_t Ali england 135 abb.1 Zoe british 150 2001_t Ali england 305 g1111.b1 Lucy russia 126 (6 Replies)
Discussion started by: patrick87
6 Replies

10. Shell Programming and Scripting

Merge lines in Flat file based on first 5 characters

Hi I have the fixed width flat file having the following data 12345aaaaaaaaaabbbbbbbbbb 12365sssssssssscccccccccc 12365sssss 12367ddddddddddvvvvvvvvvv 12367 vvvvv Here the first column is length 5 second is length 10 third is length 10 if the second or third column exceeds... (3 Replies)
Discussion started by: Brado
3 Replies
Login or Register to Ask a Question