Awk Doubt _ Urgent

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk Doubt _ Urgent
# 1  
Old 02-06-2008
Awk Doubt _ Urgent


I have the file TEST.csv generated after a join of two different files with the same columns:


I have to generate a new file from this file using the first columns $1,$2,$3,$4 however where is null it will automatically consider the other compensative column like: ghi,$2=$6

I tried to use the awk:

awk 'BEGIN { FS="," ; OFS="," ; key="" } {
if( key==$1 || key=="" ) {
if ($1="") print $1=$5;
if ($2="") print $2=$6;
if ($3="") print $3=$7;
if ($4="") print $4=$8;
}' TEST.csv | sort -t "," -k1,1b > NEW_FILE.csv

However the result is null...

Can you tell me what is the problem with my code and do you know any other way to do it?

# 2  
Old 02-06-2008
i cant access unix so cant test this, but i think u can just write $1=$5 instead of
print $1=$5; and the print the first four after the inner block..

i dont quite understand the logic behind the line
if( key==$1 || key=="" ) {
but u wudve thot of smthing, anyway.. try it out

Or, and especially if this is a big file, import into a databse and use isnull
# 3  
Old 02-07-2008
Hello Sir

The key=$1; is a variable that I set to make the if enter in the code making the relation witht the columns.

Ive solved the problem witht the following code :

awk -F, 'BEGIN { FS="," ; OFS="," ; key="" } {
if( key==$1$8 || key=="" ) {
if ($22!="") $1=$22;
if ($23!="") $2=$23;
if ($24!="") $3=$24;
if ($25!="") $4=$25;
if ($26!="") $5=$26;
if ($27!="") $6=$27;
if ($28!="") $7=$28;
if ($29!="") $8=$29;
if ($41!="") $9=$41;
if ($30!="") $11=$30;
if ($31!="") $12=$31;
if ($32!="") $13=$32;
if ($34!="") $14=$34;
if ($38!="") $17=$38;
if ($39!="") $18=$39;
if ($40!="") $19=$40;
if ($43!="") $20=$43;
if ($42!="") $21=$42;
print $0}' key3.t | sort -t "," -k1,1b > key4.t

Thank you very much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Doubt on using AWK

DE_CODE|1{AXXANY}1APP_NAME|2{TELCO}2LOC|NY DE_CODE|1{AXXATX}1APP_NAME|2{TELCO}2LOC|TX DE_CODE|1{AXXABT}1APP_NAME|2{TELCO}2LOC|BT DE_CODE|1{AXXANJ}1APP_NAME|2{TELCO}2LOC|NJ i have out put file like below i have to convert it in the format as below. DE_CODE = AXXANY APP_NAME= TELCO LOC = NY... (4 Replies)
Discussion started by: mail2sant
4 Replies

2. Shell Programming and Scripting

Clarify doubt ... plz Urgent

Hi friends, I am new to UNIX. I going to transfer files using SFTP. I am writing a script and using mget . If i am using mget * means, if all the files and their sub directories are transferred or not? If suppose , the local system dose not have the sub directory then what will... (1 Reply)
Discussion started by: punitha
1 Replies

3. Shell Programming and Scripting

Awk doubt

I have a file sample.txt with the following contents: the following gives output as awk 'NF{s=$0; print s}' sample.txt but, awk 'NF{s=$0}{print s}' sample.txtgives output as why this difference, can someone explain me? (6 Replies)
Discussion started by: royalibrahim
6 Replies

4. UNIX for Dummies Questions & Answers

Doubt in Oracle connecting unix - Very urgent

I am using a oracle query from unix.. flag=`sqlplus -s <<EOF SELECT 'Y' FROM table_name WHERE cond1 = '${table_name}' AND DECODE('${var_a}','''NA''',own,'${var_b0}')= own exception when no_data_found then dbms_output.put_line(NVL(l_owner_flag,'X')); end; EOF` Its not validating the... (7 Replies)
Discussion started by: sivakumar.rj
7 Replies

5. Shell Programming and Scripting

doubt on awk

I have executed the below command: find . -name "Ks*" -type f -exec ls -ltr {} \; | awk '{printf("%ld %s %d %s \n",$5,$6,$7,$8,$9)}' and here is the output: 1282 Oct 7 2004 51590 Jul 10 2006 921 Oct 7 2004 1389 Jun 4 2003 1037 May 19 2004 334 Mar 24 2004 672 Jul 8 2003 977... (6 Replies)
Discussion started by: venkatesht
6 Replies

6. Shell Programming and Scripting

doubt in awk

Hi , I have a file in the below format: 1.txt awk 'BEGIN { printf ("%1s", "man" )} ' awk 'BEGIN { printf ("%9s", "women" )} ' awk 'BEGIN { printf ("%56s", "human")} ' ## ### ## echo "$!" ## awk 'BEGIN { printf ("%1s", "aaa" )} ' awk 'BEGIN { printf ("%19s", "bbb" )} ' ... (4 Replies)
Discussion started by: jisha
4 Replies

7. Shell Programming and Scripting

awk doubt..

Hello, Iam trying to get the IP of the hostname of the machine from /etc/hosts. I used the command as below: But the result shows the IP with the hostname.. like I need to get only Please help Thanks in advance esham (11 Replies)
Discussion started by: esham
11 Replies

8. Shell Programming and Scripting

doubt in AWK

Hi all, column1 -------- 33 44 55 66 please provide the script using awk command to dispaly output 55. Help apperciated.. thanks, Nirmal (4 Replies)
Discussion started by: abnirmal
4 Replies

9. Shell Programming and Scripting

doubt???? urgent

How to embed a shell script into a C++ kinbdly help me by giving an example.... Thanking you... (1 Reply)
Discussion started by: vas_dba
1 Replies

10. UNIX for Dummies Questions & Answers

awk doubt

I'm having a file with 5 fields. I want to sort that file according to one field no 3. How shall I do using awk programming. Any input appreciatable. regards, vadivel. (7 Replies)
Discussion started by: vadivel
7 Replies
Login or Register to Ask a Question