To replace header name

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To replace header name
# 1  
To replace header name


i am having below csv file with header names, i need to change the header with appending a string





I have tried below but this will take time if i have 100 headers. Is there a way to simplify this
sed -e 's#h1#'hi_h1'#g'  -e 's#h2#'hi_h2'#g' test.csv

# 2  
Without seeing the rest of the file I am guessing. You can try this or maybe change it to your needs:
$ echo "h1,h2,h3,h4" | sed 's/h[0-9]\+/hi_&/g'

# 3  
When you mention "headers", you talk of first lines of a text file? Try this small amendment of zaxxon's proposal:

sed '1s/h[0-9]\+/hi_&/g' test.csv

# 4  
More generic and untested...

awk  -v add="hi_"  -F, 'NR==1{for(i=1;i<=NF;i++) $i=add $i}1' OFS=","  file

# 5  
Hello rohit_shinez,

Could you please try following too and let me know if this helps.(Considering only headers you need to change this which will be off course first line of Input_file)
awk 'NR==1{gsub(/h[0-9]+/,"hi_&",$0);print;next} 1'    Input_file

R. Singh

Last edited by RavinderSingh13; 03-06-2017 at 06:20 PM..
# 6  
Independent of name of header:
sed '1s/^/hi_/; 1s/,/,hi_/g' test.csv

GNU sed (-r), BSD sed (-E):
sed -r '1s/(^|,)/&hi_/g' test.csv

This User Gave Thanks to Scrutinizer For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #549
Difficulty: Medium
a = 10; is an example of a unary operation.
True or False?

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. Shell Programming and Scripting

Find header in a text file and prepend it to all lines until another header is found

I've been struggling with this one for quite a while and cannot seem to find a solution for this find/replace scenario. Perhaps I'm getting rusty. I have a file that contains a number of metrics (exactly 3 fields per line) from a few appliances that are collected in parallel. To identify the... (3 Replies)
Discussion started by: verdepollo
3 Replies

3. 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

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 # # 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

Add column header and row header

Hi, I have an input like this 1 2 3 4 2 3 4 5 4 5 6 7 I would like to count the no. of columns and print a header with a prefix "Col". I would also like to count the no. of rows and print as first column with each line number with a prefix "Row" So, my output would be ... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

6. UNIX for Dummies Questions & Answers

Merge all csv files in one folder considering only 1 header row and ignoring header of all others

Friends, I need help with the following in UNIX. Merge all csv files in one folder considering only 1 header row and ignoring header of all other files. FYI - All files are in same format and contains same headers. Thank you (4 Replies)
Discussion started by: Shiny_Roy
4 Replies

7. 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

8. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: umathurumella
3 Replies

9. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies

10. Linux

Reading the header of a tar file(posix header)

say i have these many file in a directory named exam. 1)/exam/newfolder/link.txt. 2)/exam/newfolder1/ and i create a tar say exam.tar well the problem is, when i read the tar file i dont find any metadata about the directories,as you cannot create a tar containig empty directories. on the... (2 Replies)
Discussion started by: Tanvirk
2 Replies