How to delete a character at the end of a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete a character at the end of a file.
# 1  
Old 07-07-2006
How to delete a character at the end of a file.

I am having a file which has data like this:-
1,2,3,4,5,


But I need to remove the last comma from this file. So the data should be like
1,2,3,4,5


The command I tried was


temp=`cat ${FileName}.txt`

len=`awk '{print length($0)}' ${FileName}.txt`

len=`expr $len - 1`

temp=`expr substr $temp 1 $len`



The problem here i am facing is if the data inside the file is very huge (say 1,2,3,4......9999,10000,)then it is giving error because the command (len=`awk '{print length($0)}' ${FileName}.txt`) returns two values for the variable like

len=10239

8651



Due to this the subsequent command fails.



Any help in this regard please.



Thanks & Regards,

Rony
# 2  
Old 07-07-2006
Code:
 sed 's/,$//' filename > newfilename

# 3  
Old 07-07-2006
Thanks Jim for your quick reply.

But for the sed command also i am getting it correct if the data is less but when it becomes big (say 1,2,3....5000,) it fails to write to the new file. So the new file is created as an empty file.

So is this is a limitation of UNIX or can we have some other work around for the problem.

Please help.

Thanks & Regards,
Rony
# 4  
Old 07-07-2006
Yes. sed, vi, and other UNIX utilities that work on lines in files have a file record length limit. It's usually 2048 bytes per line (or record).

Look into fold to break lines into smaller pieces. Maybe. If you really have 1....5000
then you'll have to use C
Code:
/* cchop.c */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* if line ends in a comma, remove that ending comma */
/* usage  cchop < oldfile > newfile  - ONLY uses stdin stdout*/

int main(void)
{
	char tmp[64000]={0x0};  /*WAAAAY oversize */
	size_t len=0;
	
	while(fgets(tmp,sizeof(tmp),stdin)!=NULL)
	{
		char *p=strchr(tmp,0);
		p--;
		while(*p=='\n' || *p==',') 
		{
			*p=0x0;
			p--;
		}
		fprintf(stdout,"%s\n",tmp);
	}
	return 0;
}

Compile that and try it.
# 5  
Old 07-10-2006
Thanks Jim. But i need to do it with shell script itself.

By the way if i change the structure of my source file as
1,
2,
3,
4,
5,
then the above problem of limitaion of length to a single record won't be there right? In that case how do i get the output as
1,2,3,4,5 ( no comma at the end).

I tried like this
awk 'sub(/$/,""){printf $0;next}
END { sub(/$/,""){printf $0;}
}' filename > newfilename

But this is giving me some error. How do i proceed with this??

Thanks,
Rony
# 6  
Old 07-10-2006
The above command works if I put it like this

awk 'sub(/$/,""){printf $0;next}' filename>newfilename

But then the output would be
1,2,3,4,5,
The last comma (after 5) should not be there in the output.

Any help please
# 7  
Old 07-10-2006
Try this :
Code:
awk '{line=line $0} END {sub(/,$/, "", line) ;print line}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Appending newline character End of File

Hi Gurus, Need help. I'm a beginner in Unix. I have a requirement, need to add or append newline (\n) character in file. Sample Data: 1|Main|Test|~# 2|Main|Hello|~# 3|Main|Unix|~# 4|Main|File|~#Output: 1|Main|Test|~# 2|Main|Hello|~# 3|Main|Unix|~# 4|Main|File|~#\n -- append only... (13 Replies)
Discussion started by: Gouri Solleti
13 Replies

3. Shell Programming and Scripting

How to add newline character at end of file?

Hi All, I have following piece of code in UNIX C Shell script and I want to add one more command which can add newline at the end of file only if there is no newline character exists. foreach file (`ls $dd_PLAYCARD_EDI_IN`) if ( -f $dd_PLAYCARD_EDI_IN/${file} ) then cat -n... (4 Replies)
Discussion started by: jnrohit2k
4 Replies

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

5. Shell Programming and Scripting

add character to every end-of line in file

Hi All I have a file which conatins record.the length of every records is 47. problem : in the end of record i don't have a "\015" character. i want to add this "\015" charcter in the end of every record. the file contains something like 700 records. i've tried with sed command - nothing. ... (8 Replies)
Discussion started by: naamas03
8 Replies

6. Shell Programming and Scripting

add character to the end of each line in file

hi all i have 32 lines in file. the length of each line is 82 , i want that in the end of each line , means in postion 83-84 to put two characters 0d(=\015), 0a(=\012) i want that the 0d will be in postion 83 and the 0a will be in postion 84 in each line of the file how shall i do it ? ... (7 Replies)
Discussion started by: naamas03
7 Replies

7. Shell Programming and Scripting

append a character at end of each line of a file

Hi, i want to append a character '|' at end of each line of a file abc.txt. for example if the file abc.txt conatins: a|b|c 1|2|33 w|2|11 i want result file xyz.txt a|b|c| 1|2|33| w|2|11| I know this is simple but sumhow i am not able to reach end of line. its urgent, thanks for... (4 Replies)
Discussion started by: muaz
4 Replies

8. UNIX for Dummies Questions & Answers

How to add new line character at the end of a file

hi all, i have this question: How to add new line character at the end of a file???? i need this because i am loading a file to sybase and i have problems with the last record thanks for your help (5 Replies)
Discussion started by: DebianJ
5 Replies

9. UNIX for Dummies Questions & Answers

How can I insert character to end of file?

Hello all How can I insert character to the end of text file without opening it in vi Just simple one liner, can it be done? Tnx (1 Reply)
Discussion started by: umen
1 Replies

10. UNIX for Advanced & Expert Users

Deleting end of line $ character in file

Hi, I've got a file where in the middle of the record is a $ end of line character, visible only when I open the file in vi and do :set list. How to I get rid of the character in the middle and keep it at the end. The middle $ character always appears after SW, so that can be used to tag it.... (3 Replies)
Discussion started by: bwrynz1
3 Replies
Login or Register to Ask a Question