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
Concatenating the two lines in a file srivsn Shell Programming and Scripting 6 07-17-2008 09:03 PM
Converting Binary decimal coded values to Ascii Values gaur.deepti UNIX for Advanced & Expert Users 3 04-02-2008 12:33 PM
Extract values from log file wdympcf Shell Programming and Scripting 5 08-10-2007 03:52 AM
Change the Values in a file rudoraj Shell Programming and Scripting 6 09-18-2006 11:58 AM
concatenating static string to records in data file gillbates Shell Programming and Scripting 5 06-22-2006 06:22 PM

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 10-08-2007
amitkhiare amitkhiare is offline
Registered User
  
 

Join Date: Feb 2006
Location: Hong Kong
Posts: 24
Smile Concatenating values in a File

Hi All,

I have a ',' delimited file and i would like concatenate a new value at a specific column.

Example :-
xXXX,XYZ,20071005,ABC,DEF,123
xXXX,XYZ,20071005,ABC,DEF,123
xXXX,XYZ,20071005,ABC,DEF,123


The output that i want is
xXXX,XYZ,20071005001,ABC,DEF,123
xXXX,XYZ,20071005002,ABC,DEF,123
xXXX,XYZ,20071005003,ABC,DEF,123
........
xXXX,XYZ,20071005010,ABC,DEF,123
xXXX,XYZ,20071005011,ABC,DEF,123
and so .... on....

As you see the only difference is in the Column # 3 where i want to add 001, 002, 003 .... till the end of file [ in the above example there are 11 records so last updated value is 20071005011 ], however all other values should remain the same.

Please let me know if you have any doubts about the query.

and thanks for your time.
Amit
  #2 (permalink)  
Old 10-08-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
Code:
#assumption: only till 999
awk 'BEGIN{OFS=FS=",";c=1}
    { 
     n=sprintf("%03d",c++)
     $3=$3n     
    }
    1' "file"
  #3 (permalink)  
Old 10-08-2007
amitkhiare amitkhiare is offline
Registered User
  
 

Join Date: Feb 2006
Location: Hong Kong
Posts: 24
Quote:
Originally Posted by ghostdog74 View Post
Code:
#assumption: only till 999
awk 'BEGIN{OFS=FS=",";c=1}
    { 
     n=sprintf("%03d",c++)
     $3=$3n     
    }
    1' "file"
Hi, can you explain how it work i am not able to understand and also not able to us it... Please help
Thanks for your time.
Amit
  #4 (permalink)  
Old 10-08-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
Quote:
Originally Posted by amitkhiare View Post
Hi, can you explain how it work i am not able to understand and also not able to us it... Please help
Thanks for your time.
Amit
1) 'BEGIN{OFS=FS=",";c=1} #set output and input field separator to ",", initialize c variable to 1. this will be used for counting up
2) n=sprintf("%03d",c++) # string formatting. precede the number by 0's and fix it at length 3. Then assign the value to n eg 001, 002, till 999
3)$3=$3n #append 001 , 002 etc to field 3, which is the field with date, eg 20071005

which platform are you in? i am using GNU awk.
  #5 (permalink)  
Old 10-08-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,079
awk

Hi,
This should be also ok.

code:
Code:
awk 'BEGIN{FS=","}
{
print $1","$2","$3*1000+NR","$4","$5","$6
}' filename
  #6 (permalink)  
Old 10-09-2007
amitkhiare amitkhiare is offline
Registered User
  
 

Join Date: Feb 2006
Location: Hong Kong
Posts: 24
Quote:
Originally Posted by ghostdog74 View Post
1) 'BEGIN{OFS=FS=",";c=1} #set output and input field separator to ",", initialize c variable to 1. this will be used for counting up
2) n=sprintf("%03d",c++) # string formatting. precede the number by 0's and fix it at length 3. Then assign the value to n eg 001, 002, till 999
3)$3=$3n #append 001 , 002 etc to field 3, which is the field with date, eg 20071005

which platform are you in? i am using GNU awk.
Hi,

I am using this on Solaris Box and i tried your solution how ever the output is different and it is adding the 001 in the front side, not sure why. So the output is 00171005 for "20071005".

Will this be case of different platform...
  #7 (permalink)  
Old 10-09-2007
reborg's Avatar
reborg reborg is offline Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
try nawk on Solaris
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 12:33 AM.


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