Splitting one line into multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting one line into multiple lines
# 1  
Old 04-23-2014
Splitting one line into multiple lines

Hi,
I have one problem scenorio as below.
my source file is :
cat input_file.
Code:
"hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","y ou."

my output should be like.
Code:
"hi","there","how","are","you?",
"It","was","great","working","with",
"you.","hope","to","work","y ou."

for above output i used below command.
Code:
$ perl -ne 's/,/++$i % 5 ? "," : "\n"/ge; print' infile

[al above is posted here a long back only Smilie]
and its working fine.

Now the Actual Issue is, i want to parameterize the no_of_colums field(i.e. number 5).
i tried it in many ways which i can but no luck.
(like $no_of_col, ${no_of_col} , "$no_of_col"...etc )
please help me in overcome the problem.

Thanks in advance..

Last edited by Franklin52; 04-23-2014 at 08:34 AM.. Reason: Please use code tags
# 2  
Old 04-23-2014
try with this

Code:
rec=5
perl -ne 's/,/++$i % '$rec' ? "," : "\n"/ge; print' input

This User Gave Thanks to Makarand Dodmis For This Post:
# 3  
Old 04-23-2014
Awesome.
It worked.
It may simple but helps me much.
Thanks Makarand Dodmis for quick response.
# 4  
Old 04-24-2014
In the above scenario the last delimeter is missing in the output.
LAST COLUMN SHOULD HAVE DELIMETER AS WELL
If file is like this:

Code:
"hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","you."

I want it like this
Code:
"hi","there","how","are","you?",
"It","was","great","working","with",
"you.","hope","to","work","you",


Quick Help is Much appriciated.

Thanks in Advance.

Last edited by Scrutinizer; 04-24-2014 at 09:57 AM.. Reason: code tags
# 5  
Old 04-24-2014
Try :

Code:
$ echo '"hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","y ou."' | \
awk 'ORS = !(NR%5)?"\n":OFS'  RS=, OFS=,

"hi","there","how","are","you?"
"It","was","great","working","with"
"you.","hope","to","work","y ou."

Code:
$ echo '"hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","y ou."' | \
awk -F, '{for(i=1;i<=NF;i++)  printf "%s%s",$i,!(i%5)? "\n" : FS }' 

"hi","there","how","are","you?"
"It","was","great","working","with"
"you.","hope","to","work","y ou."

# 6  
Old 04-24-2014
thanks akshay for reply.
its again same issue.
the expected result is , after 5th field delemeter should come.

Code:
"hi","there","how","are","you?",
"It","was","great","working","with",
"you.","hope","to","work","you",

which is missing now.

Last edited by Scrutinizer; 04-24-2014 at 09:56 AM.. Reason: code tags
# 7  
Old 04-24-2014
To add extra comma use this
Code:
awk -F, '{for(i=1;i<=NF;i++)  printf "%s%s",$i,!(i%5)? FS "\n" : FS }'
"hi","there","how","are","you?",
"It","was","great","working","with",
"you.","hope","to","work","y ou.",

This User Gave Thanks to Akshay Hegde For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiple lines to single line

I have code as below # create temporary table `temp4277`(key(waybill_no)) select waybill_no,concat_ws('',card_type,card_series_no) cardinfo from rfid_temp_ticket where waybill_no='4277' group by... (4 Replies)
Discussion started by: kaushik02018
4 Replies

2. Shell Programming and Scripting

Splitting XML file on basis of line number into multiple file

Hi All, I have more than half million lines of XML file , wanted to split in four files in a such a way that top 7 lines should be present in each file on top and bottom line of should be present in each file at bottom. from the 8th line actual record starts and each record contains 15 lines... (14 Replies)
Discussion started by: ajju
14 Replies

3. Shell Programming and Scripting

Splitting Single line into multiple line

Hi All, I am reading a line from a file and writing it to other file. Whenever I got a particular line then I want that line to be splited into 4 line and written it to new file. e.g My line is U_ABC connector3 pin24E connector4 pin25E connector5 pin26E connector6 pin27E connector7... (2 Replies)
Discussion started by: diehard
2 Replies

4. Shell Programming and Scripting

Merge multiple lines in one line

Hi guys, So i have a input file with several sequences aligned (fasta) >NC_005930 241 bp MNMINIFIINNIFDQFIPVKLSIFSLTSVGSIIA LSWVWINTKTHWAISRSNTP-SLLLNSL WTLLITNL-NEKTNPWAPWLFSLFLLCFSFNIMSLI-PYTF-SQ TSHLSFTFGLSLPIWIMVNIAGFKNNWKKKISHLLPQGTPIYLVPVMII IETISLFIQPLTLGFRLGANLLAGHLLIFLCSCTIWE... (6 Replies)
Discussion started by: andreia
6 Replies

5. Shell Programming and Scripting

Merge multiple lines to one line when line starts with and ends with

example: comment Now_TB.table column errac is for error messages 1 - first 2 - second 3 -third ; in this example I need to be able to grab the comment as first word and ; as the last word and it might span a few lines. I need it to be put all in one line without line breaks so I can... (4 Replies)
Discussion started by: wambli
4 Replies

6. Shell Programming and Scripting

splitting a huge line of file into multiple lines with fixed number of columns

Hi, I have a huge file with a single line. But I want to break that line into lines of with each line having five columns. My file is like this: code: "hi","there","how","are","you?","It","was","great","working","with","you.","hope","to","work","you." I want it like this: code:... (1 Reply)
Discussion started by: rajsharma
1 Replies

7. Shell Programming and Scripting

Multiple lines into a single line

Hi, I've some files with the following data and i need to convert the lines between the separator ---, into a single line. I've tried with the paste cmd but my main problem is that the number of lines between the separator is not fix, it can very between 1-4 lines. Input --- 2010-02-22... (4 Replies)
Discussion started by: RickyC9999
4 Replies

8. Shell Programming and Scripting

Multiple lines into one line

Hi all I have a file with these lines good gone home What code do you need to put this on one line like this good gone home (2 Replies)
Discussion started by: hawaiifiver
2 Replies

9. Shell Programming and Scripting

Splitting the line in multiple lines

Hi Guys, I need a help. I am pretty new to the shell level programing. I was trying to split a long lines to a multiple lines with few conditions. I goggle for the code and found some snippets and tried to modified it but I got few strange problems too. I have to split the lines if line is ... (3 Replies)
Discussion started by: dd_sh
3 Replies

10. Shell Programming and Scripting

Splitting a single line into multiple lines

I have a case where, I need to look into a file. Go to each line of the file, find the length of the line, if the length of the line is more than 75 chars, I need to split the line into multiple lines of 75chars max. If the length of the line is less than 75, we need not do anything. So at the... (4 Replies)
Discussion started by: thanuman
4 Replies
Login or Register to Ask a Question