Visit Our UNIX and Linux User Community

sed multilines + separator confusion !!

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed multilines + separator confusion !!
# 1  
Old 11-03-2014
Wrench sed multilines + separator confusion !!

Hi Seders,

i am new to this forum, but i think it's quite the best place to post.
So, here is my pb :

I have a csv file, with comma separator and text enclosed by ".

First pb is with text in " ......... ", wich sometimes includes lines break, and commas
And to complicate a little more, there can have many text fields on one line.

Which make it difficult to pushup lines and keep the good field number.
Which also make it impossible to use /,{87}/ s/....// to filter complete lines.

i spend quite a big time trying to reformat the lines using sed , but without success.

The file comes from gmail contacts, and is 88 fields ( so 87 separators ) .

You'll find an example file in attachment

I feel quite lost on this one, so any help highly welcome,
thanks in advance
# 2  
Old 11-03-2014
There have been presented umpteen solutions to similar problems in these fora. In this case, and for exactly your sample (which BTW has 89 fields per line), try
sed ':A;/,$/!N; s/\n//;tA'  /tmp/sed_example.txt

# 3  
Old 11-03-2014
Ok, RudiC, thanks for your answer, i tryied this one but it does not the trick.

This case can happen :

,,,,,,, " ..., some text \r\n
,,,,, \r\n

Here you push every line up, if the pattern space line doesn't end by , until the line ends by a comma .
Which doesn't mean the line is well formed, see this case :

,,,, " text text text text \r\n
,, some text, \r\n
,,, blabla "

here the second line get up, ends by a comma and the script stop and work with next line ...

hence this solution work with well endeing lines, but not with broken ones

i'd like to do it the sed way, but for the moment i can't figure out a proper solution.

Thanks again
# 4  
Old 11-03-2014
hi yogeek,
i can try to summarize for commas as a an idea.
so its like below.
your example file:
# cat testfile
0153400301,,,,,,,,,,,,,,,,,,,,0153400301,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,,Mes contacts;Importation du 25/10/2014,
0240153346,,,,,,,,,,,,,,,,,,,,0240153346,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,Mes contacts;Importation du 25/10/2014,
0687449343,,,,,,,,,,,,,,,,,,,,0687449343,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,Mes contacts;Importation du 25/10/2014,
1&1,,,,,,,,,,,,,"Client : 27004633
num contrat : 30081506
pw : kiaybi1
Client : 27004633
num contrat : 30081506
pw : kiaybi1",,,,,,,+33240151245,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,Mes contacts;Importation du 25/10/2014,
2,42,25,,,,,,,,,,,,,,,,,,24225,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,Mes contacts;Importation du 25/10/2014,
3G Bouygues Telecom,,Adrien Corre,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,Mes contacts;Importation du 25/10/2014,
3iX,,,,,,,,,,,,,"Client area :
Log : mail
pw : classic
cpanel : url:2082
afflink :

ftp / cpanel / ssh :
k75J1FioOv",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,Mes contacts;Importation du 25/10/2014,
660,,,,,,,,,,,,,,,,,,660,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,Normal,,Mes contacts;Importation du 25/10/2014,

and try
# awk -F, -va="x" '{for(i=1;i<=NF;i++){if(!$i&&i!=1){x++;printf "%s",$i} else
> {if(x)printf " %s%d%c%s%s %s","[",(x+1),a,FS,"]",$i;else printf "%s",$i;x=0}
> if(i==NF&&x)printf " %s%d%c%c%s","[",x,a,FS,"]"};x=0;print""}' testfile

0153400301 [20x,] 0153400301 [65x,] Normal [3x,] Mes contacts;Importation du 25/10/2014 [1x,]
0240153346 [20x,] 0240153346 [65x,] Normal [2x,] Mes contacts;Importation du 25/10/2014 [1x,]
0687449343 [20x,] 0687449343 [65x,] Normal [2x,] Mes contacts;Importation du 25/10/2014 [1x,]
1&1 [13x,] "Client : 27004633
num contrat : 30081506
pw : kiaybi1
Client : 27004633
num contrat : 30081506
pw : kiaybi1" [7x,] +33240151245 [65x,] Normal [2x,] Mes contacts;Importation du 25/10/2014 [1x,]
24225 [18x,] 24225 [65x,] Normal [2x,] Mes contacts;Importation du 25/10/2014 [1x,]
3G Bouygues Telecom [2x,] Adrien Corre [83x,] Normal [2x,] Mes contacts;Importation du 25/10/2014 [1x,]
3iX [13x,] "Client area :
Log : mail
pw : classic
cpanel : url:2082
afflink :

ftp / cpanel / ssh :
k75J1FioOv" [72x,] Normal [2x,] Mes contacts;Importation du 25/10/2014 [1x,]
660 [18x,] 660 [67x,] Normal [2x,] Mes contacts;Importation du 25/10/2014 [1x,]

# 5  
Old 11-03-2014
OK, try
sed ':A;/Importation du 25\/10\/2014,$/!N; s/\n//;tA'  /tmp/sed_example.txt


Previous Thread | Next Thread
Test Your Knowledge in Computers #769
Difficulty: Medium
Intel Core i7 (2008) has an 8 MB on-die unified L3 cache that is inclusive, shared by all cores.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If then statement confusion

#!/bin/bash PH=(KD 6S TC 3D) #playerhand TCIP=(AH) #topcard in play A=( "${TCIP::1}" ) # A B=( "${TCIP:1}" ) # H C=8 for e in ${PH}; do if ]; then echo "$e " >> /home/cogiz/validcards.txt else echo... (1 Reply)
Discussion started by: cogiz
1 Replies

2. UNIX for Beginners Questions & Answers

sed confusion

#!/bin/bash X=(0 2 4 6 7 0 0 0 0) Let me just say from the start that sed confuses the hell out of me! In the above line of code how can I use sed to remove all of the 0's except the first one? I have tried sed -e 's/*$//g' but it removes all of the 0's. Thank you in advance for any and... (3 Replies)
Discussion started by: cogiz
3 Replies

3. Shell Programming and Scripting

Convert Singleline to Multilines

Hi All, Could you please help to get the below output: Input: J1,ELLA_1,ISDR,JJK,TRS J2,ROSTER,JACK J3,HUP ... ... ... Output: J1,ELLA_1 J1,ISDR J1,JJK J1,TRS (3 Replies)
Discussion started by: unme
3 Replies

4. Shell Programming and Scripting

Confusion with PS

Hello All, I have a problem in counting number of process getting run with my current script name.. Here it is ps -ef | grep $0 | grep -v grep This display just one line with the PID, PPID and other details when i print it in the script. But when I want to count the numbers in my... (11 Replies)
Discussion started by: sathyaonnuix
11 Replies

5. Shell Programming and Scripting

sed to insert a separator

My txt file consists of records with 6 numbers followed by 3 characters. Is there a simple “sed” which will insert a | separator between the 6th and 7th position ? Many thanks (3 Replies)
Discussion started by: malts18
3 Replies

6. Shell Programming and Scripting

Shell Script to convert multilines to single lines

Hi, I need to create an script which reads multi lines and convert it to single line or enter escape sequence at end "\". e.g. #!/bin/sh echo -e "provide me query to run" read query create table test( id int(11), name char); echo $query But it's failing because of multi line... (8 Replies)
Discussion started by: mirfan
8 Replies

7. UNIX for Dummies Questions & Answers

Sed - Get Separator From String

Hi All, I have the following data in a korn shell variable: a="FirstValue|SecondValue|ThirdValue" The value between "FirstValue", "SecondValue" and "ThirdValue" can change, in this case is a comma: "," and I need to print it only once. I need to know what is the separator value. I... (3 Replies)
Discussion started by: felipe.vinturin
3 Replies

8. Shell Programming and Scripting

Trying to change date separator with sed

Hi there I am trying to convert some date seperators in a large newline delimited file. each line i am interested in has a date in the format 27/05/2009 all I want is to convert the slashes to tildes(~) I have come up with the following code but it does nothing. sed... (5 Replies)
Discussion started by: RadRod
5 Replies

9. UNIX for Dummies Questions & Answers

'tr' confusion

Good day, everyone! Could anybody explain me the following situation. If I'm running similar script: Var="anna.kurnikova" Var2="Anna Kurn" echo $Var | tr -t "$Var" "$Var2" Why the output is : anna KurniKova instead of Anna Kurnikova? :confused: Thank you in advance for any... (2 Replies)
Discussion started by: Nafanja
2 Replies

10. Shell Programming and Scripting

Sed confusion

Hello all, I am trying to delete all the lines in a particular file having a pattern. The problem is that it has special characters and for some reason is not doing the job. For eg. src_file /home/test/filelist.txt :xxxx:ogog /home/test/RCH/ogogogg /home/test/RYHUJ/HHHH... (3 Replies)
Discussion started by: alfredo123
3 Replies

Featured Tech Videos