awk print columns which are not null

I am working on a file with several columns as below

MO_NAME,FAULT_TYPE,CLASS,CODE1,CODE2,CODE3
RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2A,53,58
RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2B,24
RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2A,33	
RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2D,57	
RXOCF-102,OPERATOR CONDITION	FAULT CODES CLASS 2B

By using awk I'm printing the output like this

awk -F',' '{print $1,$2,substr($1,1,5)"_"substr($3,19,20)$4,substr($1,1,5)"_"substr($3,19,20)$5' OFS="," file.txt

The output comes like this

RXOCF-101,BTS INTERNAL,RXOCF_2A53,RXOCF_2A58
RXOCF-101,BTS INTERNAL,RXOCF_2B24,RXOCF_2B
RXOCF-101,BTS INTERNAL,RXOCF_2A33,RXOCF_2A	
RXOCF-101,BTS INTERNAL,RXOCF_2D57,RXOCF_2D	
RXOCF-102,OPERATOR CONDITION,RXOCF_2B

I want to skip the columns whihc have null code or nothing in the code column. Please help to get this desired output

Not sure I understand. What columns are missing? How does 2A53 get into output line 2? And 2A into line 5?

I am printing Col1,Col2,Partof(Col1)_Partof(Col2)Col4

if Col4 is null then the output is just like as above in my row 3,4 and 5 (last columns with no code)

2A53 comes from:

2A is part of Col3 and 53 is my Col4 (Concatenated both)

Not in line 2.

---------- Post updated at 23:20 ---------- Previous update was at 23:19 ----------

And - what about $6 ?

Don't have $6 in original file. Corrected it. Printing till $5 now

What about the output in line 2?

---------- Post updated at 23:31 ---------- Previous update was at 23:30 ----------

A bit more care when specifying the problem would help us help you.

In line 2, "2B" is the substring of column 3 and "24" comes from column 4.
It's the concatenation of substring of column 1, substring of column 3 and with code in column 4

Well, you have a 2A instead of the 2B ...

Howsoever, try

awk -F',' '
FNR>1   {TMP=substr($1,1,5) "_" substr($3,19,20)
         print $1,$2,TMP $4 ($5?OFS TMP $5:"")}
' OFS="," file
RXOCF-101,BTS INTERNAL,RXOCF_2A53,RXOCF_2A58
RXOCF-101,BTS INTERNAL,RXOCF_2B24
RXOCF-101,BTS INTERNAL,RXOCF_2A33
RXOCF-101,BTS INTERNAL,RXOCF_2D57
RXOCF-102,OPERATOR CONDITION,RXOCF_2B
1 Like

Sorry, corrected row 2 now.

---------- Post updated at 02:47 AM ---------- Previous update was at 02:35 AM ----------

cat file
RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2A,53,58
RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2B,24
RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2A,33  
RXOCF-101,BTS INTERNAL,FAULT CODES CLASS 2D,57  
RXOCF-102,OPERATOR CONDITION FAULT CODES CLASS 2B
awk -F',' 'FNR>1 {TMP=substr($1,1,5) "_" substr($3,19,20) print $1,$2,TMP $4 ($5?OFS TMP $5:"")}'
awk: syntax error near line 1
awk: illegal statement near line 1

It's giving me above error

What's your OS and awk version?

---------- Post updated at 23:50 ---------- Previous update was at 23:48 ----------

Independent of OS and awk , if you concatenate lines, don't forget a ; as the command separator. Why didn't you test the script as given?

I'm using Solaris 5.10.
Where to put ; ?
I don't see ; in your script

---------- Post updated at 03:05 AM ---------- Previous update was at 03:02 AM ----------

 awk -F',' '
> FNR>1   {TMP=substr($1,1,5) "_" substr($3,19,20)
>          print $1,$2,TMP $4 ($5?OFS TMP $5:"")}
> ' OFS="," file
awk: syntax error near line 3
awk: illegal statement near line 3

c.f. Don Cragun:

1 Like

Thankyou :slight_smile: nawk worked for me. Thanks for the help.