Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/bash
- N=10000
- echo "raw file:"
- ls -lh OUTCAR; wc -l OUTCAR
- echo
- > NEW_OUTCAR
- for _ in $(seq $N); do
- cat OUTCAR >> NEW_OUTCAR
- done
- cat >> NEW_OUTCAR <<'EOF'
- magnetization (z)
- # of ion s p d f tot
- --------------------------------------------------
- 1 0.037 0.030 4.519 5.000 9.586
- 2 -0.037 -0.030 -4.519 -5.000 -9.586
- 3 0.000 0.000 0.000 0.000 0.000
- 4 0.000 0.000 0.000 0.000 0.000
- 5 0.000 0.000 -0.000 -0.000 0.000
- 6 0.000 0.000 -0.000 -0.000 0.000
- 7 0.000 0.000 -0.000 -0.000 0.000
- 8 0.000 0.000 -0.000 -0.000 0.000
- 9 0.000 0.000 -0.000 -0.000 0.000
- 10 0.001 0.001 -0.000 -0.000 0.002
- ------------------------------------------------------
- tot 0.001 0.002 0.000 0.000 0.003
- EOF
- echo "huge file:"
- ls -lh NEW_OUTCAR; wc -l NEW_OUTCAR
- echo
- echo -n ">>> awk with getline ... "
- /usr/bin/time -f %es awk '
- /magnetization \(z\)/ {
- data=""; getline; getline; getline;
- for (i=1; i<=10; i++) {
- getline
- data = data sprintf("%8.3f\n", $NF)
- }
- }
- END {printf data}
- ' NEW_OUTCAR > res.awk_with_getline
- echo
- echo -n ">>> awk without getline ... "
- /usr/bin/time -f %es awk '
- {
- if ($0 ~ /magnetization \(z\)/) { f = 1; c = "" }
- if (f == 1) {
- if ($1 ~ /^[0-9]+$/) c = c sprintf("%8.3f\n", $NF)
- if ($1 ~ /tot/) f = 0
- }
- }
- END { printf c }
- ' NEW_OUTCAR > res.awk_no_getline
- echo
- echo -n ">>> perl ... "
- /usr/bin/time -f %es perl -00ne '
- /magnetization \(z\)/ and $f=1 and next;
- $f==1 and ($c,$f)=$_,0;
- }{ map {/^\s*\d+\b/ and printf "%8.3f\n", (split " ")[-1]}
- split /\n/, $c
- ' NEW_OUTCAR > res.perl
- echo
- echo -n ">>> python ... "
- /usr/bin/time -f %es python -c '
- import sys
- f, c = 0, []
- with open(sys.argv[1]) as input_file:
- for line in input_file:
- if "magnetization (z)" in line:
- f = 1
- c = []
- elif f == 1 and line.strip():
- first_field = line.strip().split()[0]
- if first_field.isdigit():
- last_field = line.strip().split()[-1]
- c.append(format(float(last_field), "8.3f"))
- elif first_field == "tot":
- f = 0
- print("\n".join(c))
- ' NEW_OUTCAR > res.python
- echo
- echo -n ">>> C ... "
- cat > manip.c <<'EOF'
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main(int argc, char *argv[]) {
- int f = 0;
- char c[1001] = {0};
- if (argc < 2) {
- printf("Please provide a filename as a command-line argument.\n");
- return 1;
- }
- FILE *file = fopen(argv[1], "r");
- if (file == NULL) {
- printf("Failed to open the file.\n");
- return 1;
- }
- char line[1001];
- while (fgets(line, sizeof(line), file) != NULL) {
- if (strstr(line, "magnetization (z)") != NULL) {
- f = 1;
- c[0] = '\0';
- } else if (strncmp(line, "tot", 3) == 0) {
- f = 0;
- } else if (f == 1) {
- char *token;
- char *lastToken = NULL;
- token = strtok(line, " ");
- while (token != NULL) {
- lastToken = token;
- token = strtok(NULL, " ");
- }
- char *endptr;
- double value = strtod(lastToken, &endptr);
- if (endptr != lastToken) {
- char temp[10];
- snprintf(temp, sizeof(temp), "%8.3f\n", value);
- strcat(c, temp);
- }
- }
- }
- fclose(file);
- printf("%s", c);
- return 0;
- }
- EOF
- gcc -O2 -o manip manip.c
- /usr/bin/time -f %es ./manip NEW_OUTCAR > res.c
- echo
- echo "answer check:"
- sha256sum res.*
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement