print non empty column/field value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print non empty column/field value
# 1  
Old 09-27-2012
print non empty column/field value

I have below file
Code:
		 	 	25-09-2012	24-09-2012	19-09-2012	31-07-2012	30-04-2012	30-03-2012
ASIAEXFVNV	N/A	CEU										4	
DMIRSOA		N/A	CAS		2		2		2			
DMIRSOA		N/A	MIDMT								2		
NFIAL22		N/A	HVNY		11		11		11			
NFIAL22		N/A	NYAL3		11		11		11			
NFIAL22		N/A	NYCN		11		11		11			
NFIAL22		N/A	VNYCNYA		11		11		11

I want to print o/p as below format

field1 + field3 + non-empty fields corresponding date stamp

as in
Code:
ASIAEXFVNV	CEU	30-4-2012
DMIRSOA		CAS	25-09-2012	24-09-2012	19-09-2012
DMIRSOA		MIDMT	31-07-2012
NFIAL22		HVNY	25-09-2012	24-09-2012	19-09-2012
etc....

could anyone assist on this ?
# 2  
Old 09-27-2012
there is problem with FS.

Just giving you example. you can find workaround from this..

try..
Code:
awk -F '\t' '{if(NR!=1){for (i=4;i<=NF;i++){if($i != ""){if(s){s=s" "a[i+1]}else{s=a[i+1]}}} print $1,$3,s;s=""}else{split($0,a," ")}}' file

# 3  
Old 09-27-2012
hey Pamu,

thanks for giving me heads up, it's kind of not working
you are right, I supposed to mention FS but forgot due course time

evenif FS is tab, I am not getting desired result
instead it's giving
Code:
ASIAEXFVNV CEU
DMIRSOA N/A 30-04-2012
DMIRSOA N/A 30-04-2012
NFIAL22 N/A 30-04-2012
NFIAL22 N/A 30-04-2012
NFIAL22 N/A 30-04-2012
NFIAL22 N/A 30-04-2012


btw my actual file contains field separator as "," as below
above results has nothing to do with my below actual one instead it's output from my initial posted input file.

Code:
,, , 25-09-2012,24-09-2012,19-09-2012,31-07-2012,30-04-2012,30-03-2012
ASIAEXFVNV,N/A,CEU,,,,,4,
DMIRSOA,N/A,CAS,2,2,2,,,
DMIRSOA,N/A,MIDMT,,,,2,,
NFIAL22,N/A,HVNY,11,11,11,,,
NFIAL22,N/A,NYAL3,11,11,11,,,
NFIAL22,N/A,NYCN,11,11,11,,,
NFIAL22,N/A,VNYCNYA,11,11,11,,,

can you just have a look....
# 4  
Old 09-27-2012
try this..

Code:
awk -F, '{if(NR!=1){for (i=4;i<=NF;i++){if($i != ""){if(s){s=s" "a[i]}else{s=a[i]}}} print $1,$3,s;s=""}else{split($0,a,",")}}' file

This User Gave Thanks to pamu For This Post:
# 5  
Old 09-27-2012
It's much easier with those commas in lieu of deliberately dispersed <TAB> chars! This should work:
Code:
awk -F","   
        'NR==1 {split($0,df)} 
         NR>1 {printf "%s,%s", $1, $3; for (i=4;i<=9;i++) {if (x$i) printf ",%s",df[i]}; print ""}
        ' file

If you don't want commas in the output, replace by whatever you want in the format strings.

Last edited by RudiC; 09-27-2012 at 10:45 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

2. Shell Programming and Scripting

How to remove empty field in a text file?

Hi all, I want to remove empty field in a text file. I tried to used sed. But it failed. Input: LG10_PM_map_19_LEnd 1000560 G AG AG LG10_PM_map_19_LEnd 1005621 G AG LG10_PM_map_19_LEnd 1011214 A AG AG LG10_PM_map_19_LEnd 1011673 T CT CT ... (3 Replies)
Discussion started by: huiyee1
3 Replies

3. Shell Programming and Scripting

Awk: print lines with one of multiple pattern in the same field (column)

Hi all, I am new to using awk and am quickly discovering what a powerful pattern-recognition tool it is. However, I have what seems like a fairly basic task that I just can't figure out how to perform in one line. I want awk to find and print all the lines in which one of multiple patterns (e.g.... (8 Replies)
Discussion started by: elgo4
8 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

How to detect empty field in awk ?

Hi ! programmers I have a need of detecting empty field in file my file looks like this 40.900|-71.600|1.6|20|1|1961|21.00|3.700||1|US|28035|10029370|31 40.900|-71.600|5.7|20|1|1961|21.00|3.700||1|US|28035|10029370|31 40.900|-71.600|7.8|20|1|1961|21.00|3.700||1|US|28035|10029370|31... (7 Replies)
Discussion started by: Dona Clara
7 Replies

6. UNIX for Dummies Questions & Answers

remove empty field

Hi all ! I'm sure it is a basic question but I didn't find any threads that fit my need. How to remove empty fields with awk? Or in other words, how to shift all the fields after an empty field on the left? input: 1|2||3|4|5||6 wanted: 1|2|3|4|5|6 I tried: awk '{for(i=1; i<=NF;... (7 Replies)
Discussion started by: lucasvs
7 Replies

7. Shell Programming and Scripting

Format the file by deleting empty field

I have the test data with 10 column separated by comma and each column has more than 1000000 rows. Can anyone help me to find empty field in all columns and delete that empty field alone and lift that column up by one row. Data with empty field: A74203XYZ,A21718XYZ,A72011XYZ,A41095XYZ,... (7 Replies)
Discussion started by: zooby
7 Replies

8. Shell Programming and Scripting

Using sed to remove lines where field is empty

I was just looking at this post: https://www.unix.com/shell-programming-scripting/22893-delete-multiple-empty-lines.html. and I am looking to achieve the same with sed. So the idea is to delete lines from a file where a certain field has no value. Inputfile: EMID MMDDYY HOURS JOB EMNAME 0241... (4 Replies)
Discussion started by: figaro
4 Replies

9. UNIX for Dummies Questions & Answers

Count of Field for Non-Empty

Hi Guys, I wanted to count the number of records for a particular field of a file. whose fields are separated by comma"," I fI use this command. cat "filename" cut -sd "," -f13 | wc -l This shows all the lines count including the blank values for the field number 13. I wanted to count... (2 Replies)
Discussion started by: Swapna173
2 Replies

10. Shell Programming and Scripting

How to print empty line when the a field is changed

Hi all, I have this input file .. BSS01 107 Swafieh 11/06/2008 12:06:57 BSS01 111 Ramada_Hotel 12/06/2008 11:37:20 BSS01 147 Kalha_Rep 11/06/2008 19:13:39 BSS01 147 Kalha_Rep ... (9 Replies)
Discussion started by: yahyaaa
9 Replies
Login or Register to Ask a Question