sed to add field heards to specific fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to add field heards to specific fields
# 1  
Old 07-07-2016
sed to add field headers to specific fields

I have tab delimited input that prints out in the format below:

I am trying to add field headers to $5 and $6. Not sure if sed is the best tool but my attempt is below. Thank you Smilie.

Code:
$5 = REF
$6 = ALT

file
Code:
ID     CHR    START        STOP
123    1         100            200        A     C
456    1         201            300        T     G

desired output
Code:
ID     CHR    START           STOP      REF    ALT
123    1         100          200         A         C
456    1         201          300         T         G

Code:
  
sed -i '1iREF, ALT' file

Code:
awk 'NR==1{print $1" "$2" "$3" "$4 REF ALT ;next}{print}' inputfile

Might also work in this case, but my actual input has 53 fields and only $50-$53 need headers added to them.

Last edited by cmccabe; 07-07-2016 at 07:50 PM.. Reason: fixed format, added awk
# 2  
Old 07-07-2016
The sample input you provided is not tab delimited. If your real input is tab delimited and you just want to set the headers for certain fields, the easy way would be something like:
Code:
awk '
BEGIN {	FS = OFS = "\t"
}
NR == 1 {
	$5 = "REF"
	$6 = "ALT"
}
1' file

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 07-07-2016
So does the NR == 1 store the header that is already there? Thank you Smilie.
# 4  
Old 07-07-2016
Quote:
Originally Posted by cmccabe
So does the NR == 1 store the header that is already there? Thank you Smilie.
What happened when you tried it?

It doesn't store anything. The NR==1 section of the code replaces the header for the specified fields on the 1st input line it reads. Then, the condition 1 with the default action ({print}) prints the updated header line and all following unmodified lines to standard output.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 07-07-2016
Please, try:
Code:
perl -ple '$_.="\tREF\tALT" if $. == 1' inputfile

This User Gave Thanks to Aia For This Post:
# 6  
Old 07-08-2016
Code:
sed '1s/$/\tREF\tALT/' file
ID	CHR	START	STOP	REF	ALT
123	1	100	200	A	C
456	1	201	300	T	G

This User Gave Thanks to RudiC For This Post:
# 7  
Old 07-08-2016
Thank you all for your help and explanations Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine all matching fields in input but only print line with largest value in specific field

In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited, that are in $1 of gene which is just a single column of text. However only the line with the greatest $9 value in input needs to be printed. So in the example below all the MECP2 and LTBP1... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to add plus or minus to fields and split another field

In the tab-delimited input below I am trying to use awk to -10 from $2 and +10 to $3. Something like awk -F'\t' -v OFS='\t' -v s=10 '{split($4,a,":"); print $1,$2-s,$3+s,a,$5,$6} | awk {split(a,b,"-"); print $1,$2-s,$3+s,b-s,b+s,$5,$6}' input should do that. I also need to -10 from $4... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Replacing entire fields with specific text at end or beginning of field

Greetings. I've got a csv file with data along these lines: Spumoni's Pizza Place, Placemats n Things, Just Lamps Counterfeit Dollars by Vinnie, Just Shades, Dollar StoreI want to replace the entire comma-delimited field if it matches something ending in "Place" or beginning with "Dollar",... (2 Replies)
Discussion started by: palmfrond
2 Replies

4. Shell Programming and Scripting

Add value in specific field

Gents, I have many files to change increasing 40000 in specific field. Can you help me with a command with sed or awk to fix this problem. Where the the field has Marker FDU. I need to add 40000 plus example Marker FDU 27152.00 Marker FDU 67152.00 I should... (4 Replies)
Discussion started by: jiam912
4 Replies

5. Shell Programming and Scripting

Insert field between two fields using awk or sed

Hi All, I am trying to insert two columns in the following text. I tried awk but failed to achieve. Highly appreciate your help DATETIME="28-Sep-2013;20:09:08;" CONTROL="AB" echo "Myfile.txt;11671;7824.90;2822.48" The DATETIME will be inserted at the beginning and CONTROL will... (4 Replies)
Discussion started by: angshuman
4 Replies

6. UNIX for Dummies Questions & Answers

using sed delete a line from csv file based on specific data in two separate fields

Hello, :wall: I have a 12 column csv file. I wish to delete the entire line if column 7 = hello and column 12 = goodbye. I have tried everything that I can find in all of my ref books. I know this does not work /^*,*,*,*,*,*,"hello",*,*,*,*,"goodbye"/d Any ideas? Thanks Please... (2 Replies)
Discussion started by: Chris Eagleson
2 Replies

7. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

8. Shell Programming and Scripting

selecting specific fields in a file (maybe with sed?)

Hi, I have a file with following lines: chr1 10 AC=2;AF=1.00;AN=2;DP=2;Dels=0.00;HRun=0;HaplotypeScore=0.00;MQ=23.00;MQ0=0;QD=14.33;SB=-10.01 chrX 18 AB=0.52;AC=1;AF=0.50;AN=2;DP=203;DS;Dels=0.00;HRun=0;HaplotypeScore=20.01;MQ=15.63;MQ0=85;QD=12.80;SB=-1289.58 I need to extract 4... (2 Replies)
Discussion started by: menenuh
2 Replies

9. Shell Programming and Scripting

AWK : Add Fields of lines with matching field

Dear All, I would like to add values of a field, if the lines match in a certain field. Then I would like to divide the sum though the number of lines that have a matched field. This is the Input: Input: Test1 5 Test1 10 Test2 2 Test2 5 Test2 13 Test3 4 Output: Test1 7.5 Test1 7.5... (6 Replies)
Discussion started by: DerSeb
6 Replies

10. Shell Programming and Scripting

Get 4 character each from 2 different fields concatenate and add as a new field

Hi, I have a huge text file. It looks like abcde bangalo country 12345 lastfield i want to get first 3 characters from field1 and first 3 characters from field 2 and insert the result as a new field. example the result should be: abcde bangalo abcban country 12345 lastfield Please... (4 Replies)
Discussion started by: ajithshankar@ho
4 Replies
Login or Register to Ask a Question