By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,278 Members | 1,530 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,278 IT Pros & Developers. It's quick & easy.

Inserting a character to a line

P: 7
Hi,

How do I insert character (e.g. , or .) at certain positions in a line? For example I want to insert a period ( . ) after the first 4 characters of every each line in the file. I cannot do it in sed command. I'm playing with my script using perl command but to no avail. Please advice. Thanks in advance.

See below:

Input:
Expand|Select|Wrap|Line Numbers
  1. 12345
  2. 67890
  3. 23456
  4. 78901
  5. 34567
  6.  
Desired Output:
Expand|Select|Wrap|Line Numbers
  1. 1234.5
  2. 6789.0
  3. 2345.6
  4. 7890.1
  5. 3456.7
  6.  

Br,
Pete
Mar 22 '08 #1
Share this Question
Share on Google+
3 Replies


eWish
Expert 100+
P: 971
Here is one way you can do it.

Expand|Select|Wrap|Line Numbers
  1. my $string = 12345;
  2.    $string =~ s/^(\w{4})(.*)$/$1.$2/g;
  3.  
  4.    print $string;
Expand|Select|Wrap|Line Numbers
  1. #Output:
  2. 1234.5
--Kevin
Mar 22 '08 #2

P: 7
Hi,

I found my answer. This works on me:

Expand|Select|Wrap|Line Numbers
  1. $x =~ s/\A(....)/$1./;
  2.  
Tnx anyway. =)
Mar 22 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
Hi,

I found my answer. This works on me:

Expand|Select|Wrap|Line Numbers
  1. $x =~ s/\A(....)/$1./;
  2.  
Tnx anyway. =)

Where did you find it?
Mar 22 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.