Inserting commas and replacing backslashes with commas


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Inserting commas and replacing backslashes with commas
# 1  
Old 11-10-2008
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 Configuration/CLOC-1.1 VendorID/105
Jul 22 2008 333 / view profile / Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)

So I want to separate the data into 4 columns:
1) Date
2) ID number
3) Action
4) Browser type

The date is the first 11 characters. The ID is a varying numeric string after the date. And the action and browser type follows.

I need to insert a comma after the date, and replace the '/' in between the ID number and action, and in between action and browser type with commas.

Any ideas? Thanks in advance =)
# 2  
Old 11-11-2008
Hammer & Screwdriver Perhaps this will work for you

Code:
> cat file65
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 Configuration/CLOC-1.1 VendorID/105
Jul 22 2008 333 / view profile / Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)

> cat adj65.sh
#! /usr/bin/bash

IFS="/"
while read fld1 fld2 fld3 
   do
   dtf=$(echo $fld1 | cut -d" " -f1-3)
   idf=$(echo $fld1 | cut -d" " -f4) 

   echo "${dtf},${idf},${fld2},${fld3}"
done <file65

> adj65.sh
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 Configuration/CLOC-1.1 VendorID/105
Jul 22 2008,333, view profile , Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)

# 3  
Old 11-11-2008
Code:
sed 's/\(.\{11\}\)\( \)\(.*\)\( \/ \)\(.*\)\( \/ \)\(.*\)/\1,\3,\5,\7/' file

Code:
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 Configuration/CLOC-1.1 VendorID/105
Jul 22 2008,333,view profile,Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)

# 4  
Old 11-11-2008
Awesome

Thanks for your help! It worked like a charm =)

I have an additional question. How would I write a basic script that will loop the same command above to multiply files in the same folder and create the corresponding files.

For example, using the "sed" solution:
sed 's/\(.\{11\}\)\( \)\(.*\)\( \/ \)\(.*\)\( \/ \)\(.*\)/\1,\3,\5,\7/'

I want to apply it so that:

file1 > file1_s
file2 > file2_s
file3 > file3_s

and so on...

Thanks!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert commas

Original content: ACACCTCAT 129 | ACACCTCAT 0 ACACCTCATX 171 | ACACCTCATX 0 ACACRESRT 0 ACACRESRT 0 ACACRESRTX 0 ... (4 Replies)
Discussion started by: loktamann
4 Replies

2. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

3. Shell Programming and Scripting

Removing just the trailing commas :-(

Hi all, I haven't needed to do any shell based editing for nearly 20 years, and no amount of searching around has found me a solution to this very simple problem :-( I have a csv file. Some lines have three commas at the end. This means the invoice hasn't been paid. I'd like to use sed / grep... (4 Replies)
Discussion started by: chardyzulu
4 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

Inserting commas into file at set locations...

Hey everyone. What would be the best method to insert commas (or comma-quotes) into a file at set locations. Every line in the file looks like this: 1234567890123456 123456BIGAUDIODYNAMITE33 123.12 123456 12345678901234MARK E WILLIAMS 123456 The comma's should... (6 Replies)
Discussion started by: Astrocloud
6 Replies

6. Shell Programming and Scripting

Commas within Delimeters

Hi experts, I would like a favour from you guys to get the info from 5th column which was separated by the delimeter comma ( , ) The Data file is as below:- 1,USER1,"90, TEST AVENUE, OLD ROAD",test1,124,N 2,USER2,88 TEST STREET NEW ROAD,test2,123,N The User File is as below:- USER1... (1 Reply)
Discussion started by: shenkz
1 Replies

7. Shell Programming and Scripting

Help with sed and replacing white spaces with commas

Dear all, I am in a bit of a quandary. I have 400 text files which I need to edit and output in a very specific way. Here is a sample text file copied from gedit ... The columns will come out a bit messed up but when I cat <file>, it gives a table with six columns (0-28, tot lob vol, vcsf,... (6 Replies)
Discussion started by: antonz
6 Replies

8. Shell Programming and Scripting

Replacing 3 backslashes with 2 in a string

Hi, I have a requirement where i need to replace 3 backslashes with 2 in the below mentioned string, but i am facing problem with backslashes. However i tried the option below but doesnt work. My input remains a constant as i receive it from my upstream. Input String= "-rfile... (3 Replies)
Discussion started by: kk_madrid
3 Replies

9. Shell Programming and Scripting

Need help in removing commas

i have the below line as output from a script. I want to delete the string "," and get the output without comma, cat D* | grep "bytes free" | awk '{print $3}' | ????? output: 40,966,189,056 Desired O/P: 40966189056 (1 Reply)
Discussion started by: ali560045
1 Replies

10. Shell Programming and Scripting

replacing commas with tilde in csv file.

hello all, i have a comma delimited file. i want to replace the commas in the file with the tilde symbol using sed. how can i do this? thanks. (4 Replies)
Discussion started by: femig
4 Replies
Login or Register to Ask a Question