1

I am performing some operations using the grep, sed, and cut commands on a Linux machinete:

zgrep -A1 "^module" *.v.gz | sed -n -e 's/^\(module \)*\(.*(.*)\).*$/\2/p' | cut -f1 -d"("

This is being run on files with the extension .v.gz and I want the output files to have a .txt extension instead.

For example, input filename:

abcd.v.gz
efgh.v.gz
ijkl.v.gz
mnop.v.gz

output filename

abcd.txt
efgh.txt
ijkl.txt
mnop.txt

So I want to keep the same file name prefix, but change the extension. How can I do this for an arbitrary number of files?

1
  • Regarding ` using the grep, sed, and cut` - any time you find yourself chaining together multiple Unix commands for manipulating text there's an excellent chance you could do the same with a single awk script.
    – Ed Morton
    Commented Nov 19, 2022 at 13:33

1 Answer 1

1

I use a shell loop in this type of case:

for file in *.v.gz; do zgrep -A1 "^module" "$file" | sed -n -e 's/^\(module \)*\(.*(.*)\).*$/\2/p' | cut -f1 -d"(" > $(basename "$file" .v.gz).txt; done

You may even not need the basename command if your shell has a primitive for that kind of operation.

As a complete shell script, it would look like:

#!/bin/bash

for file in *.v.gz
do
    zgrep -A1 "^module" "$file" | sed -n -e 's/^\(module \)*\(.*(.*)\).*$/\2/p' | cut -f1 -d"(" > $(basename "$file" .v.gz).txt
done

3
  • @Igeorget i have no idea about shell scripting Commented Nov 17, 2022 at 4:04
  • it's giving me an error as -script_shell.sh: line 2: syntax error: unexpected end of file, Commented Nov 17, 2022 at 4:05
  • @SanthoshNayakD. Sorry I forgot the end of the loop... the "done" keyword
    – lgeorget
    Commented Nov 17, 2022 at 17:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .