Monday 15 July 2013

linux - bash print first to nth column in a line iteratively -


I am trying to get the column name of a file and am printing it again. I think the problem is with print $ i , but I do not know how to correct it, I'm trying to code:

  #! / Bin / bash for me {2..5} Set snp = head -n 1 small.txt | | Example input file:  
  ID name age gender state extension 1a 12 mt 811 2 esc '{print $ i}' echo $ snp done   

B12FUT818

desired output:

  name age gender state extension   

but I found Output is empty screen.

You just would better read the first line of your file and result one Archive as an array:

  Read-A Header & lt;  
  printf "% s \ n" "$ {header   

and then printf related fields: @]: 1 } "

In addition, it uses only, and does not contain any unnecessary edges.

Edit. To reply to your comment, you can enable the loop through the header field:

  read -a header & lt; Small for SNP in "$ {header [@]: 1}" What is "$ snp"   

Edit 2. There were many mistakes in its original method, here its the correct version (though I have written a better way to solve your problem before I wrote it):

  i in {2 .5}; "Snp = $ (head -n 1 small.txt | awk" {print \ $$ i} ")" $ snp "echo   
  • set < Due to single quotation marks in / awk '{print $ i}' , $ i
  • Code> never gets extended.
  • This algorithm is not good because you are calling head and awk 4 times, while you do not need it to be a single external process.

    Hope it helps!

No comments:

Post a Comment