'Printing the numbers after match (bash)
I'm trying to write a script (with bash) that looks for a word (for example "E( ENERGY) =") and prints the rest of the line which is effectively some numbers with "-" in front. To clarify, an example line that I'm looking for in a file is;
MOST-DESIRED RESULT E( ENERGY) = -9.68765465413
All I need is -9.68765465413 to be printed. I've managed to print the whole line, but this is rather ineffective for me. The script that I have in hand up to this point is as follows (named script.sh):
#!/usr/bin/env bash
output="$1"
ext="log"
word="E( ENERGY)"
find "$output" -type f -name "*.$ext" -exec grep -wFnH "$word" {} +
And I loop this script in another script (run_script.sh) for the files in a folder to look at all the files I have. And my current output looks like this;
RESULT1_DH.log:567: MOST-DESIRED RESULT E( ENERGY) = -1.0501552997
RESULT2.log:567: MOST-DESIRED RESULT E( ENERGY) = -1.0501552997
But I want it to look like this;
RESULT1_DH.log -1.0501552997
RESULT2.log -1.0501552997
If anyone can help me with this, I'll be grateful. Thanks in advance!
Solution 1:[1]
Sample inputs:
$ head RESULT*log
==> RESULT1_DH.log <==
MOST-DESIRED RESULT E( ENERGY) = -1.0501552997
MOST-DESIRED RESULT E( POWER) = -9.9999999999
==> RESULT2.log <==
MOST-DESIRED RESULT E( ENERGY) = -1.0501552997
MOST-DESIRED RESULT E( POWER) = -9.9999999999
One awk idea:
output='.'
ext='log'
word="E( ENERGY)"
find "${output}" -type f -name "*.${ext}" -exec awk -v ptn="${word}" 'index($0,ptn) {print FILENAME,$NF}' {} +
This generates:
./RESULT1_DH.log -1.0501552997
./RESULT2.log -1.0501552997
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
