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
# 8  
Old 02-01-2010
Base on your sample data:
Code:
# awk 'BEGIN{FS=OFS="|"}/^[a-z]/{printf (NR==1?_:RS)$1;next}{$1=_;printf}' infile
zhudla.it|zkazit <co>|zpackat
vrch|kopec|pahorek|vr.ek|vyvý.enina|pahorkatina|vrchovina|vrchol|povrch|ho.ej.ek|1|ha.te.it se|hádat se|.kádlit se
spurtovat|zrychlit b.h|fini.ovat
oplést|ovinout <co .ím>|omotat|obto.it
báze|základ|základna|podklad|východisko|zásada|alkálie|1|lou.it se <s kým>|roz.ehnávat se
zahnat|odehnat <koho>|zapudit|zapla.it|ukojit (hlad)|uti.it|za.ehnat|odnést <co kam> (bou.e)
rozoh.ovat se|rozvá..ovat se|rozpalovat se|uklid.ovat se
p.ipamatovat|p.ipomenout|vzpomenout|upamatovat|zapomenout
propít|utratit (pitím)|prohý.it|prochlastat
dopustit se|spáchat (zlo.in)|provést
klepnout|.uknout|kliknout (tla.ítkem)|cvaknout|zasáhnout <koho>|pra.tit|zabít|ranit (mrtvice)|popovídat si|poklepat si|zdrbnout si
zotro.ený|utla.ený|porobený|nesvobodný

Use gawk, nawk or /usr/xpg4/bin/awk on Solaris.
# 9  
Old 02-02-2010
I could not make it work danmero. Anyway, thank you for your time.

Here is command which worked (it added pipe character to the end of each line also).
Code:
awk -F\| -v OFS=\| '/^\(/{sub(/[^|]*|/,"");var=var $0;next}var{print var "|" }{NF--;var=$0}END{print var "|"}' file

Credit for this command is going to pgas from #awk at irc.freenode.net
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