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 file extension .v.gz and I want the output files to have .txt extension instead.

For example, input filename:


output filename


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?

I am not looking for shell primitive

  • Are you asking how to put the output of the zgrep|sed|cut pipeline into files which match the input file but with a different extension?
    – icarus
    Commented Nov 17, 2022 at 4:26
  • @icarus yes I want to store output into the same file name but .txt extension the input file extension is .v.gz Commented Nov 17, 2022 at 4:32

1 Answer 1


Use a for loop to process each file

for f in *.v.gz
    zgrep -A1 "^module" "$f" | 
    sed -n -e 's/^\(module \)*\(.*(.*)\).*$/\2/p' |
    cut -f1 -d"(" > "${f%.v.gz}.txt"

I have a slight doubt about the sed program, I would have guessed that you wanted

sed -n -e 's/^module *\([^(]*\).*$/\1/p'

and then you don't need the cut at all.

  • your guess is correct but in sed, I am taking data between two strings there is no limit for lines.. in your code it'll consider only one line(module ). Commented Nov 17, 2022 at 5:00
  • 1
    My point is more that you don't need to post process sed output with cut, you can just choose to print less with sed.
    – icarus
    Commented Nov 17, 2022 at 5:13
  • i used your command but i am not getting the answer what i want Commented Nov 17, 2022 at 5:23
  • Can you add some sample output from the zgrep to the question?
    – icarus
    Commented Nov 17, 2022 at 6:18

You must log in to answer this question.

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