The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
String concatenation issue in ksh arvindcgi Shell Programming and Scripting 3 05-16-2008 08:57 AM
Help concatenation string and variable drain Shell Programming and Scripting 5 02-18-2008 06:21 AM
cannot get logic for concatenation awk user_prady Shell Programming and Scripting 7 12-09-2007 11:09 PM
Concatenation Asteroid Shell Programming and Scripting 11 04-04-2007 03:15 AM
Concatenation videsh77 Shell Programming and Scripting 2 12-14-2004 02:13 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-03-2006
Registered User
 

Join Date: Jan 2006
Posts: 23
string concatenation

my input file contains thousands of lines like below

234A dept of education
9788 dept of commerce
8677 dept of engineering

How do i add a delimeter ':' after FIRST 4 CHARACTERS in a line
234A:dept of education
9788:dept of commerce
8677:dept of engineering
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-03-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,322
Is the pattern of all the lines in the file the same as the three shown here? If yes, this is how you can do it:
Code:
# cat testfile
234A dept of education
9788 dept of commerce
8677 dept of engineering

# while read first rest; do
> echo $first:$rest
> done < testfile
234A:dept of education
9788:dept of commerce
8677:dept of engineering
Reply With Quote
  #3 (permalink)  
Old 04-03-2006
Registered User
 

Join Date: Mar 2006
Posts: 106
awk

you need awk for it.
Reply With Quote
  #4 (permalink)  
Old 04-03-2006
Registered User
 

Join Date: Jan 2006
Posts: 23
Can you show me how to do using awk or sed

Yes all the lines are of the same pattern
Reply With Quote
  #5 (permalink)  
Old 04-04-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,454
with sed,
Code:
sed 's/\(....\) \(.*\)/\1;\2/' filename
with awk,
Code:
awk '{ 
for(x=1; x<=NF; x++) 
{
if ( x==1 )
{
  printf $x";";
}
else
{
  printf $x" ";
}
}
printf "\n";
}' filename
Reply With Quote
  #6 (permalink)  
Old 04-04-2006
Registered User
 

Join Date: Dec 2005
Location: India
Posts: 218
Quote:
Originally Posted by systemsb
Can you show me how to do using awk or sed

Yes all the lines are of the same pattern

try the following

Quote:
awk '$1=$1":"{print}' filename
Gaurav

Last edited by gauravgoel; 04-04-2006 at 02:55 AM. Reason: typo
Reply With Quote
  #7 (permalink)  
Old 04-04-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,454
Quote:
Originally Posted by gauravgoel
try the following
awk '$1=$1":"{print}' filename
Gaurav
and for those working on solaris distribution
use nawk instead of awk
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:50 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0