Replace commas with newlines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace commas with newlines
# 1  
Old 05-18-2012
Replace commas with newlines

Good afternoon,

I am trying to read user input.

Here is what I have so far:

Code:
echo "Type the Container ID for every container that you want subnets exported"
echo "for (with comma between each one, for example... 1,45,98)"
echo -n "if you want every one listed, then just type ALL in caps and press [ENTER]: "
read containerid

This is an example of what $containerid looks like when I do it:

Code:
1,2,3,4

So my question is two fold. Am I getting the input the best way? Is there another way for every thing that the user enters be on a seperate line?

Otherwise, I was just thinking I could use sed to replace the commas with a newline, so that the $containerid ends up being:

Code:
1
2
3
4

What I am trying to do here is to get this all into a file so that I can do something like this:

Code:
for j in `cat $containerid`
do
export $j
done

Thanks for all of your help!

---------- Post updated at 02:54 PM ---------- Previous update was at 02:52 PM ----------

Playing with tr, I got this.

Code:
echo 1,2,3,4 | tr ',' '\n'

and it gives me:
Code:
1
2
3
4

So now I guess the question is....am I doing this the best way.

With UNIX there is always more than one way to do it...do you see a better?
# 2  
Old 05-18-2012
You don't need a file to do that.

If you did have a file, that'd be the wrong way to use one. See useless use of backticks and useless use of cat.

It's time to learn what the special IFS variable does Smilie It's a list of characters that the shell splits on, by default any whitespace.
Code:
echo "Type the Container ID for every container that you want subnets exported"
echo "for (with comma between each one, for example... 1,45,98)"
echo -n "if you want every one listed, then just type ALL in caps and press [ENTER]: "
read containerid

OLDIFS="$IFS" # Save the default for later
IFS="," # This alters what variables split upon.

for X in $containerid # Usually, would split on spaces, but now splits on ,
do
        echo "containerid $x"
done

IFS="$OLDIFS" # Put it back, or things won't split normally later

If you had your users type in 1 2 3 instead of commas, you wouldn't need to alter IFS at all.
# 3  
Old 05-18-2012
You could even do IFS=" ," so it'd work whether your users typed in 1,2,3 or 1 2 3
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Remove newlines

Hi buddy's my file are like this: s.no,name,band,sal 1,"suneel",,10 2,"bargav sand",,20 30," ebdug gil",,4 but i want s.no,name,band,sal 1,"suneel",,10 2,"bargav sand",,20 30,"ebdug gil",,4 any command or Shell script for this. please help me it's urgent to implement (33 Replies)
Discussion started by: Suneelbabu.etl
33 Replies

2. Shell Programming and Scripting

Inverted commas replace

Hi, My input file is like this chr1 + "NM_1234" chr1 - "NM_1234" If I want my third column to contain both the first and second column values, how do I do it? I know how to do it by including the column numbers, but I want the values before the inverted commas. So, my output would... (5 Replies)
Discussion started by: jacobs.smith
5 Replies

3. Shell Programming and Scripting

HELP with AWK or SED. Need to replace the commas between double quotes in CSV file

Hello experts, I need to validate a csv file which contains data like this: Sample.csv "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 "ABCD","I",23,0,9,,"23/12/2012","OK","Street,State, 91135",0 I just need to check if all the records contain exactly the number of... (5 Replies)
Discussion started by: shell_boy23
5 Replies

4. Shell Programming and Scripting

Replace field with commas with field without commas

Hey guys, I have the following text: 1,2,3,4,5,6,'NULL','when',NULL,1,2,0,'NULL' 1,2,3,4,5,6,'NULL','what','NULL',1,2,0,1 I need the same text with the word NULL without commas u know something like this: 1,2,3,4,5,6,NULL,'when',NULL,1,2,0,NULL 1,2,3,4,5,6,NULL,'what','NULL',1,2,0,1 ... (1 Reply)
Discussion started by: lmyk72
1 Replies

5. Shell Programming and Scripting

Delete newlines after every one space

Hi All, I have a file which looks like this: abc 3456 computer 3214 printer 0.9823 computer 3214 Can anyone please let me know how I can format my text like this? abc 3456 computer 3214 printer 0.9823 computer 3214 I know how to space to newlines using tr but don't know how to do... (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

6. Shell Programming and Scripting

Extract pattern before two newlines

Hi All, My file looks like this: 1 2 3 3 4 5 6 7 8 8 7 6 3 4 5 3 6 7 3 4 5 1 2 4 3 4 6 2 4 6 As you can see there are two newlines after the next pattern of numbers begin. (4 Replies)
Discussion started by: shoaibjameel123
4 Replies

7. Shell Programming and Scripting

replace but skip data between certain commas

OK, I am one needy dude. However, how can I make the program NOT change any of the values BETWEEN the first and second "," ? I dont want any of the numbers changed that are preceded by "AT". I want ALL other numeric values > 300 changed to 300. cat qin.csv |head... (4 Replies)
Discussion started by: herot
4 Replies

8. Shell Programming and Scripting

parsing log files, removing spaces and replace with commas

Hello all i am working on a database to import log files from my systems, but i cannot seem to find the answer. I searched here for a good bit and couldnt peice together what i was looking for. I know you could do this with awk, i just dont know how. Any help would be greatly appreciated.... (6 Replies)
Discussion started by: caddyjoe77
6 Replies

9. Shell Programming and Scripting

replace one or more tabs with commas

Hi, Can any one tell me how to replace one or more tabs from start of the line and in between the words with commas in the file using unix commands? My actual data in the text file is as below with spaces.The spaces are not being shown in the post..please see them while replying to the post.... (1 Reply)
Discussion started by: tucs_123
1 Replies

10. UNIX for Dummies Questions & Answers

Inserting commas and replacing backslashes with commas

Hi, Newbie here. I have a file that consists of data that I want to convert to a csv file. For example: Jul 20 2008 1111 / visit home / BlackBerry8830/4.2.2 Profile/MIDP-2.0 Configuration/CLOC-1.1 VendorID/105 Jul 21 2008 22222 / add friend / BlackBerry8830/4.2.2 Profile/MIDP-2.0... (3 Replies)
Discussion started by: kangaroo
3 Replies
Login or Register to Ask a Question