简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM