The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replace certain character with a new a new line. djsal Shell Programming and Scripting 8 01-11-2008 12:59 PM
to replace a new line character chan Shell Programming and Scripting 2 01-11-2008 12:56 PM
Replace a character in last line Mohammed Shell Programming and Scripting 6 08-01-2007 02:46 PM
How to replace a character in a file preethgideon Shell Programming and Scripting 6 08-30-2006 06:19 PM
How would you replace the n character in a file with some xyz? JosephGerard UNIX for Dummies Questions & Answers 2 07-21-2005 08:46 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-31-2004
LordJezo
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Question How would I replace the 9th character of every line in a file?

Is there a simple way to replace the 9th character of every line in a text file?

Right now it is a space and instead I want to stick in a 0, every line is the same.

Is there a quick way to do this in Unix? (Solaris)
  #2 (permalink)  
Old 08-31-2004
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Try using sed...

sed 's/\(^........\) /\10/' file1 > file2

Or awk...

awk '{print substr($0,1,8) "0" substr($0,10)}' file1 > file2
  #3 (permalink)  
Old 08-31-2004
LordJezo
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Worked perfect, thanks.

Can you explain how it works though?

awk '{print substr($0,1,8) "0" substr($0,10)}' file1 > file2

I understand the awk part, the "0" part, and the file1 > file2 part but the substr part.. I see that it works but not why it does.
  #4 (permalink)  
Old 08-31-2004
google's Avatar
google google is offline Forum Advisor  
Moderator
  
 

Join Date: Jul 2002
Location: Atlanta
Posts: 740
Code:
{print substr($0,1,8) "0" substr($0,10)}
Awk's concatenation is built into the language. Awk is concatenating the first part of the record (positions 1 through 8) then setting the 9th position to "0". Lastly, the substring command is used to concatenate the remaining portion of the record (from position 10 to the end of the file) onto the new record which is output in the print statement.
  #5 (permalink)  
Old 08-31-2004
LordJezo
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
Wow.

That's a pretty clever way of doing it. I doubt I would have thought about that angle, what I was thinking was a specific character replace.

Neat.

Thanks.
  #6 (permalink)  
Old 09-01-2004
zazzybob's Avatar
zazzybob zazzybob is offline Forum Advisor  
Registered Geek
  
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Or even simpler

sed 's/./0/9' file1 > file2

Which'll replace the 9th occurrence of "any character" with a zero.

Cheers
ZB
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:18 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0