theillien
theillien

Reputation: 1389

Removing last column from rows that have three columns using bash

I have a file that contains several lines of data. Some lines contain three columns, but most contain only two. All lines are single-tab separated. For those that contain three columns, the third column is typically redundant and contains the same data as the second so I'd like to remove it.

I imagine awk or cut would be appropriate, but I'm drawing a blank on how to test the row for three columns so my script will only work on those rows. I know awk is a very powerful language with logic and whatnot built into it, I'm just not that strong with it.

I looked at a similar question, but I'm not sure what is going on with the awk answer. Should the -4 be -1 since I only want to remove one column? What about if the row has two columns; will it remove the second even though I don't want to do anything?

I modified it to what I think it would be:

awk -F"\t" -v OFS="\t" '{ for (i=1;i<=NF-4;i++){ print $i }}' 

But when I run it (with the file) nothing happens. If I change NF-1 or NF-2 I get some output, but it only a handful of lines and only the first column.

Can anyone clue me into what I should be doing?

Upvotes: 0

Views: 2806

Answers (4)

Vijay
Vijay

Reputation: 67319

awk 'NF==3{print $1"\t"$2}NF==2{print}' your_file

Testde below:

> cat temp
1       2
3       4       5
6       7
8       9       10
>
> awk 'NF==3{print $1"\t"$2}NF==2{print}' temp
1       2
3       4
6       7
8       9
>

or in a much more simplere way in awk:

awk 'NF==3{print $1"\t"$2}NF==2' your_file

Or you can also go with perl:

perl -lane 'print "$F[0]\t$F[1]"' your_file

Upvotes: 0

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72855

The awk variable NF gives you the number for fields. So an expression like this should work for you.

awk -F, 'NF == 3 {print $1 "," $2} NF != 3 {print $0}'

Running it on an input file like so

a,b,c
x,y
u,v,w
l,m

gives me

$ cat test | awk -F, 'NF == 3 {print $1 "," $2} NF != 3 {print $0}'

a,b
x,y
u,v
l,m

Upvotes: 1

potong
potong

Reputation: 58578

This might work for you (GNU sed):

sed 's/\t[^\t]*//2g' file

Restricts the file to two columns.

Upvotes: 0

konsolebox
konsolebox

Reputation: 75628

If you just want to remove the third column, you could just print the first and the second:

awk -F '\t' '{print $1 "\t" $2}'

And it's similar to cut:

cut -f 1,2

Upvotes: 3

Related Questions