user1988385
user1988385

Reputation: 2869

editing standard input file in shell script

I got a problem to read standard input file, to edit the standard input, and to write the output to other file. So, the shell will be run with command like this:

./shellName < inputFile > outputFile

so I write my shell like this:

read INPUT
tr '[:upper:]' '[:lower:]' INPUT
grep "<td>" INPUT | sed -r 's/<td>([^<]*)<\/td>/\1/g' #to search all line that contain <td> and delete <td> and <\/td>
echo $INPUT

then, I just realize that the command read is read the standard input line by line. the code itself didn't work. I tried to change all uppercase to lowercase, and then delete and </td> on line that contain . What should I do? if I require to create an temp file, how can I create an temporary file in the shell?

Upvotes: 2

Views: 1044

Answers (1)

DrC
DrC

Reputation: 7698

It is actually much easier than you think. By default, tr reads from stdin (fd 0). The commands inside your script "inherit" stdin from the script unless you redirect it. So something like

#!/bin/sh
tr '[:upper:]' '[:lower:]' | sed -E 's/<td>([^<]*)<\/td>/\1/g'

I changed -r to -E as that is needed on my platform.

Upvotes: 1

Related Questions