LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-21-2012, 09:43 AM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,115

Rep: Reputation: 58
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
??
 
Old 11-21-2012, 09:49 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,923

Rep: Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305Reputation: 1305
this should do the job:
awk ' NR>=11 && NR <= 28 { print $0 "." } ' test_file

Last edited by pan64; 11-21-2012 at 09:51 AM.
 
Old 11-21-2012, 09:55 AM   #3
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,115

Original Poster
Rep: Reputation: 58
Simply awesome. Many thanks
 
Old 11-21-2012, 10:45 AM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,135

Rep: Reputation: 299Reputation: 299Reputation: 299
Code:
awk ' NR>=11 && NR <= 28 { print $0 "." } ' test_file
Nitpick #1) This solution doesn't deal with trailing white space.
Nitpick #2) This solution discards lines 01-10 and 29-up.

Daniel B. Martin
 
Old 11-21-2012, 12:04 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by danielbmartin View Post
Nitpick #1) This solution doesn't deal with trailing white space.
You sure? Until the record is not split into fields, the record should be printed as it is, with trailing blank spaces (if any). Example:
Code:
$ echo "    Donald Duck" | awk '{print $0 "."}'
    Donald Duck.
$ echo "  Donald Duck" | awk '$1 = $1{print $0 "."}'
Donald Duck.
In the second example, the first field is re-assigned to itself and the record is re-built to take in account the changes. In this case the trailing blank spaces are lost.
 
Old 11-21-2012, 12:21 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
sed '11,28s/$/\./' filename > newfilename
 
1 members found this post helpful.
Old 11-21-2012, 12:21 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,135

Rep: Reputation: 299Reputation: 299Reputation: 299
Quote:
Originally Posted by colucix View Post
You sure? Until the record is not split into fields, the record should be printed as it is, with trailing blank spaces (if any).
I learn by reading posts and follow up by testing proposed solutions. In this instance I created an input file using Copy-and-Paste. This resulted in lines which had trailing blanks. OP gave an example of the desired file, and it had the added periods appended to the right-most printed character.

Daniel B. Martin
 
Old 11-21-2012, 12:26 PM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,135

Rep: Reputation: 299Reputation: 299Reputation: 299
Quote:
Originally Posted by pixellany View Post
sed '11,28s/$/\./' filename > newfilename
Short and sweet! Love it!

Daniel B. Martin
 
Old 11-21-2012, 02:36 PM   #9
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by danielbmartin View Post
I learn by reading posts and follow up by testing proposed solutions. In this instance I created an input file using Copy-and-Paste. This resulted in lines which had trailing blanks. OP gave an example of the desired file, and it had the added periods appended to the right-most printed character.

Daniel B. Martin
Sorry Daniel, reading your post I realized I confused trailing blank spaces with leading spaces. You're absolutely right: the suggested solutions don't deal with trailing blank spaces. My mis-understanding due to the fact that English is not my mother language!
 
Old 11-21-2012, 02:41 PM   #10
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Here is an enhanced sed solution:
Code:
sed '11,$s/[[:blank:]]*$/./'
and a similar solution in awk:
Code:
awk 'NR >= 11 {gsub(/[[:blank:]]*$/,".")} 1' file
 
1 members found this post helpful.
Old 11-22-2012, 09:45 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,561

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Here's an awk with a twist:
Code:
awk '(NR >10 && NR < 29 && ORS=".\n") || ORS=RT' RS=' *\n' file
And if we are not to assume to much, ie maybe the user means at the end of text but to keep the rest of the line in tact:
Code:
awk '(NR >10 && NR < 29 && ORS="."RT) || ORS=RT' RS=' *\n' file
 
Old 11-26-2012, 02:05 PM   #12
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,115

Original Poster
Rep: Reputation: 58
Super!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] AWK: print to end staring from nth line after matching cristalp Programming 4 08-28-2012 04:51 AM
awk error awk: line 2: missing } near end of file boscop Linux - Networking 2 04-08-2012 10:49 AM
[SOLVED] Adding text to end of a particular line in bash dagummit Linux - General 7 11-30-2010 10:22 AM
Adding a Comma to the end of every nth line in Vi (or sed). Euler2 Linux - Newbie 6 10-12-2009 09:38 AM
adding new line at end of file airikah Programming 3 11-08-2003 04:27 AM


All times are GMT -5. The time now is 02:02 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration