add char o end of line if dosent exist


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers add char o end of line if dosent exist
# 1  
Old 12-17-2011
Power add char o end of line if dosent exist

hey ,
i want to check if the char "#" exist at the end of every line of txt file
and if it dosent then add it for example:

Code:
 
the cat jumped on my mom #
cars can run on water#
i cant get a date
blue
yellow#

will be:
Code:
 
the cat went back home#
cars can run on water#
i cant get a date#
blue#
yellow#

i found that :

Code:
 
sed "s/$/#/g" filename

will add to every line, i dont know how to check if its allready there

any help will be truly apprichiated
thank u.
# 2  
Old 12-17-2011
Test for it and if it's there branch round the replacement:

Code:
sed '/#$/b; s/$/#/' input-file

This User Gave Thanks to agama For This Post:
# 3  
Old 12-17-2011
Code:
sed '/#$/!s/$/#/' filename

This User Gave Thanks to binlib For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to add a numeric & special char to end of the first line

Need to add a numeric & special char to end of the first line Existing file: 12-11-16|11 2016 Jan 12:34:55|03:55| 13-10-16|10 2016 Jan 12:34:55|03:55|12-11-16|11 2016 Jan 12:34:55|03:55| 14-10-16|19 2016 Jan 12:34:55|03:55|13-11-16|11 2016 Jan 12:34:55|04:55| 15-10-16|18 2016 Jan... (11 Replies)
Discussion started by: Joselouis
11 Replies

2. Shell Programming and Scripting

Add line at the end

How to add a comma at the end of each line in this file?30 1412 30 3352 30 5254 30 5543 30 7478 3 28 3 30 3 39 3 54 3 108 3 152 3 178 3 182 3 214 3 271 3 286 3 300 3 348 3 349 3 371 (3 Replies)
Discussion started by: gunjan
3 Replies

3. Shell Programming and Scripting

Add new line after ' char

Hello experts, I need to convert one file into readable format. Input file is like following line. STG,12345,000,999,' PQR, 2345,000,999,' XYZ,7890,000,999, Output should be following (After ' char new line char should be added.) STG,12345,000,999,' PQR, 2345,000,999,' ... (16 Replies)
Discussion started by: meetmedude
16 Replies

4. Shell Programming and Scripting

Add char before certain line

I'm trying to add a '1' before a line that has the word "C_ID" s/.*C_ID.*/&\n1/ The above code did what I need, but the '1' is added after the C_ID word :( Help please. (5 Replies)
Discussion started by: newbeee
5 Replies

5. Shell Programming and Scripting

Get the 1st 99 characters and add new line feed at the end of the line

I have a file with varying record length in it. I need to reformat this file so that each line will have a length of 100 characters (99 characters + the line feed). AU * A01 EXPENSE 6990370000 CWF SUBC TRAVEL & MISC MY * A02 RESALE 6990788000 Y... (3 Replies)
Discussion started by: udelalv
3 Replies

6. UNIX for Advanced & Expert Users

Sed - add text to start of line if 1st char anything but space

Problem: I have a lot of files, the files first line should always have 4 spaces before any text. Occasionally some of the files will miss the leading spaces and it's a problem. This is only in the first line. So if there are 4 spaces then text, do nothing. If there are not 4 spaces, add 4... (2 Replies)
Discussion started by: Vryali
2 Replies

7. Shell Programming and Scripting

how to add ; at the end of last line

hi, i have file which is having large sql query eg : i am executing this sql file but now i want to add ; after query on same line i.e. i should look like any idea how to achieve it ? (6 Replies)
Discussion started by: crackthehit007
6 Replies

8. Shell Programming and Scripting

Add end of char \n on end of file

Hi, I want to add \n as a EOF at the end of file if it does't exist in a single command. How to do this? when I use command echo "1\n" > a.txt and od -c a.txt 0000000 1 \n \n 0000003 How does it differentiate \n and eof in this case? Regards, Venkat (1 Reply)
Discussion started by: svenkatareddy
1 Replies

9. UNIX for Advanced & Expert Users

Add line numbers to end of each line

Hi i would like to add line numbers to end of each line in a file. I am able to do it in the front of each line using sed, but not able to add at the end of the file. Can anyone suggest The following code adds line number to start of each line sed = filename | sed 'N;s/\n/\t/' how can i... (5 Replies)
Discussion started by: rudoraj
5 Replies

10. Shell Programming and Scripting

Add a new end of line

Hi, Does anyone know if its possible to add something like an end of line like c or java in unix? dirs=/home/nosnam var='' for dir in $dirs do listDirs=`ls -d1 $dir/*` for eachList in $listDirs do listRepos=`du -ks $eachList | awk '{ x+=$1 }; END { print x... (4 Replies)
Discussion started by: nosnam
4 Replies
Login or Register to Ask a Question