Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

sed - What does <"$fileVariableName"> do in csh?

I am reviewing an existing script using sed commands in cshell, and I understand all of it except one component. I have simplified it down for sharing an example but below, $templateFile is used as input, and all instances of "hello" are replaced with "world", and once this is done, it is output to the output directory and named with the output file name.

sed -e 's:hello:world:g' <"$templateFile"> "$outputDir"/"$outputFileName".txt

However, I don't understand what the <> around "$templateFile" is doing? Why is it necessary to have the <> for a sed input file? In all descriptions of sed, I can't find an explanation for what purpose this might be serving so I am a bit confused.

I understand this is a simple question, but I cannot find an answer online and I'd appreciate any clarification here. Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The < and > are stream redirection operators.

You do not need the < operator because sed accepts a filename argument after the sed command.

If you plan to save sed replacement result in a new file, > is required. If you want to save the file contents "inline", there is no obligation to use > either. With GNU sed, you can do

sed -i 's:hello:world:g' "$templateFile"

With Free BSD sed, you can do

sed -i '' 's:hello:world:g' "$templateFile"

Check the sed edit file in place question.

You do not need -e either, it only signals that the next argument is the sed command.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...