skip to Main Content

Good Day!

I have files in Ubuntu like:

  • ZAF_MM_CYCLE_K051.XLS
  • ZAF_MM_CYCLE_K052.XLS
  • ZAF_MM_CYCLE_K053.XLS

which are copy of a file "ZAF_MM_CYCLE_K036", the code of file is K036

File contents are the same

LOADED_AGRS   ZAF_MM_CYCLE_K036
AGG_DEFINE    200ZAF_MM_CYCLE_K036  $WERKS  K036                                          
AGG_1521      200ZAF_MM_CYCLE_K036           

Here I have to replace the code wiith its own code according to filename.

for example:

file: ZAF_MM_CYCLE_K051.XLS

its code is K051, and I have to replace K036 to K051, so that content of the file would be:

LOADED_AGRS   ZAF_MM_CYCLE_K051
AGG_DEFINE    200ZAF_MM_CYCLE_K051  $WERKS  K051                                          
AGG_1521      200ZAF_MM_CYCLE_K051       

Can anyone help out, please?

2

Answers


  1. As Ron mentioned in the comment, you can do something like this with bash to generate the files with modified content that matches the index:

    # Iterate over the values from 036 to 051
    for i in {036..051}; do
      echo "Generating 'ZAF_MM_CYCLE_K${i}.XLS'"
      cat > ZAF_MM_CYCLE_K${i}.XLS << EOL
    LOADED_AGRS   ZAF_MM_CYCLE_K${i}
    AGG_DEFINE    200ZAF_MM_CYCLE_K${i} $WERKS  K${i}
    AGG_1521      200ZAF_MM_CYCLE_K${i}
    EOL
    done
    

    The above will generate the files as described in the current directory. The cat > ZAF_MM_CYCLE_K${i}.XLS << EOL uses a heredoc to generate the file. The filename and contents use the $i variable for the names. As the for loop executes, i gets incremented, i.e. 036, 037, 038,…051 etc. which is used to generate the file name and the contents accordingly.

    If you want to use something like sed to replace contents. Running the following in the directory where the files are should do the trick:

    # Iterate over the files and replace contents
    for f in ZAF_MM_CYCLE_K0{36..51}.XLS; do
    
      # Set a variable the same as the filename but strip off ZAF_MM_CYCLE_K
      n=${f//ZAF_MM_CYCLE_K/}
    
      # Further strip off the file extension
      n=${n%*.XLS}
    
      echo "Replacing K036 in $f with ${n}"
    
      # Replace the contents with the number found in the file name
      sed -i "s|K036|${n}|g" "$f"
    done
    

    Basically, we iterate over each file, set a variable name that contains the number from the filename by stripping the ZAF_MM_CYCLE_K and the .XLS using bash parameter expansion. Once the number is deduced, use sed to replace the contents on the given file. sed -i does an in-place replacement on the original file.

    Hope this helps!

    Login or Signup to reply.
  2. If all the files contain K036 the below script will help you.

    #!/bin/bash
    directory="/path/to/file"
    
    cd "$directory"
    
    for file in ZAF_MM_CYCLE_K*.xls; do
    
        code=$(echo "$file" | cut -d'_' -f4 | cut -d'.' -f1)
    
        sed -i "s/K036/$code/g" "$file"
    
    
        echo "Code replaced in $file"
    done
    

    where the

    echo "$file" | cut -d’_’ -f4 | cut -d’.’ -f1

    will extract the file name k051 from the filename

    sed -i "s/K036/$code/g" "$file"

    will replace the old value with new one

    So this would help you in your case.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search