2

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}$

do 

echo $f

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

done

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
2
  • 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. 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 Commented Mar 16, 2016 at 1:18

2 Answers 2

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

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
1
  • @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. ;) Commented Mar 16, 2016 at 1:23
0

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
done
4
  • See Why does my shell script choke on whitespace or other special characters? Commented Mar 16, 2016 at 1:14
  • Also Why is printf better than echo? Commented Mar 16, 2016 at 1:20
  • And by the way, I link people to those all the time; nothing personal! :D They're just crucial data for script writers. On another note, you don't need the g flag in your sed command when you're using an anchored regex. Commented Mar 16, 2016 at 1:22
  • Ha, I know, I just grabbed the sed invocation from the original post, I didn't give that part much thought! Commented Mar 16, 2016 at 1:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.