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.
# 8  
Old 07-10-2006
Quote:
Originally Posted by aigles
Try this :
Code:
awk '{line=line $0} END {sub(/,$/, "", line) ;print line}'


Great aigles !!

Thank you very much. This is what I was looking for.

But frankly I didn't understand the code. Could please explain why we are puting line=line $0. Are you concatinating the previous value of line with the present value?

Again one more problem I face in this code is that if the number of records in the source file becomes more than 5000, then I am getting the error
Segmentation fault(coredump)
in AIX.

Thanks,
Rony

Last edited by rony_daniel; 07-10-2006 at 12:22 PM..
# 9  
Old 07-10-2006
Try this

awk 'substr($0,length($0)) == "," {print substr($0,1,length($0)-1) substr($0,length($0)) != "," {print $0}' <filename>
# 10  
Old 07-11-2006
Quote:
Originally Posted by rajeeb_d
awk 'substr($0,length($0)) == "," {print substr($0,1,length($0)-1) substr($0,length($0)) != "," {print $0}' <filename>
This code have a missing "}". I guess it should be like this
awk 'substr($0,length($0)) == "," {print substr($0,1,length($0)-1)} substr($0,length($0)) != "," {print $0}' filename
Correct me if I am wrong.

The ouput i get here is like this
1
2
3
4
5
But this is not what I want. I have to the get the output as
1,2,3,4,5
# 11  
Old 07-11-2006
Another solution without concatenation :

Code:
awk '
NR==1 {         # Memorize first record whitout printing it
   line = $0;
   next;
}
{                  
   printf("%s",line); # Print previous record without ending Line Feed
   line=$0              # Memorize current record
} 
END {
   sub(/,$/,"",line);  # Remove trailing "," on last record
   print line            # Print last record
}
' input_file > output file

Another solution wiyhout awk :

Code:
sed '$s/,$//' < input_file | tr -d '\n'> output_file

In that case, the output file is not ending with a line feed.
You can add it :
Code:
echo >> output_file

# 12  
Old 07-11-2006
Try this

awk 'NR > 1 {print l} {l=$0} END {sub(/,$/,"",l);print l}' <filename>
# 13  
Old 07-12-2006
Great aigles !!
Both the code suggested by you is working fine and is excatly what I wanted. I tried this with a huge input file and still it's able to handle it without any errors.

Thank you very much aigles .

Thanks rajeeb_d for your help.
Your code is also giving the ouput for a large input file but the problem is that it is not putting the output in a single line. The output is coming as
1,
2,
3,
4,
5
Whereas my requirement is to get the output as
1,2,3,4,5

Once again thanks to all who have taken time and effort to help me out.

Regards,
Rony
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