Conditionally add character at end of line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditionally add character at end of line
# 1  
Old 10-30-2014
Conditionally add character at end of line

Hi,

I would like have a shell script to check every line in a file to see if it ends with ";". If this is NOT the last character ";" should be added.

MyFile.csv :
Code:
web9331801;01/01/2014 23:39:35;;"93962";353150256;
web9331802;01/01/2014 23:44:29;;"479288";353153538;
web9331803;01/01/2014 00:00:11;;"877219";352215572
web9331805;01/01/2014 00:00:11;;"877219";352215572
web9331807;01/01/2014 00:00:14;;"624588";352215598

Should be
Code:
web9331801;01/01/2014 23:39:35;;"93962";353150256;
web9331802;01/01/2014 23:44:29;;"479288";353153538;
web9331803;01/01/2014 00:00:11;;"877219";352215572;
web9331805;01/01/2014 00:00:11;;"877219";352215572;
web9331807;01/01/2014 00:00:14;;"624588";352215598;

Any help ? Smilie

Thanks !

Last edited by Franklin52; 10-30-2014 at 01:46 PM.. Reason: Please use code tags
# 2  
Old 10-30-2014
Any attempt from your side?
# 3  
Old 10-30-2014
I would use
Code:
sed 's/$/;/' MyFile.csv

to add ; but how do I check this last char for every line ?

Last edited by Franklin52; 10-30-2014 at 01:47 PM.. Reason: Please use code tags
# 4  
Old 10-30-2014
Please use code tags as required by forum rules!

Adapting your attempt, try
Code:
sed -E 's/(;|.)$/;/' MyFile.csv

EDIT: Don't use - wrong solution!

Last edited by RudiC; 10-31-2014 at 05:39 AM.. Reason: added error info
# 5  
Old 10-30-2014
Thanks !

But I'm getting an invalid option -E ?
# 6  
Old 10-30-2014
Hello vg77,

Welcome to forums, please use code tags for using commands and codes in post as per forum rules. Following may help you in same too.

Code:
awk '{$NF=$NF !~ /;$/?$NF=$NF";":$NF} 1' Input_file

Output is as follows.

Code:
web9331801;01/01/2014 23:39:35;;"93962";353150256;
web9331802;01/01/2014 23:44:29;;"479288";353153538;
web9331803;01/01/2014 00:00:11;;"877219";352215572;
web9331805;01/01/2014 00:00:11;;"877219";352215572;
web9331807;01/01/2014 00:00:14;;"624588";352215598;

Thanks,
R. Singh
# 7  
Old 10-30-2014
I tried Rudis solution without -E and it worked for me (GNU-sed).
Another possible sed-solution:
Code:
sed 's/[^;]$/;/' MyFile.csv

EDIT: wrong solution! - do not use!

Last edited by cero; 10-30-2014 at 10:40 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add a character at end of line?

Hai, I have got a small requirement in my script. and i am using bash shell. I need to add a dot (.) for some particular line in a file. Say for example, $Cat rmfile 1 This is line1 2 This is line2 3 This is line3 O/p should be : $Cat rmfile 1 This is line1 2 This is line2. #... (2 Replies)
Discussion started by: Sivajee
2 Replies

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

3. Shell Programming and Scripting

Add text at the end of line conditionally

Hi All, I have a file as below: cat myfile abcdef NA rwer tyujkl na I wish to add the text ".txt" at the end of all lines except the lines starting with NA or na. I know i can add text at the end of line using following command but I am not sure how to valiate the condition. (14 Replies)
Discussion started by: angshuman
14 Replies

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

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

6. Shell Programming and Scripting

Adding a special character at the end of the line

I used following to add * at the end of the line in file1. It adds * at the end but has a space before it for some lines but some other lines it adds exactly after the last character. How do I take out the space ? sed 's/$/*/' file1 > file2 example: contents of file1 : ... (2 Replies)
Discussion started by: pitagi
2 Replies

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

8. UNIX for Dummies Questions & Answers

stripping last character from end of each line

Hi, sorry for being dumb but I have a file with a variable amount of records (day to day it differs) but is fixed at 80 characters wide. Unfortunately the 80th and last charachter in each line is a "^M" carriage return character which i want to get rid of. Is there a really easy command that i can... (6 Replies)
Discussion started by: hcclnoodles
6 Replies

9. Shell Programming and Scripting

end of the line character

How do i determine what the end of the line character is in a text file. for eg. is it \n or \f or \t etc.. Is there a unix command for this? (5 Replies)
Discussion started by: zomboo
5 Replies

10. UNIX for Dummies Questions & Answers

End of line character in BAsh

I'm trying to match the end of line character in a shell script under bash. What I have done it got is a loop that reads each line of a file into a temp variable and then I get each letter of that variable and do some work on it. One think I want to do it check if the character I checking if the... (1 Reply)
Discussion started by: Rukshan
1 Replies
Login or Register to Ask a Question