1

I have a script that prints:

dhcp-18-189-47-44:CE06_getting_new_fit myname$ ./find-duplicate-structures.sh custom_structures new_GS_calculation/selected/POSCAR_00* ../CE05-structures_recombined/enum-00135/POSCAR.ideal == new_GS_calculation/selected/POSCAR_0011 (RMSD = 1.15475827927e-06, max. displacement = 1.41428428091e-06) ../CE05-structures_recombined/enum-00146/POSCAR.ideal == new_GS_calculation/selected/POSCAR_0022 (RMSD = 1.16051714442e-06, max. displacement = 1.42835572031e-06) ../CE05-structures_recombined/enum-00150/POSCAR.ideal == new_GS_calculation/selected/POSCAR_0027 (RMSD = 3.40556388834e-16, max. displacement = 6.04819551804e-16) ../CE05-structures_recombined/enum-00151/POSCAR.ideal == new_GS_calculation/selected/POSCAR_0027 (RMSD = 4.01650747941e-16, max. displacement = 5.4726685759e-16) ../CE05-structures_recombined/enum-00163/POSCAR.ideal == new_GS_calculation/selected/POSCAR_0037 (RMSD = 1.99174954223e-06, max. displacement = 2.44948961046e-06) 

In internally the script looks like:

dirs=$1 shift while read dir do if [ -f $dir/POSCAR.ideal ] then poscar=$dir/POSCAR.ideal else poscar=$dir/POSCAR fi mg match --just-match $poscar $@ done < $dirs exit 0 

The printing occurs at the line mg match --just-match $poscar $@

However, in my practice, I would like to remove everything to the right "==", i.e.:

rm new_GS_calculation/selected/POSCAR_0011 rm new_GS_calculation/selected/POSCAR_0022 rm new_GS_calculation/selected/POSCAR_0027 rm new_GS_calculation/selected/POSCAR_0037 

How could I automate this process in shell script? Thank you .

1 Answer 1

2

You can try parsing your program's output with sed and executing the commands:

$(./YOUR_PROGRAM | sed s/^.*==/rm/ | sed s/\ \(.*//) 

will execute (from your example)

rm new_GS_calculation/selected/POSCAR_0011 rm new_GS_calculation/selected/POSCAR_0022 rm new_GS_calculation/selected/POSCAR_0027 rm new_GS_calculation/selected/POSCAR_0027 rm new_GS_calculation/selected/POSCAR_0037 


sed works with the following syntax:

sed s/string1/string2/ 

which replaces string1 with string2.

What is happening here is:

  1. the output of your program is sent to sed with the first pipe |
  2. for each line of output,sed everything from the beginning (marked by ^) to the == sign and replaces it with the letters rm. It sends the rest of the line into another sed call with a second pipe
  3. sed now looks for and removes a space followed by a left parenthesis, denoted by \ \(, and anything that follows, denoted by .*.
  4. the whole command is wrapped in $(...) so bash executes the output, which in this case a string of rm new_GS_calculation/... commands.
4
  • great... but for the convenience of user, do you think it is possible to add into the shell script itself? Thank you Commented Aug 27, 2015 at 17:15
  • You can just put the command in command.sh and execute it with ./command.sh. Commented Aug 27, 2015 at 17:23
  • In this command $(./YOUR_PROGRAM | sed s/^.*==/rm/ | sed s/\ (.*//), did you miss some parenthesis? ... Oh! I see from your explanation that no parenthesis is missed!!! Commented Aug 27, 2015 at 17:27
  • sed has been always a mystery to me, and you really explained it very well!!! Thank you. Commented Aug 27, 2015 at 17:33

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.