append file by adding commas


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting append file by adding commas
# 1  
Old 12-23-2009
Question append file by adding commas

Hi,

Iam working on Sun solaris machine.I have a file with ids in the below format.

1
2
3
4
5

I want it as:
1,2,3,4,5

I use these commands individually to get into that format:

vi file
/1,$s/$/,/g
This adds commas in the file and then i save it.
next i use this command : cat file | tr '\n' ' '
which gets into the format as 1,2,3,4,5.

Now i want to write a script which does this work ie.read content from file add commas and then bring in the required format.Can someone help me out??
jyothi_wipro
# 2  
Old 12-23-2009
1 command:
Code:
perl -le 'print join ",", map { chomp; $_ } <>;' file

# 3  
Old 12-23-2009
Thanks a lot.can u please tell me how will i redirect these ids into a file.
ie the output of this command perl -le 'print join ",", map { chomp; $_ } <>;' file into another file.
jyothi_wipro
# 4  
Old 12-23-2009
The same way you'd redirect the output of any other command:
Code:
 cmd > file

# 5  
Old 12-23-2009
Ya I recalled that after i sent the post :$
Anyways Thanks!!!Smilie

---------- Post updated at 04:00 PM ---------- Previous update was at 03:42 PM ----------

Is there any way that mysql query reads the content from this file and selects matched data from another table.
ie I have a table named xyz which has same ids as in that file.
I want this query to get count of the ids from xyz file by matching the ids.

at mysql prompt i run this command which gets me the count from xyz table.
mysql>select count(*) from xyz where id in (1,2,3,4,5);
Now i want the sql query to read the file and match these ids with ids in xyz table and get me the count.
jyothi_wipro
# 6  
Old 12-23-2009
There is, but I'd recommend starting a new thread for this, as it's basically a new question.
# 7  
Old 12-23-2009
Thanks and Ok.
jyothi_wipro
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Removing commas from CSV file

Hi I'm creating a sh script to generate a csv file. The CSV contains the values from a sql table. The content looks this: a,b,c,c2,c3,,,,,,,,,,,d,e I have some code that can separate the fields using the comma as delimiter, but some values actually contain commas, such as... (2 Replies)
Discussion started by: preema
2 Replies

2. Shell Programming and Scripting

Adding character and append last element of second column

Hi, could you help me in processing this file under bash? I need to add some text to the first line and then append the last element of the second columns. The input file is tab separated while the output should be space separated. input file is 1.00E-02 2.00E-02 4.465E+17 2.00E-02 3.00E-02... (4 Replies)
Discussion started by: f_o_555
4 Replies

3. UNIX for Advanced & Expert Users

Help in adding a string at the end of each line and append files vertically

hi, i need a help in the script , need to append a string at the end of each line of a files , and append the files into a single file vertically. eg file1 has the following columns abc,def,aaa aaa,aa,aaa files 2 has the following rows and columns abc,def,aaa aaa,aa,aaa i... (3 Replies)
Discussion started by: senkerth
3 Replies

4. Shell Programming and Scripting

Adding Extra Commas to a CSV file

Trying in this forum. Not sure if it is permitted.... but in need of help. Please find the requirements in the below link. https://www.unix.com/unix-dummies-questions-answers/191503-add-extra-commas-csv-file-2.html#post302665179 Thanks in Advance. (1 Reply)
Discussion started by: chillblue
1 Replies

5. Shell Programming and Scripting

Remove leading commas in the file

Hi , I have a file with below records 965382,10310858,1,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 767010,10217614,3,10217616,10217622,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,... (4 Replies)
Discussion started by: vputtas@gmail.c
4 Replies

6. Shell Programming and Scripting

Bash: adding commas in for loop

Dear Experts I think this is possibly the easiest thing. but I am not able to solve: I need comma to be added to end of each line echo'd. But does not want it to be added to last line. I have a script which does some data analysis and creates a command as in below code snippet for... (4 Replies)
Discussion started by: chakrapani
4 Replies

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

8. Programming

Removing empty spaces and adding commas

I have a file which contains numbers as follows: 1234 9876 6789 5677 3452 9087 4562 1367 2678 7891 I need to remove the empty spaces and add commas between the numbers like: 1234,9876,6789,5677,3452, 9087,4562,1367,2678,7891 Can anyone tell me the command to do... (4 Replies)
Discussion started by: jazz
4 Replies

9. UNIX for Dummies Questions & Answers

Question on files - adding commas at the end

Hi All, I have a file. Each line suppose to contain 188 fields delimited by , (comma --> so 187 commas) but some records have less than 188 fields which needs to be filled by additional commas at the end. Ex: a file wth 5 columns and 4 commas test1,test2,test3 red1,red2... (5 Replies)
Discussion started by: jingi1234
5 Replies

10. Shell Programming and Scripting

tab delimited file to commas

Hi there Just wondered if someone could help me out I have a file that has been delimited by tabs, ie field1<tab>fiield2<tab>field3 Does anybody know a command that will convert tabs to commas throughout the entire file? Note: there are a number of unpopulated fields in the file so... (6 Replies)
Discussion started by: hcclnoodles
6 Replies
Login or Register to Ask a Question