Replace comma delimiter by newline


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace comma delimiter by newline
# 1  
Old 05-28-2013
Replace comma delimiter by newline

The input file is as below
Code:
 
AR,age,marks,roll,section,evin,25,80,456,A,atch,23,56,789,B,eena,24
,78H245,C,Ps,ot,ecessary,hat,ame comes first then age and rest AR
AZ,kevin,25,80,456,A,Satch,23,56,789,Satch,23,56,789,B,Meena,24,78,H245,C,AZ
................
................

I am writting the below code .
I have to serch the lines containing strings in between the top name.
The top names are AR,AZ...

PHP Code:
echo "Give the top name"
read top
/usr/bin/sed -'/$top/,/$top/p' inputfile | /usr/bin/sed '/,/\\n/g' exclude_file 
example If the top name is AR,output will be
Code:
age
marks
roll
section
evin
25
80
456
A
atch
23
56
789
B
eena
24
78H245
C
Ps
ot
ecessary
hat
ame comes first then age and rest

# 2  
Old 05-28-2013
Code:
awk '/AR/ {f=(!f);next} f' RS=",|\n"

This has a problem since AR is not separate from the text. It then does not print last line.
ame comes first then age and rest
# 3  
Old 05-28-2013
i have modified the input file content as below

Code:
 
AR,age,marks,roll,section,evin,25,80,456,A,atch,23,56,789,B,eena,24
,78H245,C,Ps,ot,ecessary,hat,ame,AR
AZ,kevin,25,80,456,A,Satch,23,56,789,Satch,23,56,789,B,Meena,24,78,H245,C,AZ
................
................

And i have also tried your code...but it is not working.
# 4  
Old 05-28-2013
Is this not what you want (from AR to AR)?
Code:
age
marks
roll
section
evin
25
80
456
A
atch
23
56
789
B
eena
24

78H245
C
Ps
ot
ecessary
hat
ame

# 5  
Old 05-28-2013
Yes,,,thats what i want but the code is not working...after i ran the code it is expecting some input
# 6  
Old 05-28-2013
Code:
echo "AR,age,marks,roll,section,evin,25,80,456,A,atch,23,56,789,B,eena,24
,78H245,C,Ps,ot,ecessary,hat,ame,AR
AZ,kevin,25,80,456,A,Satch,23,56,789,Satch,23,56,789,B,Meena,24,78,H245,C,AZ" | awk '/AR/ {f=(!f);next} f' RS=",|\n"

or
Code:
awk '/AR/ {f=(!f);next} f' RS=",|\n" infile

# 7  
Old 05-29-2013
Hi Jotne,

Thank you for your help..it is working fine.
But AR is not hard coded, I have to keep this value in variable.
If i do it as below, it is not working..Can you pls suggest.

Code:
 
top="AR"
awk '/$top/ {f=(!f);next} f' RS=",|\n" infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace newline with comma and append quotes

Hi, I have below requirement. Apple Orange Banana Required O/p in bash 'Apple,Orange,Banana' Can you please help. Please wrap your samples, codes in CODE TAGS as per forum rules. (3 Replies)
Discussion started by: Rtk
3 Replies

2. Shell Programming and Scripting

Replace spaces with underscores up to first comma but not after the comma

I have a comma delimited file of major codes and descriptions. I want to replace all occurrences of spaces with underscores up to the first comma (only in the first field), but not replace spaces following the comma. For instance I have the following snippet of the file: EK ED,Elementary and... (7 Replies)
Discussion started by: tdouty
7 Replies

3. Shell Programming and Scripting

Replace comma and blank with comma and number

I, I have a file and i need to replace comma and blank space with comma and 0. cat file.txt a,5 b,1 c, d, e,4 I need the output as cat file.txt a,5 b,1 c,0 d,0 (4 Replies)
Discussion started by: jaituteja
4 Replies

4. Shell Programming and Scripting

Command to replace newline with comma

Hi Experts, I need an urgent help in replacing newline characters in my single column file by a comma. I am using following command for this purpose: sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}' parameter.rtf | rev | cut -c 4- | revMy input file has: TABLE_1 TABLE_2 TABLE_3 What I need:... (11 Replies)
Discussion started by: neerndg123
11 Replies

5. Shell Programming and Scripting

How to replace NewLine with comma using sed?

Hi , I have a huge file with following records and I want to replace the last comma with ',NULL'. I try using SED but could not create a correct script . In my opinion I need a script which can convert ,/n with ,NULL/n 1,CHANGE_MEMBER,2010-12-28 00:05:00, 2,CHANGE_MEMBER,2012-09-02... (8 Replies)
Discussion started by: Ajaypal
8 Replies

6. Shell Programming and Scripting

Change the delimiter from Comma to Pipeline

Hello All, I need to convert a csv file to pipeline delimiter file in UNIX. The data in file itself contains comma with double qouted qualifier apart from the comma separator. Let me know how to do it. Appreciate any help if awk can be used to do it. Mentioned below is the sample record of... (14 Replies)
Discussion started by: Arun Mishra
14 Replies

7. Shell Programming and Scripting

Exporting data into Comma Delimiter.

Hi, Requirement: Exporting data from Oracle to UNIX into "Comma" delimiter. Help Needed: I was able to connect to Oracle and import the data. But please let me know while importing the data I need to make it into Comma delimiter flat file. For Example: Source Data - 100 ABC TLead... (6 Replies)
Discussion started by: arunvasu2
6 Replies

8. Shell Programming and Scripting

Replace newline with comma.

I have output from a file like this: 15,01,11,14:06 235 I would like to change this to: 15,01,11,14:06,235 Removing newline and change to "," I now this can be done with tr cat OUT | tr '\n' ','' My problem is that tr is not implemented in this shell. sed is, show it should be... (7 Replies)
Discussion started by: Jotne
7 Replies

9. Shell Programming and Scripting

comma delimiter and space

I have a csv file and there is a problem which I need to resolve. Column1,Column2,Colum3,Column4 ,x,y,z ,d,c,v t,l,m,n ,h,s,k ,k,,y z,j, ,p Now if you see column1 for row 1 and row 4 though they are null there is a space but in case of row2 and row 5 there is no space. I want row... (3 Replies)
Discussion started by: RubinPat
3 Replies

10. Shell Programming and Scripting

Replace comma with newline

Hi, for some reason I cant seem to figure this out. I have a file which looks something like this word word word word word,word,word word word word,word,word,word,word word word Basically I want this whole thing to be a list with 1 word on each line like this... word word word... (1 Reply)
Discussion started by: eltinator
1 Replies
Login or Register to Ask a Question