Thursday, February 17, 2022

[SOLVED] Sed Command to delete specific items in a file

Issue

Probably similar questions have been asked before, but I cant find solution to this one:

The file in which I need to delete strings is :

#includedir /etc/sudoers.d
%wheel  ALL=(ALL)       NOPASSWD: ALL
pam_ansible ALL=(ALL) NOPASSWD: ALL
awx ALL=(ALL) NOPASSWD: ALL
***f10222 ALL=(ALL) NOPASSWD: ALL
mn2345zp ALL=(ALL) NOPASSWD: ALL
ab1235xy ALL=(ALL) NOPASSWD: ALL***

Note: The *** are to indicate the part I want to delete...the *** do not really exist in the file.

I just want to delete these kind of strings marked in bold (or between ***) on multiple files, considering this file as one of them.

What could the sed possibly be for these kind of strings? to delete it and have only the remaining content:

#includedir /etc/sudoers.d
%wheel  ALL=(ALL)       NOPASSWD: ALL
pam_ansible ALL=(ALL) NOPASSWD: ALL
awx ALL=(ALL) NOPASSWD: ALL

I am a newbie with these commands, My try on it: On my script I tried adding

sed '/[a-z0-9]/d' $user

where user contains list of the items between the *** but it only shows

sed: can't read : No such file or directory


Solution

In case you are ok with awk, could you please try following.

awk '$1 ~ /[a-zA-Z]/ && $1 !~ /[0-9]/' Input_file > temp && mv temp Input_file


Answered By - RavinderSingh13
Answer Checked By - Mary Flores (WPSolving Volunteer)