2023年8月21日 星期一

What I Learned Today (08/21/2023): grep commands

 

🧐 Searching Within a Directory: A Handy Guide! 📂

Welcome to this quick guide! Today, we'll explore some powerful command-line tools to make your life easier. Whether you're a programmer or just someone who likes to work efficiently, these commands are sure to help you out! 🚀

🔍 Search for Files and Matched Strings

Want to find specific text within your files? Use the grep command! Here's how you can search for both files and the matched text:

Show both file and matched string:

grep -R text /home/users/workspace grep -lir text /home/users/workspace

 Show only the file:

grep -Rl text /home/users/workspace

🎵 Append a String and More! 🧩

You can do more than just search! Here are some examples of appending and manipulating strings:

  • Grep and append string (perfect for downloading! 🎉):

  • more musiclist.txt | grep ecx | sed -e 's/^/wget /'

  • Append a string to every line from the grep result (great for zipping 📦 and unzipping 🎁):
ls -a | grep "Vol_" | perl -ne 'print "zip $_"'
ls -a | grep "rar" | perl -ne 'print "unrar x $_"'

✏️ Grep and Replace String: Make Quick Changes! 🔄

Need to replace a specific string across multiple files? Here's a super handy command for that:

  • Grep and replace a string:

 grep -rl matchstring somedir/ | xargs sed -i 's/string1/string2/g'

grep -rl 'windows' ./ | xargs sed -i 's/windows/linux/g'

🎉 Wrapping Up

With these commands at your fingertips, you're all set to search, manipulate, and conquer your files! Whether you're a newbie or a seasoned pro, these tips can make your daily tasks a breeze. Happy coding! 🎈

If you have any questions or need further assistance, feel free to leave a comment below. Happy searching! 🔎👩‍💻👨‍💻


沒有留言: