البرمجة

تعديل السطور التالية بعد البحث عن نص معين باستخدام sed

To achieve this with sed, you can combine the search for the specific string with a command to remove the first character of the following ten lines. Here’s a sed command that does this:

bash
sed -i '/StringToSearchFor/ {n;n;n;n;n;n;n;n;n;n;s/^./ /g}' file

Explanation of the command:

  • -i option: This tells sed to edit the file in-place.
  • /StringToSearchFor/: This is the pattern you’re searching for.
  • {n;...;n;s/^./ /g}: This is a block of commands that will be executed if the pattern is found.
    • n;: This moves to the next line. Repeated ten times to move to the tenth line after the pattern.
    • s/^./ /g: This substitutes the first character (^.) of each of the next ten lines with a space ( ).

So, when you run this command, it will search for StringToSearchFor and if found, it will remove the first character of the following ten lines.

المزيد من المعلومات

بالتأكيد! يُمكنك استخدام أمر sed في Linux للبحث عن نص محدد ثم حذف الحرف الأول من السطور التالية. على سبيل المثال، لنفترض أن لديك ملف يسمى file.txt يحتوي على الروابط التي تريد تعليقها. إذا كانت الروابط معلقة بتعليقات بداية السطر بـ #، وتريد إلغاء تعليق الروابط التي تلي النص “http://example.com” وحذف الحرف الأول من السطور التالية، يمكنك استخدام هذا الأمر:

bash
sed -i '/http:\/\/example.com/!b;n;s/^./ /' file.txt
  • -i: يقوم بتعديل الملف مباشرة.
  • /http:\/\/example.com/: يبحث عن النص “http://example.com“.
  • !b: إذا لم يتم العثور على النص، يتم الانتقال إلى السطر التالي.
  • n: ينتقل إلى السطر التالي.
  • s/^./ /: يقوم بتغيير الحرف الأول من السطر إلى مسافة.

هذا الأمر سيجد النص المطابق ويقوم بحذف الحرف الأول من السطور التالية بعد النص المطابق.

زر الذهاب إلى الأعلى