Ducki
Ducki

Reputation: 21

Applying awk pattern to all files with same name, outputting each to a new file

I'm trying to recursively find all files with the same name in a directory, apply an awk pattern to them, and then output to the directory where each of those files lives a new updated version of the file.

I thought it was better to use a for loop than xargs, but I don't exactly how to make this work...

for f in $(find . -name FILENAME.txt ); do awk -F"\(corr\)" '{print $1,$2,$3,$4}' ./FILENAME.txt > ./newFILENAME.txt $f; done

Ultimately I would like to be able to remove multiple strings from the file at once using -F, but also not sure how to do that using awk.

Also is there a way to remove "(cor*)" where the * represents a wildcard? Not sure how to do while keeping with the escape sequence for the parentheses

Thanks!

Upvotes: 2

Views: 320

Answers (1)

John1024
John1024

Reputation: 113924

To use (corr*) as a field separator where * is a glob-style wildcard, try:

awk -F'[(]corr[^)]*[)]' '{print $1,$2,$3,$4}'

For example:

$ echo '1(corr)2(corrTwo)3(corrThree)4' | awk -F'[(]corr[^)]*[)]' '{print $1,$2,$3,$4}'
1 2 3 4

To apply this command to every file under the current directory named FILENAME.txt, use:

find . -name FILENAME.txt -execdir sh -c 'awk -F'\''[(]corr[^)]*[)]'\'' '\''{print $1,$2,$3,$4}'\'' "$1" > ./newFILENAME.txt' Awk {} \;

Notes

Don't use:

for f in $(find . -name FILENAME.txt ); do

If any file or directory has whitespace or other shell-active characters in it, the results will be an unpleasant surprise.

Handling both parens and square brackets as field separators

Consider this test file:

$ cat file.txt
1(corr)2(corrTwo)3[some]4

To eliminate both types of separators and print the first four columns:

$ awk -F'[(]corr[^)]*[)]|[[][^]]*[]]' '{print $1,$2,$3,$4}' file.txt
1 2 3 4

Upvotes: 1

Related Questions