Unix/Linux

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • bash script to select characters from a string

    3 answers - 1352 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    hi,
    i have two molecular dynamics codes, both of them take the same
    input, but the in different format. let me explain,
    for one code (named PinyMD) in the input file, we have,
    ~atom_def[\atom_typ{na}\atom_ind{ 1 } \mass{14.0067}\charge{ 0.15}]
    ~atom_def[\atom_typ{na}\atom_ind{ 2 } \mass{14.0067}\charge{ 0.15}]
    ~atom_def[\atom_typ{cr}\atom_ind{ 3 } \mass{12.0107}\charge{-0.11}]
    ~atom_def[\atom_typ{cw}\atom_ind{ 4 } \mass{12.0107}\charge{-0.13}]
    ~atom_def[\atom_typ{cw}\atom_ind{ 5 } \mass{12.0107}\charge{-0.13}]
    and so on
    and for other code (named NMA) the input file,
    1 1
    2 1
    3 2
    4 1
    5 1
    6 2
    7 1
    8 1
    and so on
    the relation between them is very straightforwd.
    what appears in PinyMD's input file as "na" might be an integer 1
    or "cr" be integer 2 for NMA.
    i am trying to write a bash script, which will read in PinyMD input
    file and convert in the NMA file,
    after reading the line,
    ~atom_def[\atom_typ{na}\atom_ind{ 1 } \mass{14.0067}\charge{ 0.15}]
    ~atom_def[\atom_typ{cr}\atom_ind{ 2 } \mass{14.0067}\charge{ 0.15}]
    so on
    it should echo,
    1 1
    2 3
    so on
    how do i pick up elements 'na' (which i will replace with some
    integer) and atom_ind{ 1 } which i will replace with 1?
  • No.1 | | 1555 bytes | |

    2005-09-03, Harshal wrote:
    hi,
    i have two molecular dynamics codes, both of them take the same
    input, but the in different format. let me explain,

    for one code (named PinyMD) in the input file, we have,

    ~atom_def[\atom_typ{na}\atom_ind{ 1 } \mass{14.0067}\charge{ 0.15}]
    ~atom_def[\atom_typ{na}\atom_ind{ 2 } \mass{14.0067}\charge{ 0.15}]
    ~atom_def[\atom_typ{cr}\atom_ind{ 3 } \mass{12.0107}\charge{-0.11}]
    ~atom_def[\atom_typ{cw}\atom_ind{ 4 } \mass{12.0107}\charge{-0.13}]
    ~atom_def[\atom_typ{cw}\atom_ind{ 5 } \mass{12.0107}\charge{-0.13}]
    and so on

    and for other code (named NMA) the input file,

    1 1
    2 1
    3 2
    4 1
    5 1
    6 2
    7 1
    8 1
    and so on
    --
    the relation between them is very straightforwd.
    what appears in PinyMD's input file as "na" might be an integer 1
    or "cr" be integer 2 for NMA.

    i am trying to write a bash script, which will read in PinyMD input
    file and convert in the NMA file,
    --
    after reading the line,
    ~atom_def[\atom_typ{na}\atom_ind{ 1 } \mass{14.0067}\charge{ 0.15}]
    ~atom_def[\atom_typ{cr}\atom_ind{ 2 } \mass{14.0067}\charge{ 0.15}]
    so on
    --
    it should echo,

    1 1
    2 3
    so on

    how do i pick up elements 'na' (which i will replace with some
    integer) and atom_ind{ 1 } which i will replace with 1?

    awk -F '[{}]' '{ print $2, $4 }' FILENAME

    You will need a translation table to replace na, cr, etc with a
    number, unless it is a straight sequence.
  • No.2 | | 2126 bytes | |

    2 Sep 2005 20:29:09 -0700, Harshal <p.harshal@gmail.comwrote:
    | hi,
    | i have two molecular dynamics codes, both of them take the same
    | input, but the in different format. let me explain,
    |
    | for one code (named PinyMD) in the input file, we have,
    |
    | ~atom_def[\atom_typ{na}\atom_ind{ 1 } \mass{14.0067}\charge{ 0.15}]
    | ~atom_def[\atom_typ{na}\atom_ind{ 2 } \mass{14.0067}\charge{ 0.15}]
    | ~atom_def[\atom_typ{cr}\atom_ind{ 3 } \mass{12.0107}\charge{-0.11}]
    | ~atom_def[\atom_typ{cw}\atom_ind{ 4 } \mass{12.0107}\charge{-0.13}]
    | ~atom_def[\atom_typ{cw}\atom_ind{ 5 } \mass{12.0107}\charge{-0.13}]
    | and so on
    |
    | and for other code (named NMA) the input file,
    |
    | 1 1
    | 2 1
    | 3 2
    | 4 1
    | 5 1
    | 6 2
    | 7 1
    | 8 1
    | and so on
    |
    |
    | the relation between them is very straightforwd.
    | what appears in PinyMD's input file as "na" might be an integer 1
    | or "cr" be integer 2 for NMA.
    |
    | i am trying to write a bash script, which will read in PinyMD input
    | file and convert in the NMA file,
    |
    |
    | after reading the line,
    | ~atom_def[\atom_typ{na}\atom_ind{ 1 } \mass{14.0067}\charge{ 0.15}]
    | ~atom_def[\atom_typ{cr}\atom_ind{ 2 } \mass{14.0067}\charge{ 0.15}]
    | so on
    |
    |
    | it should echo,
    |
    | 1 1
    | 2 3
    | so on
    |
    | how do i pick up elements 'na' (which i will replace with some
    | integer) and atom_ind{ 1 } which i will replace with 1?

    $ cat PinyMD
    ~atom_def[\atom_typ{na}\atom_ind{ 1 } \mass{14.0067}\charge{ 0.15}]
    ~atom_def[\atom_typ{na}\atom_ind{ 2 } \mass{14.0067}\charge{ 0.15}]
    ~atom_def[\atom_typ{cr}\atom_ind{ 3 } \mass{12.0107}\charge{-0.11}]
    ~atom_def[\atom_typ{cw}\atom_ind{ 4 } \mass{12.0107}\charge{-0.13}]
    ~atom_def[\atom_typ{cw}\atom_ind{ 5 } \mass{12.0107}\charge{-0.13}]

    $ awk -F '[{} ]+' '{print $4 " " $2}' PinyMD | sed -e 's/na/1/' -e 's/cr/2/' -e 's/cw/3/'
    1 1
    2 1
    3 2
    4 3
    5 3

    Change the values in the sed substitute commands as appropriate.
  • No.3 | | 538 bytes | |

    Harshal <p.harshal@gmail.comwrote:

    after reading the line,
    ~atom_def[\atom_typ{na}\atom_ind{ 1 } \mass{14.0067}\charge{ 0.15}]
    ~atom_def[\atom_typ{cr}\atom_ind{ 2 } \mass{14.0067}\charge{ 0.15}]
    so on

    it should echo,

    1 1
    2 3
    so on

    how do i pick up elements 'na' (which i will replace with some
    integer) and atom_ind{ 1 } which i will replace with 1?

    Parse out '{}', and what you want is first and second elements.

    man awk(-F option)
    man grep(-o option)

Re: bash script to select characters from a string


max 4000 letters.
Your nickname that display:
In order to stop the spam: 0 + 9 =
QUESTION ON "Unix/Linux"

EMSDN.COM