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:
-----

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

Desired Output file should be:
---------------------------

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

awk '/DB_Name/{printf "\n"$0;next}{printf $0}' file

Output isn't ideal, but it is easy to fix.

Thanks for the quicky but the output looks complex:

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

whereas i wanted to have

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

Check two times if you wrote the code corectly:

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.

I am using the same way you have given:

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

Output is

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:

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).

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:

awk '/DEV_EIS_T/{printf "\n"$0;next}{printf $0}' file

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

Sorry but I was assuming that text string wont make difference like

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:

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)

Sorry, there were some unneeded characters in the last command. Try now:

awk '/DEV_EIS_T/{printf "\n"$0;next}{printf $0}' file
1 Like

Hi..

Thanks a lot...!!! Its rocking ...appreciate your time for this ..!!!