awk Adding a Period at the end of a line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Adding a Period at the end of a line
# 1  
Old 11-21-2012
awk Adding a Period at the end of a line

I started venturing in learning the art of using AWK/GAWK and wanted to simply added a period from line #11 to line #28 or to the end of the file if there is data. So for example:

Code:
11 	Centos.NM                         
12	dojo1                 
13	redhat.5.5.32Bit       
14	redhat.6.2.64Bit       
15	mandriva.9.2.32Bit     
16	RELEASE_WIN2003       
17	SaintBox-Ubuntu-x64     
18	SAINTexploitVM-x86      
19	SAINTVM-x64             
20	Ubuntu-12.04.x64        
21	bigbadwolf.x64          
22	WIN2003PATCHED          
23	WIN2003UNPATCH          
24	WIN-IQF3U12CJA5         
25	XPSP3PATCHED            
26	XPPROUNPATCHED          
27	WIN2012                 
28	WIN8_141

to

Code:
11 	Centos.NM.                         
12	dojo1.                 
13	redhat.5.5.32Bit.       
14	redhat.6.2.64Bit.       
15	mandriva.9.2.32Bit.     
16	RELEASE_WIN2003.       
17	SaintBox-Ubuntu-x64.     
18	SAINTexploitVM-x86.      
19	SAINTVM-x64.             
20	Ubuntu-12.04.x64.        
21	bigbadwolf.x64.          
22	WIN2003PATCHED.          
23	WIN2003UNPATCH.          
24	WIN-IQF3U12CJA5.         
25	XPSP3PATCHED.            
26	XPPROUNPATCHED.          
27	WIN2012.                 
28	WIN8_141.

This is what I had so far:

Code:
awk '{ NR==11,NR==28 print $0 "." }' < test_file 
awk: { NR==11,NR==28 print $0 "." }
awk:         ^ syntax error
awk: { NR==11,NR==28 print $0 "." }
awk:                 ^ syntax error

??
# 2  
Old 11-21-2012
Code:
awk ' { if(NR>=11 && NR<=28) print $0"."; else print $0; } ' test_file

# 3  
Old 11-21-2012
I read the solution from another forum where I posted the question:

Code:
awk ' NR>=11 && NR <= 28 { print $0 "." } ' test_file

Smilie
# 4  
Old 11-21-2012
That will print only that range of lines, rather than simply modifying that range and leaving the others unchanged.
# 5  
Old 11-21-2012
Try:
Code:
awk 'NR>=11 && NR<=28 { sub(/[[:space:]]*$/,".&") }1' infile

# 6  
Old 11-21-2012
And another way:

Code:
awk '/11/,/28/{gsub(/\s*$/, "");print $0"."}' file

11      Centos.NM.
12      dojo1.
13      redhat.5.5.32Bit.
14      redhat.6.2.64Bit.
15      mandriva.9.2.32Bit.
16      RELEASE_WIN2003.
17      SaintBox-Ubuntu-x64.
18      SAINTexploitVM-x86.
19      SAINTVM-x64.
20      Ubuntu-12.04.x64.
21      bigbadwolf.x64.
22      WIN2003PATCHED.
23      WIN2003UNPATCH.
24      WIN-IQF3U12CJA5.
25      XPSP3PATCHED.
26      XPPROUNPATCHED.
27      WIN2012.
28      WIN8_141.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding comma to end of each line if more than 1 line

I have a file with dates as '2013-01-01' '2013-01-02' I want the output to be '2013-01-01','2013-01-02' if there is only 1 entry then there should not be any comma. (6 Replies)
Discussion started by: ATWC
6 Replies

2. Shell Programming and Scripting

Adding semicolon at the end of each line

Hi, I have a script which I need to change. I want to add a semicolon at the end of each line where the line starts with "grant" for e.g. create table(.... ); grant select on TABL1 to USER1 grant select on TABL1 to USER2should become create table(.... ); grant select on TABL1 to... (3 Replies)
Discussion started by: pparthiv
3 Replies

3. Shell Programming and Scripting

Adding tab/new line at the end of each line of a file

Hello Everyone, I need a help from experts of this community regarding one of the issue that I am facing with shell scripting. My requirement is to append char's at the end of each line of a file. The char that will be appended is variable and will be passed through command line. The... (20 Replies)
Discussion started by: Sourav Das
20 Replies

4. Shell Programming and Scripting

Script adding ^M to end of line

I trying to make a simple script to get info from remote servers my problem is the output of this line- SERVER_NAME=`ssh -t $USER@$REMOTESERVER 'hostname'`the output is linux1^M I would like to remove the ^M where is my error? Many Thanks -Steve (1 Reply)
Discussion started by: shoodlum
1 Replies

5. UNIX for Dummies Questions & Answers

Adding comma at the end of every line

Hi all, I have this sample file (actual file is larger) and i need to add comma at the end of every line. 1234 4335 232345 1212 3535 Output 1234, 4335, 232345, 1212, 3535, TIA - jak (2 Replies)
Discussion started by: jakSun8
2 Replies

6. Shell Programming and Scripting

adding characters end of line where line begins with..

Hi all, using VI, can anyone tell me how to add some characters onto the end of a line where the line begins with certain charactars eg a,b,c,......., r,s,t,........, a,b,c,......., all lines in the above example starting with a,b,c, I want to add an x at the end of the line so the... (6 Replies)
Discussion started by: satnamx
6 Replies

7. Shell Programming and Scripting

Adding lines at end of a line

This is what I want to do. I want to write a script that reads each line (of the highlighted file below) and add a specific number of blank lines (sometime 2, 3 or 5 lines) at the end of each line while copying that line. For example, here is the input. The sky is blue. I like to eat. I like... (19 Replies)
Discussion started by: Ernst
19 Replies

8. UNIX for Dummies Questions & Answers

insert period at the end of each line

file1 contains: this is a test this is a test and only a test this is another test this is another test and only another only i'd like my file to look like this: this is a test. this is a test and only a test. this is another test. this is another test and only another only. (6 Replies)
Discussion started by: tjmannonline
6 Replies

9. Shell Programming and Scripting

adding semicolumn at end of line

Hi , I need to add semicolumn at the end of each line in a file. can any one help me in this? Thanks in advance (2 Replies)
Discussion started by: scorpio
2 Replies

10. Shell Programming and Scripting

Adding new line at the end of file

Hi I have few files. For some files the cursor is at the end of last line. For other files, cursor is at the new line at the end. I want to bring the cursor down to next line for the files that are having cursor at the end of last line In otherwords, I want to introduce a blank line at the... (5 Replies)
Discussion started by: somesh_p
5 Replies
Login or Register to Ask a Question