Printing columns using awk or cut?

I am writing a script that will take the file name as an argument, find the word a specific word at the beginning of each line - the word ATOM in this case - and print the values ​​from specific columns.

$FILE=*.pdb *

if test $# -lt 1
then
 echo "usage: $0 Enter a .PDB filename"
 exit
fi
if test -r $FILE
then
 grep ^ATOM $FILE | awk '{ print $18 }' | awk '{ print NR $4, "\t" $38,}'
else
 echo "usage: $FILE must be readable"
 exit
fi

I am having trouble figuring out three issues:

  • How to use awk to print only lines containing ATOM as the first word
  • How to use awk to print only certain columns of rows that match the above criteria, in particular columns 2-20 and 38-40
  • How can I indicate that this should be a pdb file? * .pdb *
+3
source share
3 answers

, awk. grep cut ...

if [ $# -lt 1 ];then
 echo "usage: $0 Enter a .PDB filename"
 exit
fi
FILE="$1"
case "$FILE" in
*.pdb )

if test -r $FILE
then 
 # do for 2-20 assuming whites paces as column separators
 awk '$1=="ATOM" && NF>18 { 
   printf "%s ",$2
   for(i=3;i<=19;i++){
     printf "%s ",$i
   }
   printf "%s",$20   
 }' "$FILE"
else
 echo "usage: $FILE must be readable"
 exit
fi
;;
*) exit;;
esac
+1
  • awk '$1 == "ATOM"' $FILE
    
  • , , cut:

    grep ^ATOM $FILE | cut -c 2-20,38-40
    
  • , , script, .pdb: -, , ( UNIX), -, , :

    "${1%%.pdb}" == "$1" && echo "usage:..." && exit 1
    

    ($1), .pdb, , . , , 1.

+4

, bash, :

#!/bin/bash

declare    key="ATOM"
declare    print_columns=( 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 38 39 40 )

[ ! -f "${1}" ] && echo "File not found." && exit
[ "${1%.pdb}" == "${1}" ] && echo "File is wrong type." && exit

while read -a columns; do
  if [ ${columns[0]} == ${key} ]; then
    printf "%s " ${key}
    for print_column in ${print_columns[@]}; do
      printf "%s " ${columns[${print_column}]}
    fi
    printf "\n"
  fi
done < ${1}
0

Source: https://habr.com/ru/post/1769795/


All Articles