DEV Community

Janak Shrestha
Janak Shrestha

Posted on

Text Processing in Linux: Deleting Lines with grep and Replacing Exact Words with sed

There is some data on Nautilus App Server 2 in Stratos DC. Data needs to be altered in some of the files. On Nautilus App Server 2, alter the /home/BSD.txt file as per details given below.

a. Delete all lines containing the word software and save the results in /home/BSD_DELETE.txt file. (Please be aware of case sensitivity)

b. Replace all occurrences of the word and (look for the exact match) with their and save the results in /home/BSD_REPLACE.txt file.

Note: Let's say you are asked to replace the word to with from. In that case, make sure not to alter any words containing the string itself, for example; up*to, contributo*r etc.


Solution

Step 1: Connect to App Server 2 (stapp02)

ssh steve@stapp02
# Password: Am3ric@
Enter fullscreen mode Exit fullscreen mode

Step 2: Switch to root (if needed)

sudo su -
# Password: Am3ric@
Enter fullscreen mode Exit fullscreen mode

Step 3: Verify the file exists

ls -la /home/BSD.txt
Enter fullscreen mode Exit fullscreen mode

Step 4: Create the processed files

a. Delete all lines containing "software"

grep -v "software" /home/BSD.txt > /home/BSD_DELETE.txt
Enter fullscreen mode Exit fullscreen mode

b. Replace all occurrences of "and" with "their"

sed -E 's/\band\b/their/g' /home/BSD.txt > /home/BSD_REPLACE.txt
Enter fullscreen mode Exit fullscreen mode

Note: \b ensures exact word match (word boundaries), preventing partial matches like "and" in "stand" or "sandwich".

Step 5: Verify the results

# Check files created
ls -la /home/BSD_DELETE.txt /home/BSD_REPLACE.txt

# Verify line counts
echo "Original lines: $(wc -l < /home/BSD.txt)"
echo "After deleting 'software' lines: $(wc -l < /home/BSD_DELETE.txt)"
echo "After replacing 'and' with 'their': $(wc -l < /home/BSD_REPLACE.txt)"

# Verify content (samples)
echo "=== BSD_DELETE.txt sample ==="
head -5 /home/BSD_DELETE.txt

echo "=== BSD_REPLACE.txt sample ==="
head -5 /home/BSD_REPLACE.txt
Enter fullscreen mode Exit fullscreen mode

One-Line Commands

From jump host:

echo 'Am3ric@' | ssh steve@stapp02 "sudo -S bash -c 'grep -v \"software\" /home/BSD.txt > /home/BSD_DELETE.txt && sed -E \"s/\\band\\b/their/g\" /home/BSD.txt > /home/BSD_REPLACE.txt && ls -la /home/BSD_DELETE.txt /home/BSD_REPLACE.txt'"
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Interactive Commands

# Connect to stapp02
ssh steve@stapp02
# Password: Am3ric@

# Switch to root
sudo su -
# Password: Am3ric@

# Step 1: Check original file
echo "=== Original file ==="
wc -l /home/BSD.txt
head -20 /home/BSD.txt

# Step 2: Delete lines containing "software"
echo "=== Creating BSD_DELETE.txt ==="
grep -v "software" /home/BSD.txt > /home/BSD_DELETE.txt

# Step 3: Replace "and" with "their"
echo "=== Creating BSD_REPLACE.txt ==="
sed -E 's/\band\b/their/g' /home/BSD.txt > /home/BSD_REPLACE.txt

# Step 4: Verify
echo "=== Verification ==="
echo "Original: $(wc -l < /home/BSD.txt) lines"
echo "Delete: $(wc -l < /home/BSD_DELETE.txt) lines"
echo "Replace: $(wc -l < /home/BSD_REPLACE.txt) lines"

# Step 5: Check for "software" in BSD_DELETE.txt (should be none)
echo "=== Checking BSD_DELETE.txt for 'software' ==="
grep "software" /home/BSD_DELETE.txt | wc -l
echo "✓ No 'software' lines found"

# Step 6: Check "and" vs "their" in BSD_REPLACE.txt
echo "=== Checking replacements ==="
echo "Original 'and' count: $(grep -o '\band\b' /home/BSD.txt | wc -l)"
echo "New 'their' count: $(grep -o '\btheir\b' /home/BSD_REPLACE.txt | wc -l)"

# Exit
exit
exit
Enter fullscreen mode Exit fullscreen mode

Expected Output

[root@stapp02 ~]# grep -v "software" /home/BSD.txt > /home/BSD_DELETE.txt
[root@stapp02 ~]# sed -E 's/\band\b/their/g' /home/BSD.txt > /home/BSD_REPLACE.txt

[root@stapp02 ~]# ls -la /home/BSD_DELETE.txt /home/BSD_REPLACE.txt
-rw-r--r-- 1 root root 4567 Jul 19 10:00 /home/BSD_DELETE.txt
-rw-r--r-- 1 root root 4890 Jul 19 10:00 /home/BSD_REPLACE.txt

[root@stapp02 ~]# echo "Original lines: $(wc -l < /home/BSD.txt)"
Original lines: 150

[root@stapp02 ~]# echo "Lines after delete: $(wc -l < /home/BSD_DELETE.txt)"
Lines after delete: 145

[root@stapp02 ~]# echo "Replacements: $(grep -o '\btheir\b' /home/BSD_REPLACE.txt | wc -l)"
Replacements: 8
Enter fullscreen mode Exit fullscreen mode

Command Explanation

Part A: Delete lines with "software"

grep -v "software" /home/BSD.txt > /home/BSD_DELETE.txt
Enter fullscreen mode Exit fullscreen mode
  • grep -v: Invert match (exclude lines with pattern)
  • "software": Exact case-sensitive match
  • > /home/BSD_DELETE.txt: Redirect output to file

Part B: Replace "and" with "their"

sed -E 's/\band\b/their/g' /home/BSD.txt > /home/BSD_REPLACE.txt
Enter fullscreen mode Exit fullscreen mode
  • -E: Extended regular expressions
  • s/pattern/replacement/g: Substitute globally
  • \b: Word boundary (ensures exact word match)
  • and: Pattern to match (exact word)
  • their: Replacement word
  • g: Replace all occurrences (not just first)

Word Boundary Explanation

\band\b  matches: "and" (standalone word)
\band\b  does NOT match: "stand", "sand", "andrew", "hand"
Enter fullscreen mode Exit fullscreen mode

Verification Commands

# 1. Check files exist
ls -la /home/BSD_DELETE.txt /home/BSD_REPLACE.txt

# 2. Verify no "software" lines in BSD_DELETE.txt
grep "software" /home/BSD_DELETE.txt | wc -l
# Should output: 0

# 3. Verify all "and" are replaced with "their"
grep -o '\band\b' /home/BSD_REPLACE.txt | wc -l
# Should output: 0

# 4. Count how many "their" were added
grep -o '\btheir\b' /home/BSD_REPLACE.txt | wc -l

# 5. Compare line counts
echo "Original: $(wc -l < /home/BSD.txt)"
echo "Deleted: $(wc -l < /home/BSD_DELETE.txt)"
echo "Replaced: $(wc -l < /home/BSD_REPLACE.txt)"

# 6. Check specific lines for proper replacement
grep -n "their" /home/BSD_REPLACE.txt | head -5

# 7. Ensure partial matches weren't affected
grep -E "stand|sand|hand|andrew" /home/BSD_REPLACE.txt | head -5
# These should still exist (not affected)
Enter fullscreen mode Exit fullscreen mode

Alternative Methods

Part A: Using sed to delete lines

sed '/software/d' /home/BSD.txt > /home/BSD_DELETE.txt
Enter fullscreen mode Exit fullscreen mode

Part A: Using awk

awk '!/software/' /home/BSD.txt > /home/BSD_DELETE.txt
Enter fullscreen mode Exit fullscreen mode

Part B: Using awk for replacement

awk '{gsub(/\band\b/,"their")}1' /home/BSD.txt > /home/BSD_REPLACE.txt
Enter fullscreen mode Exit fullscreen mode

Part B: Using sed without extended regex

sed 's/\<and\>/their/g' /home/BSD.txt > /home/BSD_REPLACE.txt
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

  1. File not found:
   find /home -name "BSD.txt" 2>/dev/null
Enter fullscreen mode Exit fullscreen mode
  1. Case sensitivity:
   # If case-insensitive needed (not required here)
   grep -iv "software" /home/BSD.txt
Enter fullscreen mode Exit fullscreen mode
  1. Word boundary not working:
   # Use \< and \> instead of \b
   sed 's/\<and\>/their/g' /home/BSD.txt > /home/BSD_REPLACE.txt
Enter fullscreen mode Exit fullscreen mode

Summary

  • BSD_DELETE.txt: All lines containing "software" removed
  • BSD_REPLACE.txt: All occurrences of "and" replaced with "their"
  • Word boundaries used: Partial matches not affected
  • Case sensitivity: "software" matches exactly
  • ✅ Files created in /home/

Both files have been successfully processed according to the specifications.

Top comments (0)