Concatenate multiple lines based.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenate multiple lines based.
# 1  
Old 06-30-2010
Concatenate multiple lines based.

Hello,

I have been searching the forum for concatenation based on condition. I have been close enough but not got th exact one.

infile:
-----
Code:
DB_Name ABC (X,
Y,Z).
DB_Name DEF (T).
DB_Name GHI (U
,V,W).

Desired Output file should be:
---------------------------
Code:
DB_Name ABC (X,Y,Z).
DB_Name DEF (T).
DB_Name GHI (U,V,W).

Basically idea behind is
- If the line does not start with string "DB_Name" then append that line to previous line and if it does start with "DB_Name" print that line.

Thanks in advance.
Indrajit


Moderator's Comments:
Mod Comment Please use code tags!
# 2  
Old 06-30-2010
Code:
awk '/DB_Name/{printf "\n"$0;next}{printf $0}' file

Output isn't ideal, but it is easy to fix.
# 3  
Old 06-30-2010
Thanks for the quicky but the output looks complex:

Code:
DB_Name ABC (X,Y,Z).DB_Name 
DEF (T).DB_Name GHI (U,V,W).

whereas i wanted to have
Code:
DB_Name ABC (X,Y,Z).
DB_Name DEF (T).
DB_Name GHI (U,V,W).

because I will be using this file as input to my database script.

Thanks

Last edited by Scott; 06-30-2010 at 07:45 PM.. Reason: Code tags
# 4  
Old 06-30-2010
Check two times if you wrote the code corectly:
Code:
awk '/DB_Name/{printf "\n"$0;next}{printf $0}' file

The "\n" is before $0, so new line goes alway before "DB_Name" therefore it can't produce output you provided: DB_Name ABC (X,Y,Z).DB_Name.
# 5  
Old 06-30-2010
I am using the same way you have given:

Code:
awk '/DB_Name/{printf "\n"$0;next}{printf $0}' Q1_Expln_file_diag

Output is

Code:
DEV_EIS_T.ProdtAtSlsOrg COLUMN SLSORGNBR.DEV_EIS_T.ProdtAtSlsOrg COLUMN (PARTITION).DEV_EIS_T.ProdtAtSlsOrg COLUMN (PRODTCD, DIVCD ,ACTVIND).  DEV_EIS_T.ProdtAtSlsOrg COLUMN (PRODTCD, DIVCD).

I am expecting:
Code:
DEV_EIS_T.ProdtAtSlsOrg COLUMN SLSORGNBR.
DEV_EIS_T.ProdtAtSlsOrg COLUMN (PARTITION).
DEV_EIS_T.ProdtAtSlsOrg COLUMN (PRODTCD, DIVCD ,ACTVIND).  
DEV_EIS_T.ProdtAtSlsOrg COLUMN (PRODTCD, DIVCD).


Last edited by Scott; 06-30-2010 at 07:46 PM.. Reason: Code tags
# 6  
Old 06-30-2010
You put "DB_Name" in the first post, so I assumed it is how your file looks like... So for your real file this should work:
Code:
awk '/DEV_EIS_T/{printf "\n"$0;next}{printf $0}' file

Next time, if you expect accurate answers, please provide accurate information.

Last edited by bartus11; 06-30-2010 at 06:43 PM.. Reason: fixed
# 7  
Old 06-30-2010
Sorry but I was assuming that text string wont make difference like

Code:
DEV_EIS_T.ProdtAtSlsOrg COLUMN SLSORGNBR.

DB_Name.ProdtAtSlsOrg COLUMN SLSORGNBR.

would require same logic and if we just change the string that we want it would serve the purpose.

I tried with the new command and the output is same as previous:

Code:
DEV_EIS_T.ProdtAtSlsOrg COLUMN SLSORGNBR.DEV_EIS_T.ProdtAtSlsOrg COLUMN (PARTITION).DEV_EIS_T.ProdtAtSlsOrg COLUMN (PRODTCD, DIVCD ,ACTVIND).  DEV_EIS_T.ProdtAtSlsOrg COLUMN (PRODTCD, DIVCD)

Moderator's Comments:
Mod Comment Code tags, please...

Last edited by Scott; 06-30-2010 at 07:51 PM.. Reason: Code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Merging multiple lines into single line based on one column

I Want to merge multiple lines based on the 1st field and keep into single record. SRC File: AAA_POC_DB.TAB1 AAA_POC_DB.TAB2 AAA_POC_DB.TAB3 AAA_POC_DB.TAB4 BBB_POC_DB.TAB1 BBB_POC_DB.TAB2 CCC_POC_DB.TAB6 OUTPUT ----------------- 'AAA_POC_DB','TAB1','TAB2','TAB3','TAB4'... (10 Replies)
Discussion started by: raju2016
10 Replies

2. Shell Programming and Scripting

awk joining multiple lines based on field count

Hi Folks, I have a file with fields as follows which has last field in multiple lines. I would like to combine a line which has three fields with single field line for as shown in expected output. Please help. INPUT hname01 windows appnamec1eda_p1, ... (5 Replies)
Discussion started by: shunya
5 Replies

3. Shell Programming and Scripting

Split a line into multiple lines based on delimeters

Hi, I need help to split any lines that contain ; or , input.txtAc020 Not a good chemical process AC030 many has failed, 3 still maintained AC040 Putative; epithelial cells AC050 Predicted binding activity AC060 rodC Putative; upregulated in 48;h biofilm vs planktonic The output... (8 Replies)
Discussion started by: redse171
8 Replies

4. Shell Programming and Scripting

Concatenate select lines from multiple files

I have about 6000 files of the following format (three simplified examples shown; actual files have variable numbers of columns, but the same number of lines). I would like to concatenate the ID (*Loc*) and data lines, but not the others, as shown below. The result would be one large file (or... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. Shell Programming and Scripting

Copying lines from multiple logfiles, based on content of the line

d df d d (1 Reply)
Discussion started by: larsk
1 Replies

6. Shell Programming and Scripting

Combine multiple lines in file based on specific field

Hi, I have an issue to combine multiple lines of a file. I have records as below. Fields are delimited by TAB. Each lines are ending with a new line char (\n) Input -------- ABC 123456 abcde 987 890456 7890 xyz ght gtuv ABC 5tyin 1234 789 ghty kuio ABC ghty jind 1234 678 ght ... (8 Replies)
Discussion started by: ratheesh2011
8 Replies

7. Shell Programming and Scripting

Extracting lines based on identifiers into multiple files respectively

consider the following is the contents of the file cat 11.sql drop procedure if exists hoop1 ; Delimiter $$ CREATE PROCEDURE hoop1(id int) BEGIN END $$ Delimiter ; . . . . drop procedure if exists hoop2; Delimiter $$ CREATE PROCEDURE hoop2(id int) BEGIN END $$ (8 Replies)
Discussion started by: vivek d r
8 Replies

8. Shell Programming and Scripting

AWK Duplicate lines multiple times based on a calculated value

Hi, I'm trying to create an XML sitemap of our dynamic ecommerce sites SEO Friendly URLs and am trying to create the initial page listing. I have a CSV file that looks like the following and need duplicate the lines based on a value which needs calculating. ... (2 Replies)
Discussion started by: jamesfx
2 Replies

9. Shell Programming and Scripting

want to concatenate multiple files based on the rest of ls -lrt

uadm@4132> ls -lrt -rw------- 1 uadm uadm 3811819 Jun 6 04:08 data_log-2010.05.30-10:04:08.txt -rw------- 1 uadm uadm 716246 Jun 13 01:38 data_log-2010.06.06-10:04:08.txt -rw------- 1 uadm uadm 996 Jun 13 04:00 data_log-2010.06.06-10:04:22.txt -rw------- 1 uadm uadm 7471 Jun 20 02:03... (5 Replies)
Discussion started by: mail2sant
5 Replies

10. Shell Programming and Scripting

Find multiple patterns on multiple lines and concatenate output

I'm trying to parse COBOL code to combine variables into one string. I have two variable names that get literals moved into them and I'd like to use sed, awk, or similar to find these lines and combine the variables into the final component. These variable names are always VAR1 and VAR2. For... (8 Replies)
Discussion started by: wilg0005
8 Replies
Login or Register to Ask a Question