1

I have a script that for each file in a directory do something with the awk command.

For that i use a for loop, but i'd want that each awk command running for a file redirect my output to a file. Like 1 source file -> 1 target file.

For the moment, my shell is setup like this :

#!/bin/bash

FILES="/home/yha/AG2R/*"

for f in $FILES 
do 
    echo "Processing $f file.."; 
    awk -F ';' '$1=="TABLE" && $3=="" {printf "01 %s.\n\n", $2; next} {sub(/CHAR/,"PIC X", $2);printf "   * %s.\n\n     05 %s %s.\n\n", $3, $1, $2;}' $f > temp.cpy
done

As you can see i redirect the output in "temp.cpy" but not only does he only give me the last file of the directory output even if he'd put all my files transformation in that file this is not what i want as i described it at the beginning. I'd also like to redirect the outputs in a specific folder

2 Answers 2

3

Make the output file depend on $f.

#!/bin/bash

FILES="/home/yha/AG2R/*"
target_dir=/home/yha/AG2R/COPY

for f in $FILES 
do 
    echo "Processing $f file.."; 
    base=$(basename "$f")
    awk -F ';' '$1=="TABLE" && $3=="" {printf "01 %s.\n\n", $2; next} {sub(/CHAR/,"PIC X", $2);printf "   * %s.\n\n     05 %s %s.\n\n", $3, $1, $2;}' "$f" > "$target_dir/$base"
done
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you so much, is there a way to put the output files in a specific directory ?
Of course. Just put the directory name at the beginning. > "dir/$f.cpy"
I did this : "$TARGET_DIRECTORY/$f.cpy" but i get "No such file or directory" error
Then $TARGET_DIRECTORY must be wrong. If it's a relative pathname, make sure it's relative to your current directory. Using an absolute pathname avoids problems.
I've updated the answer. You should update the question to say that you want them in a different directory.
|
1

awk can do this by itself:

awk -F ';' '
    FNR == 1 {
        printf "Processing %s file..\n", FILENAME
        output = FILENAME ".out"
    }
    $1 == "TABLE" && $3 == "" {
        printf "01 %s.\n\n", $2 > output
        next
    } 
    {
        sub(/CHAR/,"PIC X", $2)
        printf "   * %s.\n\n     05 %s %s.\n\n", $3, $1, $2 > output
    }
' /home/yha/AG2R/*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.