How to selectively NOT output some fileds?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to selectively NOT output some fileds?
# 1  
Old 06-06-2013
How to selectively NOT output some fileds?

Dear All

I have a text file which has many columns (>10,000). I want to create a new text file which will NOT include following columns: 5,15,105,200. How can I do that in shell (or awk, perl)? Thanks.
# 2  
Old 06-06-2013
You could use cut:
Code:
cut -d, -f-4,6-14,16-104,106-199,201- file

Change the delimiter with -d
# 3  
Old 06-06-2013
Quote:
Originally Posted by littlewenwen
Dear All

I have a text file which has many columns (>10,000). I want to create a new text file which will NOT include following columns: 5,15,105,200. How can I do that in shell (or awk, perl)? Thanks.
with awk

Code:
awk '{$5=$15=$105=$200=""}1' file

This User Gave Thanks to pamu For This Post:
# 4  
Old 06-06-2013
Quote:
Originally Posted by pamu
with awk

Code:
awk '{$5=$15=$105=$200=""}1' file

That will only make the fields empty, not remove them.
# 5  
Old 06-06-2013
Quote:
Originally Posted by Subbeh
That will only make the fields empty, not remove them.
Yes. That will make fields to null value.
But the ultimate aim of not including them into the output is resolved. Smilie
# 6  
Old 06-06-2013
Using Perl:
input file
Code:
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890

Perl program to exclude columns 3, 5 and 7
Code:
#!/usr/bin/perl -w
use strict;

my $cur_dir = $ENV{PWD};
my $filename = "$cur_dir/$ARGV[0]";

my $record;

open(FILEIN,"<$filename") or die"open: $!";
while( defined( $record = <FILEIN> ) ) {
  chomp $record;
  my @t = split(//,$record);
  foreach my $i (@t) {
    print $i unless $i =~ /3|5|7/;
  }
  print "\n";
}
close(FILEIN);

Output :
Code:
%./file020.pl file020
12468901246890124689012468901246890
12468901246890124689012468901246890
12468901246890124689012468901246890
12468901246890124689012468901246890
12468901246890124689012468901246890
12468901246890124689012468901246890
12468901246890124689012468901246890

# 7  
Old 06-06-2013
Wow, I got solutions in shell, awk and perl!
Thank you all 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

Replace the perticular fileds in passwd file

Hi I have 2 different password entries in 2 different files for the same user. file 1 - siva:correct:1000:23:siva:/home/siva:/bin/bash file 2 - siva:incorrect:1000:23:siva:/home/siva:/bin/bash file 1 is having correct passwd entry where as file 2 is wrong. Now, i want to compare... (3 Replies)
Discussion started by: kumar85shiv
3 Replies

2. Shell Programming and Scripting

awk to store in a list the values of certain fileds

Dear Group, I have following input: sa;sb;sc;sd;period;ma;mb;mc;md;me sa1;sb1;sc1;sd1;200001;ma1;mb1;mc1;md1;me1 sa2;sb2;sc2;sd2;200002;ma2;mb2;mc2;md2;me2 sa3;sb3;sc3;sd3;200003;ma3;mb3;mc3;md3;me3 first line contains the headers! I want to create with one pass the following output:... (8 Replies)
Discussion started by: suturjik
8 Replies

3. Shell Programming and Scripting

Add two more fileds to awk command

This is what I was trying but failed to do so need help. cat 1.sql | awk '{printf("%s",NR%4 ? $0",":$0"\n")}' Output :- I want to add 2 more values for each line (hostname,user name) so the output should have hostname & username (testsrv01,test1) Output should be like... (4 Replies)
Discussion started by: lazydev
4 Replies

4. Shell Programming and Scripting

Re: using AWK to compare fileds

I have following text: NAME=ora.LISTENER.lsnr TYPE=ora.listener.type TARGET=ONLINE , ONLINE , ONLINE , ONLINE STATE=ONLINE on host1, ONLINE on host2, ONLINE on host3, ONLINE on host4 NAME=ora.LISTENER_1525.lsnr TYPE=ora.listener.type TARGET=ONLINE ... (2 Replies)
Discussion started by: rcc50886
2 Replies

5. Shell Programming and Scripting

How to selectively suppress perl output?

The following perl statement in a bash script consists of two substatements. I intend the first perl substatement (the assignment with glob) to get input from the preceding bash pipe, and the second perl substatement (the foreach loop) to output back to bash. However, the first perl substatement... (7 Replies)
Discussion started by: LessNux
7 Replies

6. Shell Programming and Scripting

A script that read specific fileds from the 7th line in a file

Dear All, I need a unix script that will read the 7th line and especially these fileds from a file Mo speed 16, Mt speed 15 every 15 minutes starting from 00:00 to 23:45 on daily basis and put the result in a txt file and name it MT_MO_20090225.txt, please also note that the system date format... (2 Replies)
Discussion started by: samura
2 Replies

7. Shell Programming and Scripting

how to selectively mount FS?

Hi all, I have this doubt as to whether we can selective mount FS .by taking the input from the vfstab? ie, suppose i want to mount tmpfs(actually i want to do it :))but i dont want to follow the conventional way through the script that actually does!! However i want to just collect that... (0 Replies)
Discussion started by: wrapster
0 Replies

8. Shell Programming and Scripting

print the fileds of a file

Hi Friends, Please help me in finding the solution for this : I have a file as below : CuDv: name = "hdisk0" status = 1 chgstatus = 2 ddins = "scsidisk" location = "" parent = "vscsi0" connwhere = "830000000000" PdDvLn... (3 Replies)
Discussion started by: vijaya2006
3 Replies

9. UNIX for Advanced & Expert Users

Mandatory fileds in Bugzilla

Hi, We need to configure some of the fileds in bugzilla like Platform,OS, Version etc are mandatory. Is it possible to set, if yes then how to configure. Thanks & Regards, Bache (0 Replies)
Discussion started by: bache_gowda
0 Replies

10. Shell Programming and Scripting

Merging fileds from 2 files

I have 2 files - fileA and fileB I need to match the first field of the 2 files then output some of the fields from fileA on the same line as certain fields from fileB. There will be instances where fileB does not have a match for first field in fileA Thanks in advance (8 Replies)
Discussion started by: Mudshark
8 Replies
Login or Register to Ask a Question