Visit The New, Modern Unix Linux Community


Match on columns and replace other columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match on columns and replace other columns
# 1  
Match on columns and replace other columns

Hi Friends,

I have the following input file

Code:
cat input
chr1 100 200 0.1 0.2 na 1 na nd
chr1 105 200 0.1 0.2 1 1 na 98
chr1 110 290 nf 1 na nd na 1
chr2 130 150 12 3 na 1 na 1
chr3 450 600 nf nf na 10 na nd
chr4 300 330 1 1 10 11 23 34


My requirement is
1. If $6 is na make $7 nd and $4 nf and $5 nf.
2. If $8 is na make $9 nd and $4 nf and $5 nf.

This is my output file

Code:
cat output
chr1 100 200 nf nf na nd na nd
chr1 105 200 nf nf na nd na nd
chr1 110 290 nf nf na nd na nd
chr2 130 150 nf nf na nd na nd
chr3 450 600 nf nf na nd na nd
chr4 300 330 1 1 10 11 23 34

Thanks in advance
# 2  
Quote:
Originally Posted by jacobs.smith
My requirement is
1. If $6 is na make $7 nd and $4 nf and $5 nf.
2. If $8 is na make $9 nd and $4 nf and $5 nf.
Code:
awk ' {
 if($6=="na") {
  $7="nd";
  $4="nf";
  $5="nf";
 }
 if($8=="na") {
  $9="nd";
  $4="nf";
  $5="nf";
 }
}1' input

This User Gave Thanks to Yoda For This Post:
# 3  
Quote:
Originally Posted by bipinajith
Code:
awk ' {
 if($6=="na") {
  $7="nd";
  $4="nf";
  $5="nf";
 }
 if($8=="na") {
  $9="nd";
  $4="nf";
  $5="nf";
 }
}1' input

Damn, I have to learn thinking simple. Thanks my friend.

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

Test Your Knowledge in Computers #54
Difficulty: Easy
COBOL stands for 'Common Business Oriented Language'.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data match 2 files based on first 2 columns matching only and join if match

Hi, i have 2 files , the data i need to match is in masterfile and i need to pull out column 3 from master if column 1 and 2 match and output entire row to new file I have tried with join and awk and i keep getting blank outputs or same file is there an easier way than what i am... (4 Replies)
Discussion started by: axis88
4 Replies

2. Shell Programming and Scripting

Match value in two files and replace values in selected columns

The purpose is to check if values for column 3 and 4 in file1 match with column 1 in file2. If any value match do: 1) Replace values in file2 for column 2 and 3 using the information of file1 columns 5 and 6 2) Replace string ($1,1,5) and string ($1,6,5) in file2 with values of columns 7... (8 Replies)
Discussion started by: jiam912
8 Replies

3. Shell Programming and Scripting

Match Columns in one file and extract columns from another file

Kindly help merging information from two files with the following data structure. I want to match for the CHR-SNP in Foo and get the columns that match from CHROM-rsID Fields 1 & 2 of foo may have duplicates, however, a joint key of Fields $1$2$3$4 is unique. Also would be helpful to clean up... (4 Replies)
Discussion started by: genehunter
4 Replies

4. Shell Programming and Scripting

Request: How to Parse dynamic SQL query to pad extra columns to match the fixed number of columns

Hello All, I have a requirement in which i will be given a sql query as input in a file with dynamic number of columns. For example some times i will get 5 columns, some times 8 columns etc up to 20 columns. So my requirement is to generate a output query which will have 20 columns all the... (7 Replies)
Discussion started by: vikas_trl
7 Replies

5. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

6. Shell Programming and Scripting

Match columns several files

Hey fellas! Here come my problem. I appreciate if you have a look at it. I have several files with following structure: file_1:1 21 4 45 file_2:2 31 4 153 6 341 and so on... and I have a 'reference' file look like this: File_ref:A 1 B 2 C 3 (5 Replies)
Discussion started by: @man
5 Replies

7. Shell Programming and Scripting

How to get lines that does not match between two columns

Hi, I wanted to get records which does not match bet two columns. Suppose i have a column A and column B, please see example below column A Column B 1 1 2 8 3 5 4 7 5 6 7 8 9 10 Please help (2 Replies)
Discussion started by: reignangel2003
2 Replies

8. Shell Programming and Scripting

Replace specific columns in one file with columns in another file

HELLO! This is my first post here! By the way, I think it is great that people do this. My question: I have two files, one is a .dilm and one is a .txt. It is my understanding that the .dilm file can be treated as a .txt file. I wrote another program where I was able to manipulate it as if it... (3 Replies)
Discussion started by: mehdib
3 Replies

9. UNIX for Dummies Questions & Answers

Replace columns from File1 with columns from File2

Hi all, I would like to replace some columns from file1 with columns from file2. Currently, I'm able to do it with the following command: awk 'NR==FNR{a=$1;b=$2;c=$3;next;} {$2=a;$4=b;$5=c;print}' file2 file1 > temp mv -f temp file1 First, i make the changes and save it as a temp... (1 Reply)
Discussion started by: seijihiko
1 Replies

10. Shell Programming and Scripting

match columns using awk

Hi All, I need some help in writing a small script using Awk. My input file has following deatils A,B,C,D 8239359,8239359,8388125,8388125 8239359,8239359,8388125,8388125 7165981,7165981,8363138,8363138 8283830,8283830,8382987,8382987 8209964,8209964,8367098,8367098 ... (8 Replies)
Discussion started by: pistachio
8 Replies

Featured Tech Videos