0

Currently I am building one script to grep region records in the config file based on parameter and then I am creating a text file with that output and I am reading the source file path in that output file. Now I need to pass one more parameter like module based upon that it has to create a text file.

My script:

#!/bin/bash

SOURCE_CONF="./test.properties"

#input region name
region="$1"
echo $region
cwd=$(pwd)
calu=$(grep $region test.properties > ab.txt)

IFS=$'\n'

#loop to read the config one line at a time
while read p; do
    echo $p

    a=($(echo "$p" | tr '|' '\n'))
    echo "Region is:" "${a[0]}"
    echo "path is:""${a[3]}"
    path="${a[3]}"
    #remove white space before path
    echo $cwd
    NO_LEAD_SPACE="$(echo -e $path | tr -d '[:space:]')"
    echo "path is:"$NO_LEAD_SPACE
    # change to path spacified in the config file
    sh -c 'cd $NO_LEAD_SPACE && echo "in the subshell" && echo $pwd && echo "exit subshell" && exec pwd'
    echo $cwd
    echo "--------------------"
done < ab.txt

echo "abc"

My config file:

region | Module |country code | filepath |filename


APJ | WDMD | MYS |/c/Users/vattikun/desktop | MYS*ADPGV*.XML
APJ | WDED | EUR|/c/Users/vattikun/desktop/Payroll Support | EUR*ADPGV*.XML

APJ | WDMD | RER|/c/Users/vattikun/desktop/Payroll Support | RER*ADPGV*.XML

APJ | WDJD | EYZ|/c/Users/vattikun/desktop/Payroll Support | EYZ*ADPGV*.XML


EMA | WDMD | AUS|/c/Users/vattikun/desktop | AUS*ADPGV*.XML
EMA | WDMD | AYS| /c/Users/vattikun/desktop | AYS*ADPGV*.XML

AMS | WDMD |ITI| /c/Users/vattikun/desktop | ITI*ADPGV*.XML
AMS |WDMD |AYS| /c/Users/vattikun/desktop | AYS*ADPGV*.XML

ETIME | WDMD |ADP /c/Users/vattikun/desktop | ADP_WDET_JOBCODE*.XML
ETIME | WDMD |AEP| /c/Users/vattikun/desktop | AEP_WDET_JOBCODE*.XML
2
  • 1
    It is unclear what the result should look like given the data that you show.
    – Kusalananda
    Jun 29, 2018 at 9:23
  • Your grep $region test.properties will match lines you don't want. For example, if region=XML, all of the entries will match. A better version is grep "^ *${region}\\>" test.properties. And sh -c 'cd $NO_LEAD_SPACE && ...' won't work because the single quotes don't interpolate the variable into the command, and NO_LEAD_SPACE isn't exported. It would work better to use (cd $NO_LEAD_SPACE && ...) instead.
    – Tim Smith
    Jun 29, 2018 at 17:50

1 Answer 1

0

To answer the specific question you asked, start your search expression with a pattern that will skip one field:

module="$2"
/bin/grep "^[^|]*| *${module}\\>" test.properties

To break that down:

^      Start of line
[^|]*  0 or more non-separator characters
|      A field separator
 *     0 or more spaces
${module}  Switch from single- to double-quotes to interpolate variable name
\\>    End of a word ("\\>" becomes '\>' when shell parses it)

But you're better off using something like awk or perl:

awk -F' *\\| *' -v module="$module" '$2 == module' test.properties

In the long run it might be better to use a well-known format like CSV and tools meant to work with it.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.