Combine multiline to one line with proper format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine multiline to one line with proper format
# 1  
Old 12-13-2015
Combine multiline to one line with proper format

Hello Guys,

I have a file say FILE1.txt contains below data:-

Code:
A
B
C
D
E
F
G
H
I
J
K
L

I need the output in another file as FILE2 as:-

Code:
'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J',
'K', 'L'

Note:- New line should start after every 5 characters and not coma after last character.

Thanks to help.

Regards,
Jassi

Last edited by Don Cragun; 12-13-2015 at 08:19 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 12-13-2015
Hi,
with gnu sed:
Code:
sed -n "s/.*/'&',/;H;5~5{z;x;s/\n//g;p;};\${x;s/\n\|,$//g;p;}" file1.txt

Regards.
# 3  
Old 12-13-2015
Plain vanilla awk, not as sophisticated as disedorgue's sed:
Code:
awk '{T=T DL Q $1 Q ","} !(NR%5) {print T; T=""} END {sub (/,$/, _, T); print T}' Q="\047" file1
'A','B','C','D','E',
'F','G','H','I','J',
'K','L'

# 4  
Old 12-13-2015
THANKS A LOT GUYS AWK DOES THE MAGIC Smilie
# 5  
Old 12-13-2015
Code:
perl -pe 's/(^|\n)/\047/g; (!eof)?{($.%5)?{$_.=", "}:{$_.=",\n"}}:{$_.="\n"}' FILE1.txt

Code:
'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J',
'K', 'L'

# 6  
Old 12-14-2015
For academic interest, this is how you can do it with a Posix sed
Code:
sed '
:loop
$!{
N
/\(.*\n\)\{4\}/!b loop
}
s/^/'\''/
s/$/'\''/
s/\n/'\'','\''/g
$!s/$/,/
' FILE1.txt

Note, the '\'' is a ' character within ' '.
An old Unix sed needs the hold space for counting
Code:
sed '
:loop
$!{
N
x; s/$/a/; /a\{4\}/b exit
x; b loop
:exit
s/.*//
x
}
s/^/'\''/
s/$/'\''/
s/\n/'\'','\''/g
$!s/$/,/
' FILE1.txt


Last edited by MadeInGermany; 12-14-2015 at 12:05 PM.. Reason: Posix sed needed, adding a version for Unix sed
# 7  
Old 12-14-2015
bash
Code:
while read i;do
((count++))
 if [ $count -lt 5 ]
 then
 echo -ne ${i},
else
echo $i
count=0
echo -ne "\n"
 fi
 done < file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine multiline to one line till a blank line

Hello, I have a file as :- ABC DEF GHI JKL <BlankLine> MNO PQR STU VWX <BlankLine> YZA I need it as below:- ABCDEFGHIJKL; MNOPQRSTUVWX; (3 Replies)
Discussion started by: jassi10781
3 Replies

2. Shell Programming and Scripting

Need to grep this Data in proper format:- Please Guide

Hi Guys, I need to grep below data in this format backup_id creation expiration policy sched_label backup_id = picoserver38_1212077050, version = 2 creation = 05/29/2008 18:04:10 (1212077050) expiration = 06/29/2008 18:04:10 (1214755450) retention_level = 3, fragment = 2, file_num = 1... (14 Replies)
Discussion started by: manalisharmabe
14 Replies

3. Shell Programming and Scripting

Awk - Summation in Proper decimal Format

Hi I am executing below command to do summation on 46th coloumn. cat File1| awk -F"|" '{p += $46} END { printf"Column Name | SUM | " p}' I am getting output as Column Name | SUM | 1.01139e+10 Here I want output in Proper decimal format. Can someone tell me what change is required for same? (1 Reply)
Discussion started by: sanranad
1 Replies

4. Shell Programming and Scripting

Format Parts of Multiline Section to Single Line

Hello, I have an input file that I need formatted. I was hoping I could use bash to get this done. Title: Kitchen Blender Washer Dishes Title: Bathroom Toilet Sink Title: Bedroom Bed Desired output would be similar to Results("Blender","Washer","Dishes") (1 Reply)
Discussion started by: jl487
1 Replies

5. Shell Programming and Scripting

Need to split a xml file in proper format

Hi, I have a file which has xml data but all in single line Ex - <?xml version="1.0"?><User><Name>Robert</Name><Location>California</Location><Occupation>Programmer</Occupation></User> I want to split the data in proper xml format Ex- <?xml version="1.0"?> <User> <Name>Robert</Name>... (6 Replies)
Discussion started by: avishek007
6 Replies

6. Shell Programming and Scripting

Getting Proper Date Format in SH Script

There's a small SH script I'm trying to write where it will get the current month and find a log file that is based on the date. Example: Today is February, so the log file is going to be 201102.log (2011 + 02) An additional thing is that if today is the 1st of a month, it will also find the log... (3 Replies)
Discussion started by: kooshi
3 Replies

7. Shell Programming and Scripting

Output file not displayed in the proper format

Hi am using uuencode fro attaching one report which is nothing but sql query output. But when i receive the report through attachement and when it is opened the report is not displayed in proper format. Means if the sql query has 100 rows the mail attachment report displays the report in 2... (2 Replies)
Discussion started by: weknowd
2 Replies

8. Shell Programming and Scripting

Awk Multiline Record Combine?

I'm trying to use Awk to get the id and name fields ($1 and $2) of file1 combined with their corresponding multiline records in file2 that are separated by blank line. Both files are ordered so that the first line of file1 corresponds to the first set of multiline records in file2 and so on. ... (4 Replies)
Discussion started by: RacerX
4 Replies

9. Programming

What is the proper way to combine C++ files (with g++) to avoid link (ld) errors?

Problem background: gcc v 4.1 2 .cpp files, 2 .h files Files: main.cpp a.cpp a.h b.h Organization: main.cpp includes a.h (because it calls a.cpp code) a.cpp includes a.h a.h includes b.h (because a class in a.h uses a b.h class) There is no inheritance between a.h or b.h or any of... (1 Reply)
Discussion started by: johnqsmith
1 Replies

10. Shell Programming and Scripting

Check for proper e mail id format

Hi, We run an application called meta which reads user information from database and updates in LDAP.For that we have some scripts to check the uniqueness of mail ids between the existing LDAP and Database.It works fine when people enter mail ids in proper format (xxx.yyy@abc.com) but if it... (2 Replies)
Discussion started by: prolay
2 Replies
Login or Register to Ask a Question