awk - If then else rule like Excel cell


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users awk - If then else rule like Excel cell
# 1  
Old 06-12-2014
Linux awk - If then else rule like Excel cell

Hi,
I need extract / transpose where "Y" in the input file

My input file is
Code:
Item,EB,SB,SD,TP,GR
LP,Y,N,N,N,Y
GC,Y,N,N,N,N
CO,N,Y,Y,Y,Y
PS,Y,N,N,Y,Y

Expecting output is
Code:
EB-/'LP/',/'GC/',/'PS/'
SB-/'CO/'
SD-/'CO/'
TP-/'CO/',/'PS/'
GR-LP,CO,PS

I appreciate for your help

Regards
Balaji

Last edited by Scrutinizer; 06-12-2014 at 12:43 AM.. Reason: code tags
# 2  
Old 06-12-2014
I don't understand.

Why do the first four lines of your expected output contain slashes and single quotes in a strange pattern while the last line of your expected output seems much more "normal"?
# 3  
Old 06-12-2014
I too have same question like Don why 5th line of o/p does not contain single quotes and slashes ? is it typo ?
Code:
$ cat file
Item,EB,SB,SD,TP,GR
LP,Y,N,N,N,Y
GC,Y,N,N,N,N
CO,N,Y,Y,Y,Y
PS,Y,N,N,Y,Y

Code:
awk -F, '      {
 		for(i=2;i<=NF;i++)  
		{ 
		if(FNR==1){
				H[i]=$i
			  }
		else
			  {
				if(tolower($i)=="y")
					D[i]=(i in D)?D[i]","q"/"$1"/"q:q"/"$1"/"q
			  }
		}
	       }
	    END{
		for(i=2;i<=NF;i++)
		{
                        # I assume you want to remove single quotes and slashes for last line 
			if(i==NF){
				   gsub("\x27|/",x,D[i])
				 }
			print H[i]"-"D[i]
		}
	       }
          ' q="'" file

Resulting
Code:
EB-'/LP/','/GC/','/PS/'
SB-'/CO/'
SD-'/CO/'
TP-'/CO/','/PS/'
GR-LP,CO,PS


Last edited by Akshay Hegde; 06-12-2014 at 01:38 AM..
# 4  
Old 06-12-2014
I have ignored the last line formatting
Code:
awk -vq="'" 'BEGIN {FS = OFS = ","}
  NR == 1 {
    for(i = 2; i <= NF; i++)
      {N[i] = $i};
    n = NF;
    next}
  {for(i = 2; i <= NF; i++)
    {if($i == "Y")
      {C[i] = (C[i] == "") ? ("/" q $1 "/" q) : ( C[i] OFS "/" q $1 "/" q)}}}
  END {for(i = 2; i <= n; i++)
    {print N[i] "-" C[i]}}' file

# 5  
Old 06-12-2014
Yes. The last line in the output is typo error. I got the output. thank you for support

Last edited by myrole; 06-12-2014 at 04:19 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parse excel file with html on each cell

<DIV><P>Pré-condição aceder ao ecrã Home do MRS.</P></DIV><DIV><P>OK.</P></DIV><DIV><P>Seleccionar Pesquisa de Recepção Directa.</P></DIV><DIV><P>Confirmar que abriu ecrã de Recepção Directa.</P></DIV><DIV> (6 Replies)
Discussion started by: oliveiraum
6 Replies

2. Shell Programming and Scripting

awk equivilent of Excel WEEKNUM()

I am trying to calculate the week (Sun-Sat) number for a given date with week 01 defined as the week that ends in the new year (the week with the first Saturday of the new year). Excel WEEKNUM() calls the week containing Jan 1 the first week. Using this, I can get what I want above by... (13 Replies)
Discussion started by: Michael Stora
13 Replies

3. Shell Programming and Scripting

Perl - Append data to existing excel cell

Hello All, I have the following code in PERL to write data to excel sheet. Can someone please help me about how to append data to an exisitng cell? For ex in the below given case,Cell 1,1 has Active State PERL Now I want to add a new line like "prorgamming" without overwritting the... (3 Replies)
Discussion started by: prasperl
3 Replies

4. UNIX for Dummies Questions & Answers

AWK to excel

I've looked at a few different posts and none have entirely answered my question. Wondering how i'd go about using AWK to export to an excel file, which i gather would be a .csv file from what I've read. Atm my AWK command will read 2 files and export 2 files. Each of those files have 2... (3 Replies)
Discussion started by: Aussiemick
3 Replies

5. Shell Programming and Scripting

Comparison of Cells in EXCEL using awk

Hi I have 2 csv files which looks like the following and i have to compare the 2 CSVs and create a third file such that if the value of the 1st cell in A.CSV and the value of the first cell in the B.CSV are same, it should print "SAME" in the third file or else print NOT SAME. Likewise i need... (19 Replies)
Discussion started by: meva
19 Replies

6. Shell Programming and Scripting

Export into a single cell of excel sheet...

The awk is giving below output: SELECT divrel.child_org_idn org_main_idn NULL ( VARCHAR(200)) div_nam_2 NULL ( VARCHAR(200)) div_nam_3 NULL ( VARCHAR(200)) div_nam_4 NULL ( VARCHAR(200)) div_nam_5 NULL ( VARCHAR(200)) div_nam_6 NULL ( VARCHAR(200)) div_nam_7 ... (2 Replies)
Discussion started by: goutam_igate
2 Replies

7. Shell Programming and Scripting

printing two lines in awk as two columns in excel

hi guys, i would like to print two lines from a file as two adjacent columns using excel using awk.. i have this so far: awk '{for(i=1; i<=NF; i++) {printf("%s\n",$i)}}' "$count".ttt > "$count".csv #this to print the first line from the .ttt file as rows of the first column in the .csv... (9 Replies)
Discussion started by: npatwardhan
9 Replies

8. Shell Programming and Scripting

PERL: Modify Excel cell formatting

Hi, I would like to modify an existing excel cell format to a custom format. Is there a way to do it with out writing into a new excel file. In-place editing? Thanks (0 Replies)
Discussion started by: sandeep78
0 Replies

9. Shell Programming and Scripting

How to change cell background in excel sheet using perl

I need to change a cell in an excel sheet to different background color using perl.I tried SaveParse, but was unable to get it Please help regarding this. Thanks and Regards, Neelam G https://www.unix.com/images/misc/progress.gif (0 Replies)
Discussion started by: gujrathinr
0 Replies

10. UNIX for Dummies Questions & Answers

Change the cell background in excel sheet using Perl

I need to change a cell in an excel sheet to different background color using perl. Please help regarding this. Thanks and Regards, Neelam G (1 Reply)
Discussion started by: gujrathinr
1 Replies
Login or Register to Ask a Question