The UNIX and Linux Forums  


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
Converting Column to Rows in a Flat file srinikal Shell Programming and Scripting 5 10-10-2008 04:32 PM
Flat file manipulation, (this could be a tough one) mrbungle50 Shell Programming and Scripting 2 01-10-2008 04:46 AM
Look up column in a flat file jambesh Shell Programming and Scripting 5 09-18-2006 06:44 AM
Column names in flat files srivsn Shell Programming and Scripting 1 12-27-2005 06:47 AM
File Manipulation (Move Column Position) ultimate Shell Programming and Scripting 1 03-29-2005 11:22 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-03-2009
net net is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 11
Flat File column manipulation

Hi All,

I have a tab delimited input file with say 4 fields (columns) as below :

0000443 1AGPR061 2006 Daiml
0002198 1B3XG0K2 1989 Chdds
0002199 1Bd64J0L 1990 Ch34s
0002275 1B3s4J0K 1989 Chadys
0002276 1B465302 2002 Dageml
0002290 1B45430K 1989 Cays

I want the 2nd column in file to be replaced with only first 2 chars and 6th char of the 2nd column values, so output should look like :

0000443 1A0 2006 Daiml
0002198 1B0 1989 Chdds
0002199 1BJ 1990 Ch34s
0002275 1BJ 1989 Chadys
0002276 1B3 2002 Dageml
0002290 1B3 1989 Cays

Can anyone help me with this ?
  #2 (permalink)  
Old 03-03-2009
ShawnMilo ShawnMilo is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 252

Code:
cat temp.txt  | perl -T\t -ane '$F[1] =~ s/^(.{2}).{3}(.).*$/$1$2/; print join("\t", @F) . "\n"'

The -T flag tells Perl to split the string into an array. In this case, by tabs. Then you do a regex on the second element of the array, then re-join them with tabs.
  #3 (permalink)  
Old 03-03-2009
ripat ripat is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2006
Location: Belgium
Posts: 438

Code:
gawk -F'\t' '{print $1, substr($2, 1, 2) substr($2, 6, 1), $3, $4}' file

  #4 (permalink)  
Old 03-03-2009
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,915
With sed:


Code:
sed 's[     \(..\)...\(.\)..[       \1\2[' infile

  #5 (permalink)  
Old 03-03-2009
Goldorakk's Avatar
Goldorakk Goldorakk is offline
Registered User
  
 

Join Date: Feb 2009
Location: France
Posts: 43

Code:
awk '{printf "%s\t%s%s\t%s\t%s\n", $1, substr($2,1,2),substr($2,6,1), $3, $4}' file

  #6 (permalink)  
Old 03-05-2009
net net is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 11
Thanks everyone for good and quick solutions provided ...Its working
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:58 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