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 > 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 11:57 AM
Help concatenation string and variable drain Shell Programming and Scripting 5 02-18-2008 10:21 AM
cannot get logic for concatenation awk user_prady Shell Programming and Scripting 7 12-10-2007 03:09 AM
Concatenation Asteroid Shell Programming and Scripting 11 04-04-2007 06:15 AM
Concatenation videsh77 Shell Programming and Scripting 2 12-14-2004 06:13 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 04-03-2006
systemsb systemsb is offline
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
  #2 (permalink)  
Old 04-03-2006
blowtorch's Avatar
blowtorch blowtorch is offline Forum Advisor  
Supporter
  
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,350
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
  #3 (permalink)  
Old 04-03-2006
amro1 amro1 is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 105
awk

you need awk for it.
  #4 (permalink)  
Old 04-04-2006
systemsb systemsb is offline
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
  #5 (permalink)  
Old 04-04-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
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
  #6 (permalink)  
Old 04-04-2006
gauravgoel gauravgoel is offline
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 05:55 AM.. Reason: typo
  #7 (permalink)  
Old 04-04-2006
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
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
Sponsored Links
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 03:59 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