Check if last char in a line is '.' and if not, insert it


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Check if last char in a line is '.' and if not, insert it
# 1  
Old 07-26-2010
Check if last char in a line is '.' and if not, insert it

Hi all,

I've searched for this but couldn't seem to find the answer I'm looking for. I have a text file that looks like this:

sgea0447 Earnings followed the same path. earnings followed the same path
sgea0448 Economic growth slowed further. economic growth slowed further
sgea0449 Emergency vehicles flash red or red blue. emergency vehicles flash red or red blue.
sgea0450 Encourage someone who is in low spirits. encourage someone who is in low spirits,
sgea0451 Enter the corporate raider. enter the corporate raider,
sgea0452 Entertaining leaves you feeling relaxed. entertaining leaves you feeling relaxed
sgea0453 Especially by your wife. especially by your wife,

As you can see, the last character of each line either already has a period, or it has commas, or it just has no punctuation. What I want is for all the lines to have a period at the end, should look like this:

sgea0447 Earnings followed the same path. earnings followed the same path.
sgea0448 Economic growth slowed further. economic growth slowed further.
sgea0449 Emergency vehicles flash red or red blue. emergency vehicles flash red or red blue.
sgea0450 Encourage someone who is in low spirits. encourage someone who is in low spirits.
sgea0451 Enter the corporate raider. enter the corporate raider.
sgea0452 Entertaining leaves you feeling relaxed. entertaining leaves you feeling relaxed.
sgea0453 Especially by your wife. especially by your wife.

Can anyone help??
# 2  
Old 07-26-2010
Try this:
Code:
sed 's/[,.]*$/./' file > newfile

# 3  
Old 07-26-2010
Hi Franklin,
Thanks for your reply, but it didn't seem to check if the last char in the line is already a '.', but only added it. Here's what I got with your one-liner:

sgea0447 Earnings followed the same path. earnings followed the same path
.
sgea0448 Economic growth slowed further. economic growth slowed further
.
sgea0449 Emergency vehicles flash red or red blue. emergency vehicles flash red or red blue.
.
sgea0450 Encourage someone who is in low spirits. encourage someone who is in low spirits,
.
sgea0451 Enter the corporate raider. enter the corporate raider,
.
sgea0452 Entertaining leaves you feeling relaxed. entertaining leaves you feeling relaxed
.
sgea0453 Especially by your wife. especially by your wife,
.
# 4  
Old 07-26-2010
Try this:
Code:
awk '!/\.$/{sub(/$/, ".")};{print}' myfile.txt

EDIT:
Hmm, didnt see the comma, sorry.

EDIT:
This works..
Code:
awk 'sub(/,$/, "") || !/\.$/{sub(/$/, ".")};{print}' myfile.txt

This User Gave Thanks to Ikon For This Post:
# 5  
Old 07-26-2010
Hi Ikon,

I got the same exact output as earlier with Franklin's code.
# 6  
Old 07-26-2010
odd I get this:

Code:
$ cat testing
sgea0447 Earnings followed the same path. earnings followed the same path
sgea0448 Economic growth slowed further. economic growth slowed further
sgea0449 Emergency vehicles flash red or red blue. emergency vehicles flash red or red blue.
sgea0450 Encourage someone who is in low spirits. encourage someone who is in low spirits,
sgea0451 Enter the corporate raider. enter the corporate raider,
sgea0452 Entertaining leaves you feeling relaxed. entertaining leaves you feeling relaxed
sgea0453 Especially by your wife. especially by your wife,


[host ~]$ awk 'sub(/,$/, "") || !/\.$/{sub(/$/, ".")};{print}' testing
sgea0447 Earnings followed the same path. earnings followed the same path.
sgea0448 Economic growth slowed further. economic growth slowed further.
sgea0449 Emergency vehicles flash red or red blue. emergency vehicles flash red or red blue.
sgea0450 Encourage someone who is in low spirits. encourage someone who is in low spirits.
sgea0451 Enter the corporate raider. enter the corporate raider.
sgea0452 Entertaining leaves you feeling relaxed. entertaining leaves you feeling relaxed.
sgea0453 Especially by your wife. especially by your wife.

What OS?

I get same on Redhat and HP-UX.
# 7  
Old 07-26-2010
I'm using PuTTY. Is there a workaround to this solution for PuTTY users?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search a certain char and insert new text if a match found

Have a file which has the create statement like below create table emp ( empno integer, empname char(50)) primary index(empno); i need to find a string starting with create and ends with semi-colon ;. if so insert the below statement before create statement rename table emp to emp_rename;... (2 Replies)
Discussion started by: Mohan0509
2 Replies

2. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

3. Shell Programming and Scripting

How to separate one line to mutiple line based on one char?

Hi Gurus, I need separate one file which is one huge line to mutiple line. file like abcd # bcd # def # fge # ged I want to get abcd bcd def fge ged Thanks in advance (4 Replies)
Discussion started by: ken6503
4 Replies

4. Shell Programming and Scripting

Formatting File having big single line into 95 Char Per Line

Hi All, I have 4 big files which contains one big line containing formatted character records, I need to format each file in such way that each File will have 95 Characters per line. Last line of each file will have newline character at end. Before:- File Name:- File1.dat 102 121340560... (10 Replies)
Discussion started by: lancesunny
10 Replies

5. Shell Programming and Scripting

Insert carriage return on the 10th char position of each line

Hi experts, Need your help on how to insert carriage return after the 10th char position of each line in a file and then add two blank spaces after the carriage return. Example: >cat test.txt testingline dummystring samplesample teststringline Expected output should be.. ... (2 Replies)
Discussion started by: brichigo
2 Replies

6. UNIX for Advanced & Expert Users

Check EOF char in Unix. OR To check file has been received completely from a remote system

Advance Thanks. (1) I would like to know any unix/Linux command to check EOF char in a file. (2) Or Any way I can check a file has been reached completely at machine B from machine A. Note that machine A ftp/scp the file to machine B at unknown time. (5 Replies)
Discussion started by: alexalex1
5 Replies

7. Shell Programming and Scripting

how first char in odd line and second char in even line

Hi I m having ifconfig -a o/p like sbanlab1:ksh# ifconfig -a | egrep "flags|inet" | awk -F' ' '{print $1,$2}' lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> inet 127.0.0.1 lo0:1: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> inet 127.0.0.1 bge0:... (1 Reply)
Discussion started by: tarunn.dubeyy
1 Replies

8. Shell Programming and Scripting

How to insert a char in every two?

Hi, I've written a script to interrogate all fibre devices on our systems and output the WWNs to a file. I now want to insert a ":" every 2 characters into the WWN so I can write another script to create aliases within the SAN. So the current file has WWNs in the following format;... (2 Replies)
Discussion started by: h8mmer
2 Replies

9. UNIX for Dummies Questions & Answers

unable to insert '#' char for a file in vi mode

Hi all, I am very new to this forum and also new to UNIX. I am working on HP-UX machine. I noticed that for some of the users I am unable to insert the # character in a file in vi mode. May I know y am I having the problem and how can I resolve this Thanks, Maroli (1 Reply)
Discussion started by: maroli
1 Replies

10. Shell Programming and Scripting

How to check a column contain numeric or char data type ??

I have a file called clientname_filename.csv whose contents are like col1|col2|col3|col4| 510|abc|xxx|450| 510|abc11|yyy|350 510|pqr99|zzz| 670 512|222|439|110 Here i have check the contents of column for data type. i have a constraints that col1 always contain Numeric value column 2... (12 Replies)
Discussion started by: jambesh
12 Replies
Login or Register to Ask a Question