Remove the last character (,) for every line in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove the last character (,) for every line in a file
# 1  
Old 12-09-2012
Wrench Remove the last character (,) for every line in a file

Good afternoon:

im working wih 2 files to find differences and use the cmp command

Code:
cmp file1 file2
file1 file2 are are diifferent char 302 line1

i found what the difference is with the sed command and that is the file1 at the end of every line has a (,) (comma) character.
i.e

Code:
sed -n l file1|head -5
,1790365,1,F,1,01,00,20121205,23305300,17.00,0000001700,03214534032,0\
3187170198,I,,0,,,N,I,20121207 001649,EDFN,,,0,I,STD,COMCE,BSC,COMBOG\
EG,TAND,27,0404,MOB-315,ENT,ENT-MOB,MM01,Telefonos de BSC,N,0,T_D,1,1\
.00,60.00,17.00,MINRED,1,COMCE,TO,PCO,0.000000,0.000000,MINUTO,1.00,1\
,0.000000,0.00,0.00,,0.00,\r$

Code:
sed -n l file2|head -5
1790365,1,F,1,01,00,20121205,23305300,17.00,0000001700,03214534032,0\
3187170198,I,,0,,,N,I,20121207 001649,EDFN,,,0,I,STD,COMCE,BSC,COMBOG\
EG,TAND,27,0404,MOB-315,ENT,ENT-MOB,MM01,Telefonos de BSC,N,0,T_D,1,1\
.00,60.00,17.00,MINRED,1,COMCE,TO,PCO,0.000000,0.000000,MINUTO,1.00,1\
,0.000000,0.00,0.00,,0.00\r$

I want to remove the last character (,or comma) in every line for file1, dont know much about sed and i tried with the following:
Code:
sed 's/,$//g' file1 > file4

the above command was likely to work but it didnt since i run the the cmp command yieldiing the same error in the same line and character:

Code:
cmp file1 file4
file1 file2 are are different char 302 line1

Id appreciate your help in advanced

Last edited by Scott; 12-09-2012 at 07:38 PM.. Reason: Final reminder to add code tags
# 2  
Old 12-09-2012
I see a comma at the beginning as well, try this command:-
Code:
sed 's/^,//g; s/,...$/\\r\$/g' file1 > file4


Last edited by Yoda; 12-09-2012 at 08:21 PM..
# 3  
Old 12-09-2012
thanks, but the last command remove the comma at the beginning but at the end of the line remains but what i want is just to remove the last comma before the r$ character (End of line). this is the output

Code:
sed -n l file4|head -5
1790365,1,F,1,01,00,20121205,23305300,17.00,0000001700,03214534032,03\
187170198,I,,0,,,N,I,20121207 001649,EDFN,,,0,I,STD,COMCE,BSC,COMBOGE\
G,TAND,27,0404,MOB-315,ENT,ENT-MOB,MM01,Telefonos de BSC,N,0,T_D,1,1.\
00,60.00,17.00,MINRED,1,COMCE,TO,PCO,0.000000,0.000000,MINUTO,1.00,1,\
0.000000,0.00,0.00,,0.00,\r$

Id appreciate your help thanks in advanced
Moderator's Comments:
Mod Comment Please use code tags


---------- Post updated at 09:57 PM ---------- Previous update was at 08:17 PM ----------

furthermore i tested to substitute with a string and then grep it and i couldnt find the substitution i.e
Code:
sed 's/,$/TestStr/g' file1 > file4

grep -i TestStr  file4
found nothing

now another doubts arises: this is a 100000 records file i want to process, and im working with ubuntu 10.1 with bourne shell and then want to run in a HP unix,so dont know if the version of the sed command has something to do ?

Last edited by Scott; 12-10-2012 at 12:17 AM.. Reason: Code tags
# 4  
Old 12-09-2012
please do a dos2unix on source files first. It is confused to work on a DOS-format file in Unix env.

Code:
dos2unix file1 file1
dos2unix file2 file2

# 5  
Old 12-11-2012
OK ive got it, i run the same command (sed 's/^,//g; s/,...$/\\r\$/g' file1 > file4) in the server HP-UX and work it out succesfully working with the same file. the server is :
HP-UX proetldb01c B.11.31 U ia64 4072542272 11 but in my computer with Ubuntu installed didnt work. So the problem has something to do with the version of sed in Ubuntu ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to remove $ or new line character in a file?

Hi All, Could any one suggest how to remove $ symbol in a text file when i am opening in vi editor. Scenario; For example iam having a file name aaa.txt the data inside the file is like sample name when i am opening in vi editor The same file resembles like below when i am... (1 Reply)
Discussion started by: Chandru_Raj
1 Replies

2. Shell Programming and Scripting

How to Remove comma as last character in end of last line of file?

how to Remove comma as last charector in end of last line of file: example: input file --------------- aaaaaa, bbbbbb, cccc, 12345, ____________ output file : ----------- aaaaaa, bbbbbb, (6 Replies)
Discussion started by: RahulJoshi
6 Replies

3. UNIX for Dummies Questions & Answers

Remove last character in each line

Hi guys, Does anyone know how to remove the last character in each of the line? This is what I have: ABCDE.1 GLSJD.2 HIJPL.2 HKAGB.3 IUBWQ.1 What I want (remove the dot and number): ABCDE GLSJD HIJPL HKAGB IUBWQ I tried to use this: sed 's/.*//' But I'm not sure if that is... (3 Replies)
Discussion started by: narachaid
3 Replies

4. Shell Programming and Scripting

Remove new line character and add space to convert into fixed width file

I have a file with different record length. The file as to be converted into fixed length by appending spaces at the end of record. The length should be calculated based on the record with maximum length in the file. If the length is less than the max length, the spaces should be appended... (4 Replies)
Discussion started by: Amrutha24
4 Replies

5. Shell Programming and Scripting

How to remove , if first character on line

Hi, I have a file with lines such as the below. I want to remove the comma only if it is the first character on a line. I can't work out how to do this using sed. *ELSET, ELSET=WHEEL_TD2 63, 64, 65, 72, 82, 88, 89, 92, 120, 121, 152, 181, 190, 221, 252, 259 , 260, 282, 283, 285, 286,... (2 Replies)
Discussion started by: carlr
2 Replies

6. Shell Programming and Scripting

How to remove new line character at end of file.

I need to remove new line character from end of file. Suppose here are content. a|b|c|d|r a|b|c|d|r a|b|c|d|r <new line> that means file contains 4 lines but data is there in 3 lines. so I want that only 3 lines should be there in file. Please help (20 Replies)
Discussion started by: varun940
20 Replies

7. Shell Programming and Scripting

How to remove line break character in a file

Hi, we are trying to process a csv file,in which we are getting data with line breaks.How to remove the line break character in the file? when i try to print the line break charcter using od -c,it gives as '\n' character for both line break and line feed. Please provide your valuable... (6 Replies)
Discussion started by: cnraja
6 Replies

8. HP-UX

How to remove new line character and append new line character in a file?

Hi Experts, I have data coming in 4 columns and there are new line characters \n in between the data. I need to remove the new line characters in the middle of the row and keep the \n character at the end of the line. File is comma (,) seperated. Eg: ID,Client ,SNo,Rank 37,Airtel \n... (8 Replies)
Discussion started by: sasikari
8 Replies

9. UNIX for Advanced & Expert Users

To remove new line character

Hi, I am facing one interesting problem : I have a file which contains data like this 459,|1998-11-047|a |b |c \n efg | d|e | \n 459,|1998-11-047|a \n c|b |c \n efg | d|e | \n Basically what I have to do is , I have to remove all \n which is coming ( enclosed ) in between... (7 Replies)
Discussion started by: shihabvk
7 Replies

10. Shell Programming and Scripting

Remove Last Character of Line

Hi, I need to put the single line contents of a file into a variable, but remove the last character, for example the file would have this sort of contents: 2;4;3;10;67;54;96; And I want the variable to be: 2;4;3;10;67;54;96 (notice the last ";" has gone). Unfortunately I can't just... (4 Replies)
Discussion started by: danhodges99
4 Replies
Login or Register to Ask a Question