Posts mit dem Label awk werden angezeigt. Alle Posts anzeigen
Posts mit dem Label awk werden angezeigt. Alle Posts anzeigen

Montag, 30. Mai 2011

Shell 006: Replacing String with incrementing index

Lets say we have some text as below.

header
foo
foo
foo
header
bar
bar
bar
header
baz
baz
baz

where we want the "header" be replaced by "model {1,2,3}".
I was pointed out to a couple of solutions over at LinuxQuestions.org

An awk solution.

$ awk 'BEGIN { cntr = 0 } /header/ { cntr++ ; print "model", cntr } !/header/ { print $0 }' infile

where 'infile' contains the data.

A bash solution.

#!/bin/bash

filename=data

index=1

for entry in `grep -n model $filename`
do
line=`echo $entry | awk -F":" '{print$1}'`

sed -e "$line s/model/model $index/" -i $filename

index=$(($index + 1))
done


An even shorter awk solution.

awk '$0 = /header/?"model "++c:$0' file


A one line bash solution

unset count; while read line; do [[ $line =~ header ]] && line="model $((++count))"; echo "$line" >> oufile; done < infile


Great to have the support from the online community.

Mittwoch, 13. April 2011

Shell-004: Selective Listing

Given the case that a number of files are in a directory, file-test.py and at the same time there is a directory called file-test in the same directory. How can only the directories be listed? This is useful when the content of the directories needs to be processed somehow.

ls -l|grep ^d|awk '{print $8}'

Grep matches the beginning of the line output by 'ls' with 'd', which indicates a directory. " 'awk {print $8}' " then prints the 8th column of the output (so the directory name is available again).

Sonntag, 3. April 2011

GAMESS 002: Copying to a new job type

When a batch of optimizations finished, the final coordinates need to be transfered to for example a HESSIAN job. At the same time, the file should change the extension from .log to .inp and the jobtype identifier should change from opt to hes. On the command line, this can be done in a for-loop by issueing:
mzh @ ~/shell $ for i in a-{1,2,3}-opt.log
do
OPTLOG=$i
HESLOG=${OPTLOG/opt/hes} # Rename jobtype
HESINP=${HESLOG/log/inp} # Rename extension
grep -i -A 29 "equilibrium" $OPTLOG|tail -n 26 >> $HESINP
done

I dont know how to rename two parts of a file name in the same step, i.e. how to get from 'a-{1,2,3}-job-opt.log' to 'a-{1,2,3}-job-hes.inp' in one command. That's why I rename part of the file step by step. Then, after I have the filename ready in a variable, the important content is written to this file. The molecule has 26 atoms, but the identifier is 29 lines above the end of the coordinate block, that's why I tail it (I dont know how to identify an empty line which terminates the coordinate block-- there is no identifier for the ending of the coordinate block). The generic command would be to insert N+3 where N is the number of atoms in the file.IMPORTANT: remember the '>>' append operator when transfering to the input file.

Now a more tricky part, transfering the Hessian matrix from the GAMESS output .dat file to the SADPOINT job file. How can this be automated in a smart way?
This line of code is the solution:

mzh ~/shell $ awk '/ \$HESS/,/^ \$END/' hess.dat >> sad.inp

I dont know what the ',' does. Note the quotes.

There also appear to be other ways of doing this:


mzh ~/shell $ ruby -ne 'print if /\$HESS/../\$END/' file

or

mzh ~/shell $ f=0;
while read l;
do [[ $l =~ \$HESS ]] && f=1;
[[ f -eq 1 ]] && echo $l;
[[ $l =~ \$END ]] && f=0;
done < file

These other options are less readable to me, but seem to work nicely.