I want the sed command to work on all files in a directory and output the result into a new file. If f is val1.txt, the name of the output file should be val1_ended.txt -this is what I am trying to do with echo and sed, but it doesn't work. Any help would be appreciated. Thank you!

files= ls *.txt

echo "${files}"

for f in ${files}$


echo $f

sed -n -e 's/Trial End/&/p' $f>`  echo $f | sed 's/.txt$/_ended.txt/g' `


In my input files, lines are as such:

3413476   999.3   549.3  1876.0 32768.0
3413477  1000.7   549.6  1880.0 32768.0
3413478   999.3   551.1  1875.0 32768.0
INPUT   3413485 127
END 3413485     SAMPLES EVENTS  RES   59.84   45.82
MSG 3413491 Trial End   2
MSG 3414099 RECCFG CR 1000 0 0 R

While in output, I want all lines where it finds the pattern "Trial End":

MSG 3411256 Trial End   1
MSG 3413491 Trial End   2
MSG 3415678 Trial End   3
MSG 3417842 Trial End   4
MSG 3420114 Trial End   5
  • your syntax is stuck somewhere between windows and bash. First files=$(ls *.txt) and it is not a good reference. You can do this in the for line as for f in *.txt. And for me to make a comment on your sed command, I need to see a few sample lines from your input file and your expected output.
    – MelBurslan
    Commented Mar 16, 2016 at 0:41
  • Wow, does this code snippet and the answers remind me of the Evolution of a Programmer. I guess I'm a guru hacker by now! :D
    – Wildcard
    Commented Mar 16, 2016 at 1:18

2 Answers 2

for file in *.txt; do
  grep 'Trial End' "$file" >> "${file%.txt}_ended.txt"

I'm using >> instead of > just in case any of the *_ended.txt files already exists—>> will append to the file rather than truncating and overwriting it.

Another thing about this is it's not very reusable. If you've already run it once then you'll have filename_ended.txt files that match the pattern and get processed, and you'll have filename_ended_ended.txt files.

You're better off using another extension for these files to prevent this potential hassle in the future (and by the way, extensions don't matter much in the Unix world).

for file in *.txt; do grep 'Trial End' "$file" >> "$file.ended"; done
  • @user2484316, great! Here on stack exchange, the preferred method of thanks is by accepting the answer that you found most helpful. There's a green checkmark to the left of the answer. ;)
    – Wildcard
    Commented Mar 16, 2016 at 1:23

How about using grep? Try the following:

for f in *.txt; do
out=$(echo ${f} | sed 's/.txt$/_ended.txt/')
grep 'Trial End' $f > $out

You must log in to answer this question.

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