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
replace UTF-8 characters with tr ripat Shell Programming and Scripting 2 06-26-2008 08:43 AM
Want to replace characters arndorff Shell Programming and Scripting 5 01-29-2008 04:05 PM
How to replace characters 7 through 14 of every line in a file jakSun8 Shell Programming and Scripting 9 12-13-2007 02:13 AM
Replace Characters... lgardner17325 UNIX for Dummies Questions & Answers 8 10-26-2006 06:04 PM
how to replace control characters using sed? hillxy Shell Programming and Scripting 5 10-16-2003 11:12 AM

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 09-16-2008
umathurumella umathurumella is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 6
Header Replace characters

Hi,

I have a flat file with header with tab delimiter.

nbr id name salesid detail num source num jun_2007 jul_2007 aug_2007 sep_2007 ....feb_2008

I need to modify the header for the columns
nbr to Id1
jun_2007 to Jun07
jul_2007 to Jul07
aug_2007 to Aug07
sep_2007 to Sep07
oct_2007 to Oct07
.
.
.
.
.
feb_2008 to Feb08

Can any one please give me the command to run a shell script passing this flat file as parameter.

I heard it wud be awk to read the first reocrd. File has 10K rows.

Thanks in advance

  #2 (permalink)  
Old 09-16-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Cool One approach

I show the input file, then the command to reformat the data. It is going to the screen, but you could redirect to a file.
The fix to the header breaks out the header line, then does all the changes via sed as you noted. (You can add more.) The command then continues with the ; (semi-colon) and issues another command -- the tail command to give me everything except for the header line.


Code:
> cat file1
nbr id name salesid detail num source num jun_2007 jul_2007 aug_2007 sep_2007 
line 1
line 2
line 3
line 4
line 5

> head -1 file1 | sed "s/nbr/Id1/" | sed "s/jun/Jun/" | sed "s/jul/Jul/" | sed "s/aug/Aug/" | sed "s/sep/Sep/" | sed "s/_2007/07/g" ; tail +2 file1
Id1 id name salesid detail num source num Jun07 Jul07 Aug07 Sep07 
line 1
line 2
line 3
line 4
line 5

  #3 (permalink)  
Old 09-16-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj - the student
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 611
Seems you have alonf list of replacement to be done.If that is the case, Prepare a file in the below format:

s/search_str1/replace_str1/
s/search_str2/replace_str2/
...

and use


Code:
sed -f pattern_file < input_file > output_file

  #4 (permalink)  
Old 09-16-2008
danmero danmero is online now Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,448
@joeyg one-liner simplified:

Code:
sed "1s/nbr/Id1/;s/jun/Jun/;s/jul/Jul/;s/aug/Aug/;s/sep/Sep/;s/_20//g" file

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 06:02 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