如何用Shell脚本将/home/temp.txt中的行替换为/home/list.txt里的URL2?
Hey there! Let's tackle your two shell scripting requirements one by one—first a general approach to text string modification, then the specific line replacement task you mentioned.
Here are the most common, practical tools and methods I use day-to-day for string edits in shell scripts:
Using
sed(the go-to for quick replacements)
This is my first pick for simple to moderately complex edits. Basic syntax:# Replace first occurrence of "old_str" with "new_str" in each line sed 's/old_str/new_str/' input_file.txt # Replace ALL occurrences (global) in each line sed 's/old_str/new_str/g' input_file.txt # Directly modify the original file (add .bak to create a backup first) sed -i.bak 's/old_str/new_str/g' input_file.txtPro tip: If your string contains slashes (
/), swap the delimiter to something like#or@to avoid escaping headaches:sed 's#/usr/old/path#/usr/new/path#g' config.txtUsing
awk(better for complex logic)
Great if you need to combine string replacement with other line-based operations:# Replace all "old_str" with "new_str" and print all lines awk '{gsub(/old_str/, "new_str"); print}' input_file.txt # Save changes to a new file (then overwrite original if needed) awk '{gsub(/old_str/, "new_str"); print}' input_file.txt > output.txt && mv output.txt input_file.txtUsing
perl(powerful for regex-heavy edits)
Similar tosedbut with more robust regex support:# In-place edit with backup perl -pi.bak -e 's/old_str/new_str/g' input_file.txt
Always remember to back up your files before running in-place edits—better safe than sorry!
Let's break this into two clear steps: first extract the URL2 content from list.txt, then replace the target line in temp.txt.
Step 1: Extract URL2 content from /home/list.txt
First, we need to grab the right content from list.txt. Let's cover two common scenarios:
Scenario 1: URL2 is the 2nd line in list.txt
Iflist.txthas one URL per line and the second line is your target:url_content=$(sed -n '2p' /home/list.txt)Scenario 2: URL2 is in a key-value pair (e.g.,
URL2=https://example.com)
Iflist.txtuses key-value lines, extract the value like this:url_content=$(grep '^URL2=' /home/list.txt | cut -d'=' -f2)
Step 2: Replace the specified line in /home/temp.txt
Let's say you want to replace line 10 in temp.txt (replace 10 with your actual line number):
Method 1: Using sed (simple and direct)
# Replace line 10 with the extracted URL content, create a backup of temp.txt first sed -i.bak "10c $url_content" /home/temp.txt
Note: Use double quotes (") here so the shell expands the $url_content variable correctly.
Method 2: Using awk (safer for edge cases like special characters)
If your URL contains characters that might mess with sed (like & or backslashes), awk is more reliable:
# Replace line 10, save to a temp file then overwrite original awk -v target_line=10 -v new_content="$url_content" ' NR == target_line { print new_content; next } 1 # Print all other lines as-is ' /home/temp.txt > /home/temp.txt.tmp && mv /home/temp.txt.tmp /home/temp.txt
After running either method, check temp.txt to make sure the replacement worked as expected. The .bak file (from sed -i.bak) or the temp file (from awk) will keep your original content just in case.
内容的提问来源于stack exchange,提问作者tioma

