within of my bash code I have a part of an sed + AWK code, which do interatively some operation on input file and add the results to another txt file (the both filles had been created by the same bash script, and can be stored as different variables).
#sed removing some lines in input fille "${file}".xvg, defined in the begining of bash script
sed -i '' -e '/^[#@]/d' "${file}".xvg
# AWK measure XMAX and YMAX in the input file
# adding these outputs as two lines in another batch.bfile, which is going to be used for something
awk '
NR==1{
max1=$1
max2=$2
}
$1>max1{max1=$1}
$2>max2{max2=$2}
END{printf "WORLD XMAX %s\nWORLD YMAX %s\n",max1+0.5,max2+0.5'} "${file}".xvg >> "${tmp}"/batch.bfile
is it possible to combine the both (sed +awk ) actions into some function (defined in the beggining of my bash script) and then use it as one line command within the script (in more sophisticated cas it will be applied on many filles within FOR loop)?
Here is example of my version:
#!/bin/bash
#folder with batch file
home=$PWD
tmp="${home}"/tmp
## define some functions for file processing
bar_xvg_proc () {
##AWK procession of XVG file: only for bar plot;
sed -i '' -e '/^[#@]/d' ${file}
# check XMAX and YMAX for each XVG
awk '
NR==1{
max1=$1
max2=$2
}
$1>max1{max1=$1}
$2>max2{max2=$2}
END{printf "WORLD XMAX %s\nWORLD YMAX %s\n",max1+0.5,max2+0.5'} ${file} >> "${tmp}"/grace2.bfile
}
###
bar_xvg_proc "${home}"/test.xvg
and here is an error from sed
sed: -i may not be used with stdin
BUT if I define my test.xvg as a new variable $file="${home}"/test.xvg before calling my function in the script - it works well. How I could use this function directly with input file (w/o the specific variable assigned to the file )?
Here is my xvg file:
# Created by:
# :-) GROMACS - gmx cluster, 2019.3 (-:
#
# Executable: /usr/local/bin/../Cellar/gromacs/2019.3/bin/gmx
# Data prefix: /usr/local/bin/../Cellar/gromacs/2019.3
# Working dir: /Users/gleb/Desktop/DO/unity_or_separation
# Command line:
# gmx cluster is part of G R O M A C S:
#
# Good gRace! Old Maple Actually Chews Slate
#
@ title "Cluster Sizes"
@ xaxis label "Cluster #"
@ yaxis label "# Structures"
@TYPE xy
@g0 type bar
1 94
2 31
3 24
4 24
5 15
6 6
7 6
8 5
9 4
10 4
11 3
12 3
13 3
14 3
15 2
16 2
17 2
18 2
19 1
20 1
21 1
22 1
23 1
24 1
25 1
sed
call simply serves to remove "comment" lines that start with#
or@
, so that the subsequentawk
call processes a file that only contains numbers. Can you post an example of one such.xvg
files? Are you sure the "in-place editing" of the.xvg
file viased
to remove such comments is necessary?