Delete multiple columns using awk

Asked

Viewed 249 times

3

I have a database with 6037 columns and 450 rows as below:

1807 1452 1598 1 6.655713  A B A B ... 0 
1808 1452 1763 1 9.362033  0 0 A B ... A 
1809 1452 1527 2 6.728534  A B A A ... B 
1810 1452 1367 2 9.4055  A B A A B ... A 
... ... ... ... ... ... ... ... ... ...
1812 1452 1258 1 6.363032  0 0 A B ... B

I want to get a new database with only the first 676 columns.

Preferably some form that uses awk or sed.

Thanks in advance.

  • Using a loop would not be enough?? awk -F" " '{ for(i=1; i<=$qntColunas; i++) print $i }'

  • 1

    Thanks, I got it sorted.

  • Hello @andrec, Welcome to Sopt, I see you found a solution. If your solution is different from those proposed by the community, you can add an answer showing how you solved it, it would be of great help to people in the community who have the same problem. If any solution proposed by the community helped you can accept the answer, by accepting an answer will reward those who helped you and help those who have the same problem and still keep the site healthy because your question is no longer a unresolved question. =D -- It is only worth you to take a look at our [Tour]

4 answers

1

Create a cols.awk script file:

{
  for (i = 1; i < 676; i++)
    printf("%s ", $i)
  printf($676 "\n")
}

shell run the command:

awk -f cols.awk seu_arquivo > novo_arquivo  

That’s it.

  • Interesting, thank you.

1

Good night. I managed to resolve this issue with the script below:

awk '{for (i=1;i<=676;i++) {printf (i==1?"":FS)$i}; print ""}' <file.in >file.out

The input file is "file.in" with 6037 columns. The output file is "file.out" with the first 676 columns. To get other files with other amounts of initial columns just change the number 676 by other amounts of columns.

Thank you.

1

  • Thanks, I got it sorted.

1

I suggest:

awk 'NF=676'  arquivo > novo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.