Combine multi-row cell into a single line


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Combine multi-row cell into a single line
# 1  
Old 10-30-2018
Combine multi-row cell into a single line

My CSV file looks similar to this example (the K, L, and M are in the same cell as J but each on a new line within that cell):

Code:
1,  A,  B,  C,  D
2,  E,  F,  G,  H
3,  I,   J  N,  O,
          K
          L
          M,
4,  P,   Q,  R,  S

I would like to have it look like this:

Code:
1,  A,  B,  C,  D
2,  E,  F,  G,  H
3,  I,   J K L M,  N,  O
4,  P,   Q,  R,  S

Any ideas on how I can reformat that multi-line cell?




Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 10-31-2018 at 08:46 AM.. Reason: Added CODE tags.
# 2  
Old 10-30-2018
We can give you better answers if you tell us what UNIX you have and what shell you are using - please post the output of these two simple commands:
Code:
echo $SHELL
uname -a

# 3  
Old 10-30-2018
/bin/bash

Linux 2.6.32-696.28.1.el6.x86_64 #1 SMP Thu Apr 26 04:27:41 EDT 2018 x86_64 x86_64 x86_64 GNU/Linux

Last edited by Kim Ashby; 10-30-2018 at 02:15 PM..
# 4  
Old 10-30-2018
I don't understand what you're trying to do.

Is it that for any line that does not start with a number you want to add the text of that line to the 3rd field of the previous line just before the last space in that field? What happened to the comma after the O in your input file?

What have you tried to solve this problem on your own?

Where are you stuck trying to get the results you want?
# 5  
Old 10-31-2018
On top of what Don Cragun did ask, can this spread happen to other cells as well? How to tell which cell is involved? Are there additional control chars in the cell indicating a split?
# 6  
Old 11-01-2018
Combine data with the Ampersand symbol (&)
Select the cell where you want to put the combined data.
Type = and select the first cell you want to combine.
Type & and use quotation marks with a space enclosed.
Select the next cell you want to combine and press enter. An example formula might be =A2&" "&B2.
# 7  
Old 11-01-2018
Are you able to install csvtool or csvkit? I know the former is able to work with multiline cells in a csv file; I don't know about the latter.

csvtool has a "call command" option allowing it to call a shellscript or shell function to modify the file one line at a time.

I think for the example you gave:
Code:
flatten() { echo "$1","$2","${3//$'\n'/ }","$4","$5" }
export -f flatten
csvtool call flatten file.csv

This is untested and you will have to try it out.

csvtool has the ablilty to cut and paste columns just like the cut and paste tools, so it might be easier to pull out the problem column, fix it as above. and paste it back into the original file.

I think you need version 4 of bash to get the $'' functionality, or look at using tr: tr '\r' ' '

Andrew
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multi line log files to single line format

I want to read the log file which was generate from other command . And the output was having multi line in log files for job name and server name. But i need to make all the logs on one line Source file 07/15/2018 17:02:00 TRANSLOG_1700 Server0005_SQL ... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

2. Shell Programming and Scripting

Help with reformat single-line multi-fasta into multi-line multi-fasta

Input File: >Seq1 ASDADAFASFASFADGSDGFSDFSDFSDFSDFSDFSDFSDFSDFSDFSDFSD >Seq2 SDASDAQEQWEQeqAdfaasd >Seq3 ASDSALGHIUDFJANCAGPATHLACJHPAUTYNJKG ...... Desired Output File >Seq1 ASDADAFASF ASFADGSDGF SDFSDFSDFS DFSDFSDFSD FSDFSDFSDF SD >Seq2 (4 Replies)
Discussion started by: patrick87
4 Replies

3. UNIX for Dummies Questions & Answers

Remove multi line and single line comments

Hi, I am trying to remove multi line and single line comments like examples below I have tried this pattern. it works fine for single line comments and multi line comments in a single line only. but this fails when the comments are extended in multiple lines as shown in the comment 2 of... (3 Replies)
Discussion started by: ahmedwaseem2000
3 Replies

4. Shell Programming and Scripting

Joining multi-line output to a single line in a group

Hi, My Oracle query is returing below o/p ---------------------------------------------------------- Ins trnas value a lkp1 x a lkp1 y b lkp1 a b lkp2 x b lkp2 y ... (7 Replies)
Discussion started by: gvk25
7 Replies

5. Shell Programming and Scripting

Multi lines to single line

HI, My input file contains the data as like below: A1234119993 B6271113 Bghjkjk A1234119992 B6271113hi Bghjkjkmkl the output i require is : A1234119993 B6271113 Bghjkjk A1234119992 B6271113hi Bghjkjkmkl Please help me in this. Thanks (6 Replies)
Discussion started by: pandeesh
6 Replies

6. Shell Programming and Scripting

Merge multi-line output into a single line

Hello I did do a search and the past threads doesn't really solve my issue. (using various awk commands) I need to combine the output from java -version into 1 line, but I am having difficulties. When you exec java -version, you get: java version "1.5.0_06" Java(TM) 2 Runtime... (5 Replies)
Discussion started by: flagman5
5 Replies

7. Shell Programming and Scripting

Transpose multi-line records into a single row

Now that I've parsed out the data that I desire I'm left with variable length multi-line records that are field seperated by new lines (\n) and record seperated by a single empty line ("") At first I was considering doing something like this to append all of the record rows into a single row: ... (4 Replies)
Discussion started by: daveyabe
4 Replies

8. 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

9. Shell Programming and Scripting

How to use Perl to join multi-line into single line

Hello, Did anyone know how to write a perl script to merge the multi-line into a single line where each line with start at timestamp Input--> timestamp=2009-11-10-04.55.20.829347; a; b; c; timestamp=2009-11-10-04.55.20.829347; aa; bb; cc; (5 Replies)
Discussion started by: happyday
5 Replies

10. Shell Programming and Scripting

How to use Perl to merge multi-line into single line

Hi, Can anyone know how to use perl to merge the following multi-line information which beginning with "BAM" into one line. For each line need to delete the return and add a space. Please see the red color line. ******Org. Multi-line) BAM admin 101.203.57.22 ... (3 Replies)
Discussion started by: happyday
3 Replies
Login or Register to Ask a Question