awk command to replace columns in 2 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command to replace columns in 2 files
# 1  
Old 06-27-2013
awk command to replace columns in 2 files

Hi All,
I already have a code which replaces column 14 of NPBR.XTR.tmp with column 8 of NPBR3G.XTR.final

Code:
awk -F'\|' 'FNR==NR{a[$1]= $2"^"$8;next;}a[$2]{split(a[$2],b,"^");$8=b[1];$14=b[2];}1' OFS="|" ${SHTEMP}NPBR3G.XTR.final ${SHTEMP}NPBR.XTR.tmp > ${SHTEMP}NPBR.XTR.final

I also need to replace column 15 of NPBR.XTR.tmp with column 4 of NPBR3G.XTR.final

AND
Replace column 6 of NPBR.XTR.tmp with column 2 of NPBR3G.XTR.final
# 2  
Old 06-27-2013
Quote:
Originally Posted by nua7
I also need to replace column 15 of NPBR.XTR.tmp with column 4 of NPBR3G.XTR.final AND
Replace column 6 of NPBR.XTR.tmp with column 2 of NPBR3G.XTR.final
Code:
awk -F\| '
        FNR == NR {
                a[$1] = $2"^"$8"^"$4
                next
        }
        a[$2] {
                split(a[$2],b,"^")
                $8 = b[1]
                $14 = b[2]
                $15 = b[3]
                $6 = b[1]
        }
        1
' OFS="|" ${SHTEMP}NPBR3G.XTR.final ${SHTEMP}NPBR.XTR.tmp

This User Gave Thanks to Yoda For This Post:
# 3  
Old 06-27-2013
To append column names to a table using db2 and shell script
# Get all the columns seperated with comma.
Col_names=$(db2 -x "select colname||',' from syscat.columns where tabname = '$TABLENAME' order by colno")

#Put all the columns in a text file
echo $Col_names > columns.txt


#Export table data to csv which you want to append columns
db2 "EXPORT TO $HOME/$TABLENAME.csv OF DEL MESSAGES $HOME/$TABLENAME.txt select * from $SCHEMA.$TABLENAME"

#Append columns to Table
cat columns.txt $HOME/$TABLENAME.csv > $HOME/newfile.csv
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with awk or sed Command to Replace Text in Files

Hello Everyone, I have many files like so: file1.txt file2.txt file3.txt Within each file I have many lines of random text separated by commas like so: abcAAA,123,defAA,456777,ghiA,789 jklB,101,mnoBBB,11211,pqrB,13111 stuCC,415,vwxCCCC,161,yzaC,718 I am trying to use SED or AWK to... (4 Replies)
Discussion started by: D3U5X
4 Replies

2. UNIX for Beginners Questions & Answers

awk command to paste 1st columns of 2 files.

I have 2 files contains more than 5000 lines, I want to paste the 1st column of both file in an output file using awk. Please suggest to me I had tried paste command but it merges my both column. --- Post updated at 10:26 PM --- file1.txt 2020-01-07 235400 2020-01-07 235400 2020-01-07... (4 Replies)
Discussion started by: Sagar Singh
4 Replies

3. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

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

5. Shell Programming and Scripting

Compare and replace two columns from two files

Hello, I have two text tab delimited files File 1 has 30 columns. I am pasting only first 9 Chr Position Ref Alt Score Gene HGVS_C HGVS_P Coding_Consequence dbSNP 1 17312743 C T 1 Gene1 - ... (2 Replies)
Discussion started by: nans
2 Replies

6. Shell Programming and Scripting

Do replace operation and awk to sum multiple columns if another column has duplicate values

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (12 Replies)
Discussion started by: as7951
12 Replies

7. Shell Programming and Scripting

Awk script to replace null columns with blank

I have a file with contents "08011"||20080812 "23348"|20080827|20080924 "23387"|20080829|20080915 "23581"|20081003|20081028 "23748"|20081017|20090114 "24095"|20080919|20081013 "24105"|20070723|20070801 "24118"|20080806|20081013 "24165"|20080820|20080912 "24221"|20080908|20080929 i... (3 Replies)
Discussion started by: sonam273
3 Replies

8. Shell Programming and Scripting

awk command to print multiple columns

Hello Team, I have written following command which is giving output is as shown below. bash-3.00$ grep -i startup catalina.out | tail +2 | sed -n 1p | awk -F" " '{ for (x=1; x<=5; x++) { printf"%s\n", $x } }' Dec 19, 2010 3:28:39 PM bash-3.00$ I would like to modify above command to... (2 Replies)
Discussion started by: coolguyamy
2 Replies

9. Shell Programming and Scripting

awk command for simple join command but based on 2 columns

input1 a_a a/a 10 100 a1 a_a 20 200 b1 b_b 30 300 input2 a_a a/a xxx yyy a1 a1 lll ppp b1 b_b kkk ooo output a_a a/a 10 100 xxx yyy (2 Replies)
Discussion started by: ruby_sgp
2 Replies

10. UNIX for Dummies Questions & Answers

Using awk to get columns from different files

How can I use awk to create a new file that has 2 columns, each colums comes form a different file. example: I need column 3 from file1 and column 5 from file2 to make file3. (3 Replies)
Discussion started by: cosmologist
3 Replies
Login or Register to Ask a Question