Visit The New, Modern Unix Linux Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Program to combine two lines in a file on checking the first character of each line
# 1  
Question Program to combine two lines in a file on checking the first character of each line

Hi,

I have a requirement where I need to combine two lines in a file based on first character of each line in a file.

Please find the sample content of the file below:
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
________________________

I want the output for this file as
_______________________
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

________________________

can you please help me with the shell program that achieve my requirement?

Last edited by jim mcnamara; 02-25-2016 at 05:16 PM..
# 2  
What have you tried so far? If you have some code we can help you get it working.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Hi Jim,

I'm new to the Shell scripting. Not sure on how to start. But I tried a piece of code and I'm sure that its completely wrong Smilie
_______________
#!/bin/ksh

for i in '/data/informatica/HQ/APSSR01/SrcFiles/tgt_detail_import.dat| grep -o ^.'
do

case $i in
[0-9]*) var= 1;;
if [$var= 1] then 'sed 'N;s/\n//''
fi
esac
done
________________
Please help me by creating a new program.

Regards,
Jaya
# 4  
Hello jayaP,

Please use code tags as per forum rules in your posts for Commands/Inputs/Codes which you use into your post.
Following may help you in same too.
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, married-calm-talented
9, bhaskar, male, 29-12-92, single-studios

On a Solaris/SunOS system, change awkto /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk.
EDIT: Adding a non-one liner form of solution here too.
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

Thanks,
R. Singh

Last edited by RavinderSingh13; 02-26-2016 at 03:06 AM.. Reason: Added non-one liner form of solution now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Hi Ravinder,

Thanks for your solution. I'm testing it at my end. Can you please let me know if we can write the output of the awk program to a new file?

Regards,
Jaya
# 6  
Hello Jaya,

Yes, you could write the output of code into a new file as follows.
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_file

Following is a non-one liner form of solution too.
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_file

In both of the above codes, output will be stored in file named Output_file.
Hope this helps you.

Thanks,
R. Singh
# 7  
How about
Code:
awk '{printf "%s%s", /^[0-9]+/?XS:"-", $0; XS = RS} END {printf RS}' file
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


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #755
Difficulty: Medium
Flash memory is a type of EEPROM.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. UNIX for Advanced & Expert Users

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

4. UNIX for Dummies Questions & Answers

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

5. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

6. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

7. Shell Programming and Scripting

Combine multiple lines into single line

Hi All , I have a file with below data # User@Host: xyz @ # Query_time: t1 Lock_time: t2 Rows_sent: n1 Rows_examined: n2 SET timestamp=1396852200; select count(1) from table; # Time: 140406 23:30:01 # User@Host: abc @ # Query_time: t1 Lock_time: t2 Rows_sent: n1 Rows_examined:... (6 Replies)
Discussion started by: rakesh_411
6 Replies

8. Shell Programming and Scripting

Checking ^M character in file

i have the file <infile> with ^M charactrer. i just wnt to know through script whether the file have ^M character or not. Please let me know the script steps to check the same. Thanks in Advance Ganesh. (3 Replies)
Discussion started by: Ganesh L
3 Replies

9. Shell Programming and Scripting

Combine multiple unique lines from event log text file into one line, use PERL or AWK?

I can't decide if I should use AWK or PERL after pouring over these forums for hours today I decided I'd post something and see if I couldn't get some advice. I've got a text file full of hundreds of events in this format: Record Number : 1 Records in Seq : ... (3 Replies)
Discussion started by: Mayday22
3 Replies

10. Shell Programming and Scripting

Combine multiple lines in single line

This is related to one of my previous post but now with a slight difference: I need the "Updated:" to be in one line as well as the "Information:" on one line as well. These are in multiple lines right now as seen below. These can have 2 or more lines that needs to be in one line. System name:... (8 Replies)
Discussion started by: The One
8 Replies

Featured Tech Videos