LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-05-2014, 08:29 PM   #1
peris
LQ Newbie
 
Registered: May 2014
Posts: 1

Rep: Reputation: Disabled
printing specific fields


Hi

I have a file as shown bellow

Quote:
G1 A1 X 3 6 NA 7 NA NA NA 7 NA NA
G1 B1 X NA NA NA NA NA NA NA NA 7 6
G1 C1 X NA 1 3 4 NA NA NA NA NA NA
G2 D1 Y NA NA NA 1 NA NA NA NA NA NA
G2 E1 Y 2 NA NA NA NA NA NA NA NA NA
G2 F1 Y NA NA NA NA 0 NA NA NA NA NA
G1 G1 Y NA NA NA NA NA 8 7 NA NA NA
G2 H1 X NA NA NA NA NA NA NA NA NA NA
Now I want to print those fields which are not equivalent to NA. Again I dont want to change the 1st three column format but after that I want to print remaining fields as separated by comma. The output file I want as

Quote:
G1 A1 X 3, 6, 7, 7
G1 B1 X 7, 6
G1 C1 X 1, 3, 4
G2 D1 Y 1
G2 E1 Y 2
G2 F1 Y 0
G1 G1 Y 8, 7
G2 H1 X
Can someone help me with this.

Thanks
 
Old 05-05-2014, 09:04 PM   #2
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,404

Rep: Reputation: Disabled
You could just replace "NA" with nothing.

The following sed expression will replace "NA " at the beginning of the line or " NA" anywhere else with an empty string:
Code:
sed "s/\(^NA | NA\)//g"
 
Old 05-06-2014, 03:25 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Copying the data showed you have tabs and not spaces between each item, so the following seems to work:
Code:
sed -r 's/NA\t?//g;s/\t$//;s/\t/,/4g'
 
Old 05-06-2014, 08:40 AM   #4
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
Suppose your filename is NA.txt which is in directory /tmp
If delimiter (special character which separates two columns) is space
Run
Quote:
sed -e 's/ NA//g' -e 's/NA //g' -e 's/ /,/4g' /tmp/NA.txt
If delimiter (special character which separates two columns) is tab
Run
Quote:
sed -e 's/\tNA//g' -e 's/NA\t//g' -e 's/\t/,/4g' /tmp/NA.txt

Last edited by eklavya; 05-06-2014 at 08:46 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] AWK/PERL printing all fields tekvaio Programming 9 10-25-2012 01:19 PM
matching and printing fields of two files ksaad Programming 3 11-07-2011 03:15 PM
Removing specific field seperator to combine two fields Met_girl Linux - Newbie 3 12-02-2010 09:38 AM
Unique lines based on specific fields. carl.waldbieser Programming 6 08-21-2005 03:26 PM
search for specific text in fields using awk Helene Programming 2 04-23-2004 01:13 AM


All times are GMT -5. The time now is 07:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration