1820878 Members
5620 Online
109628 Solutions
New Discussion юеВ

Remove columns in file

 
Paola Guizado
Occasional Contributor

Remove columns in file

Hi.
Please,
I have a file which has some rows and this has 22 columns, separated by tab, however I want to create a new file or in the same one that I take off the head and alone it places 20 columns, of equal it forms separated by tab

Their help with a script that allows me to make this.
Thank you

"Attachment deleted as it appeared to contain priveleged information"
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: Remove columns in file

Hi Paola:

# perl -nle 'next if $.==1;@a=split;print join "\t", @a[0..19] file

This will skip the first line of 'file' and output twenty (20) columns of data from each row read using a tab (\t) as a column field seperator. Perl counts zero-relative, hence the [0..19] slice.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Remove columns in file

You can use awk. The character after -F and OFS= is a tab:
awk -F" " -v OFS=" " '
BEGIN { getline } # skip first
{
print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20
} ' filename

Paola Guizado
Occasional Contributor

Re: Remove columns in file

Ok thanks.
This is:
awk -F"\t" -v OFS="\t" '
BEGIN { getline } # skip first
{
print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
$11, $12, $13, $14, $15, $16, $17, $18, $19, $20
} ' casita.txt
James R. Ferguson
Acclaimed Contributor

Re: Remove columns in file

Hi (again) Paola:

The Perl solution splits the fields of your input file on "whitespace" which means that either spaces OR tab characters (or both) can delimit your input fields. As you requested, the output field seperators are tab characters (\t).

Of course, the Perl solution is quite short :-))

Regards!

...JRF...
Paola Guizado
Occasional Contributor

Re: Remove columns in file

Hi.
That I modify in script if I want to maintain the head??
Thanks.
Sandman!
Honored Contributor

Re: Remove columns in file

Yet another wat of doing the same thing:

# awk -F'\t' 'NR>1 {for(i=1;i<=20;++i) printf(i<20?"%s\t":"%s\n",$i)}' ofile
James R. Ferguson
Acclaimed Contributor

Re: Remove columns in file

Hi (again);

> That I modify in script if I want to maintain the head??

This will retain the first line ($.==1) of your file in an unaltered state but produce output of twenty tab-seperated fields from the remaining lines:

# perl -nle 'print if $.==1;@a=split;print join "\t", @a[0..19] file

Now, if you want to update your 'file' "in-place" while creating a backup of the original file ('file.old'), do:

# perl -ni.old -le 'print if $.==1;@a=split;print join "\t", @a[0..19] file

Regards!

...JRF...



Dennis Handly
Acclaimed Contributor

Re: Remove columns in file

>That I modify in script if I want to maintain the head??

For awk, if you don't want to truncate the extra columns:
BEGIN { getline; print $0 } # print first
...
If every column title is tab delimited and you want to delete the name, just remove that BEGIN and process the headings as normal data.

>JRF: the Perl solution is quite short :-))
>Sandman: Yet another way of doing the same thing:

The reason I spelled everything out in awk is to allow future more complex changes and be more maintainable.

If our answers were helpful please read the following about assigning points:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33

Also if no further questions you should close the thread.
Nitin Kumar Gupta
Trusted Contributor

Re: Remove columns in file

head -1 casita.txt | awk -F"\t" -v OFS="\t" '
BEGIN { getline } # skip first
{
print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
$11, $12, $13, $14, $15, $16, $17, $18, $19, $20
} '

intLine=`cat casota.txt | wc -l`

tail -${intLine} | awk -F"\t" -v OFS="\t" '
BEGIN { getline } # skip first
{
print $1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
$11, $12, $13, $14, $15, $16, $17, $18, $19, $20
} '


I have given the shell script, you can use it. Here I assume your file is casota.txt.

Modify according to your requirement.

Rgds
-NKG-