Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Program to combine two lines in a file on checking the first character of each line

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 02-26-2016
jayaP jayaP is offline
Registered User
 
Join Date: Feb 2016
Last Activity: 26 February 2016, 9:24 AM EST
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Hi Ravinder/Rudi,

What if my file content looks like this

Code:
5, jaya, male, 4-5-90, single
-smart
6, prakash, male, 5-4-84, married
-fair
7, raghavi, female, 12-10-85, married
calm
-talented
9, bhaskar, male, 29-12-92, single
-studios
___________

Please suggest and also can you please explain your code?

Regards,
Jaya
Sponsored Links
    #9  
Old Unix and Linux 02-26-2016
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 24 March 2017, 8:00 AM EDT
Location: Chennai
Posts: 2,404
Thanks: 537
Thanked 1,129 Times in 1,015 Posts
Hello Jaya,

Could you please try following and let me know if this helps you.

Code:
awk '($0 !~ /^[0-9]/){A=A?A OFS $0:$0;next} ($0 ~ /^[0-9]/ && A){print A;A=""} {A=$0} END{print A}' OFS=""  Input_file

Output will be as follows.

Code:
5, jaya, male, 4-5-90, single-smart
6, prakash, male, 5-4-84, married-fair
7, raghavi, female, 12-10-85, marriedcalm-talented
9, bhaskar, male, 29-12-92, single-studios

Explanation for above code is as follows too.

Code:
awk '($0 !~ /^[0-9]/){  ##### Checking if line is NOT starting from digits or not here.
A=A?A OFS $0:$0;        ##### creating a variable named A which will having current line's value appending to itself whenever above condition will be TRUE.
next}                   ##### skipping all statements now by using next keyword.
($0 ~ /^[0-9]/ && A){   ##### Checking if line is starting from digits or not here.
print A;                ##### printing the variable A's value now.
A=""}                   ##### Nullifying the value of variable A now.
{A=$0}                  ##### taking current line's value to variable A now.
END{                    ##### starting END block now.
print A}                ##### printing the variable A's value now.
' OFS=""  Input_file    ##### setting OFS(Output field separator) value to NULL here and mentioning Input_file name here too.

Thanks,
R. Singh
Sponsored Links
    #10  
Old Unix and Linux 02-26-2016
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 March 2017, 6:18 AM EDT
Location: Aachen, Germany
Posts: 10,515
Thanks: 255
Thanked 3,219 Times in 2,968 Posts
Quote:
Originally Posted by jayaP View Post
Hi Ravinder/Rudi,

What if my file content looks like this

Code:
5, jaya, male, 4-5-90, single
-smart
6, prakash, male, 5-4-84, married
-fair
7, raghavi, female, 12-10-85, married
calm
-talented
9, bhaskar, male, 29-12-92, single
-studios
___________

Please suggest and also can you please explain your code?

Regards,
Jaya
What if you explained what you then need/expect?
    #11  
Old Unix and Linux 02-26-2016
jayaP jayaP is offline
Registered User
 
Join Date: Feb 2016
Last Activity: 26 February 2016, 9:24 AM EST
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
Hi Ravinder/Rudi,

Both the codes are not impacting the file and copying the file as is.

Code:
5, jaya, male, 4-5-90, single
-smart
6, prakash, male, 5-4-84, married
-fair
7, raghavi, female, 12-10-85, married
calm
-talented
9, bhaskar, male, 29-12-92, single
-studios


Code:
5, jaya, male, 4-5-90, single-smart
6, prakash, male, 5-4-84, married-fair
7, raghavi, female, 12-10-85, marriedcalm-talented
9, bhaskar, male, 29-12-92, single-studios

I'm pasting my sample original file below

Code:
5,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,Validate
Class [TGWFTRNBO]],,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
6,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,Validate
Class [TGWFTRNBO]],,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
7,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,Validate
Class [TGWFTRNBO]],,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
The operative account
10,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,Validate
Class [TGWFTRNBO]],,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

It is ok even if the commas in second line is concatenated with before line.

---------- Post updated at 07:32 PM ---------- Previous update was at 07:16 PM ----------

Guys-

Ravinder and Rudi. You are simply AWESOME!! Hats OFF

The script you provided is working great. Thanks a lot.

Regards,
Jaya

---------- Post updated at 07:42 PM ---------- Previous update was at 07:32 PM ----------

Ravinder,

It is working fine with your script. One more need is that, I want to remove those commas followed by the line starting with number while concatenating.

Sample input file

Code:
5,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,Validate
Class [TGWFTRNBO]],,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
6,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,Validate
Class [TGWFTRNBO]],,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
7,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,Validate
Class [TGWFTRNBO]],,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
The operative account
10,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,Validate
Class [TGWFTRNBO]],,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

output should be like

Code:
5,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,ValidateClass[TGWFTRNBO]]
6,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,ValidateClass[TGWFTRNBO]]
7,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,ValidateClass[TGWFTRNBO]]The operative account
10,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,ValidateClass [TGWFTRNBO]]

Regards,
Jaya

Last edited by Don Cragun; 02-26-2016 at 07:53 PM.. Reason: Fix CODE tags on Sample input file.
Sponsored Links
    #12  
Old Unix and Linux 02-26-2016
RavinderSingh13 RavinderSingh13 is online now Forum Advisor  
Registered User
 
Join Date: May 2013
Last Activity: 24 March 2017, 8:00 AM EDT
Location: Chennai
Posts: 2,404
Thanks: 537
Thanked 1,129 Times in 1,015 Posts
Hello jayaP,

Please use code tags for showing inputs also which you are using into your posts too.
Could you please try following and let me know if this helps you.

Code:
awk '($0 !~ /^[0-9]/){sub(/,+$/,X,A);A=A?A OFS $0:$0;next} ($0 ~ /^[0-9]/ && A){sub(/,+$/,X,A);print A;A=""}{A=$0}END{sub(/,+$/,X,A);print A}' OFS= ""  Input_file

Output will be as follows.

Code:
5,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,ValidateClass [TGWFTRNBO]]
6,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,ValidateClass [TGWFTRNBO]]
7,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,ValidateClass [TGWFTRNBO]]The operative account
10,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,ValidateClass [TGWFTRNBO]]

Thanks,
R. Singh
Sponsored Links
    #13  
Old Unix and Linux 02-26-2016
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 March 2017, 6:18 AM EDT
Location: Aachen, Germany
Posts: 10,515
Thanks: 255
Thanked 3,219 Times in 2,968 Posts
Try also
Code:
awk '{sub (/,+$/,_); printf "%s%s", /^[0-9]+/?XS:"", $0; XS = RS} END {printf RS}' file
5,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,ValidateClass [TGWFTRNBO]]
6,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,ValidateClass [TGWFTRNBO]]
7,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,ValidateClass [TGWFTRNBO]]The operative account
10,1,32784506,6/3/2015,6/3/2015,9:15:22,6/3/2015,9:15:34,0,16,0,ValidateClass [TGWFTRNBO]]

Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... Ferocci Shell Programming and Scripting 8 06-28-2014 05:56 AM
Combine multiple lines into single line rakesh_411 Shell Programming and Scripting 6 04-09-2014 04:46 PM
Combine multiple unique lines from event log text file into one line, use PERL or AWK? Mayday22 Shell Programming and Scripting 3 11-06-2012 08:00 PM
Combine multiple lines in single line The One Shell Programming and Scripting 8 10-26-2010 12:15 PM



All times are GMT -4. The time now is 08:07 AM.