👤
Home Man
Search
Today's Posts
Register

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

👤 Login to reply

 
Thread Tools Search this Thread
# 8  
Old 02-26-2016
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
# 9  
Old 02-26-2016
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
# 10  
Old 02-26-2016
Quote:
Originally Posted by jayaP
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 02-26-2016
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.
# 12  
Old 02-26-2016
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
# 13  
Old 02-26-2016
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]]

👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk to remove lines that do not start with digit and combine line or lines cmccabe Shell Programming and Scripting 3 07-14-2017 08:51 AM
Need to combine two lines in a file based on first character of each line in a file jayaP UNIX for Advanced & Expert Users 1 02-25-2016 07:49 PM
Need to combine two lines in a file based on first character of each line in a file jayaP UNIX for Dummies Questions & Answers 1 02-25-2016 07:09 PM
awk to combine lines from line with pattern match to a line that ends in a pattern Wes Kem Shell Programming and Scripting 5 02-23-2016 07:11 PM
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
Checking ^M character in file Ganesh L Shell Programming and Scripting 3 05-31-2013 04:21 AM
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
How to remove new line character and append new line character in a file? sasikari HP-UX 8 10-27-2010 06:09 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 02:19 AM.



All times are GMT -4. The time now is 02:19 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password