Header Replace characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Header Replace characters
# 1  
Old 09-16-2008
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  
Old 09-16-2008
Tools 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  
Old 09-16-2008
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  
Old 09-16-2008
@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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - How to update header of scripts in one pass - multiline search/replace

Hello. A find command return a list of file. For each fileReplace the content starting with the first "§" (of two) ending with last "ɸ" (of two), regardless of the content ( five lines ) by the following content (exactly) : §2019_08_23§ # # ... (8 Replies)
Discussion started by: jcdole
8 Replies

2. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

3. Shell Programming and Scripting

To replace header name

Hi, i am having below csv file with header names, i need to change the header with appending a string test.csv h1,h2,h3,h4 Ouptut hi_h1,hi_h2,hi_h3,hi_h4 I have tried below but this will take time if i have 100 headers. Is there a way to simplify this sed -e... (5 Replies)
Discussion started by: rohit_shinez
5 Replies

4. Shell Programming and Scripting

Replace a column with a value by ignoring the header lines

i have a file in the gz format , the content of the file is as follow. gzcat f1.gz # 1.name # 2.location # 3.age # 4.dob . . . . . . . . . # 43.hobbies < Aravind,33,chennai,09091980, , , , , , , surfing> (5 Replies)
Discussion started by: aravindj80
5 Replies

5. Shell Programming and Scripting

Remove last few characters in a file but keeping Header and trailer intact

Hi All, I am trying write a simple command using AWK and SED to this but without any success. Here is what I am using: head -1 test1.txt>test2.txt|sed '1d;$d' test1.txt|awk '{print substr($0,0,(length($0)-2))}' >>test2.txt|tail -1 test1.txt>>test2.txt Input: Header 1234567 abcdefgh... (2 Replies)
Discussion started by: nvuradi
2 Replies

6. Shell Programming and Scripting

Replace special characters with Escape characters?

i need to replace the any special characters with escape characters like below. test!=123-> test\!\=123 !@#$%^&*()-= to be replaced by \!\@\#\$\%\^\&\*\(\)\-\= (8 Replies)
Discussion started by: laknar
8 Replies

7. Shell Programming and Scripting

how to replace characters using tr

Hi, I have a file which includes some French Characters and I want to change them to other characters like À to &Agrave; Â to &Acirc; É to &Eacute; ..... ..... and so on. I am tyring to use tr command like tr ÀÂÉ &Agrave;&Acirc;&Eacute; < input file But it does not work. Only... (2 Replies)
Discussion started by: naveed
2 Replies

8. Shell Programming and Scripting

improve performance - replace $\| with $#@ and remove header and trailer records

Hi All, In my file i need to remove header and trailer records which comes in 1st line and last line respectively. After that i need to replace '$\|' with '$#@'. I am using sed command for this and its taking lot of time. Is there any other command which can be used to improve performance? ... (1 Reply)
Discussion started by: HemaV
1 Replies

9. Shell Programming and Scripting

How to replace characters with random characters

I've got a file (numbers.txt) filled with numbers and I want to replace each one of those numbers with a new random number between 0 and 9. This is my script so far: #!/bin/bash rand=$(($RANDOM % 9)) sed -i s//$rand/g numbers.txtThe problem that I have is that it replaces each number with just... (2 Replies)
Discussion started by: hellocatfood
2 Replies

10. Shell Programming and Scripting

Want to replace characters

Hi I have searched for a way to replace odd characters in a FOLDER NAME. All search-and-replace issues I have seen, only involves how to make search-and-replace on a FILE och with TEXT INSIDE a FILE. My problem is with the FOLDER NAME. My case is this: I have a couple of persons that every... (5 Replies)
Discussion started by: arndorff
5 Replies
Login or Register to Ask a Question