How to read all data after a specific string from a text file ?

Hi,

I have a file(input.txt) and trying to format as output.txt. See the attached file format.

Note: This is a windows file (DOS format) and the commands are also going to execute on windows.

Basically I am trying to capture all the data in between Local Group Memberships and Global Group Memberships. Then remove unnecessary strings like * from beginning of each string.

Thanks!!!

Hello Manoj2014,

Welcome to forum, following may help you in same.
EDIT: Adding code to remove garbage chars thanks to RudiC for reminding same.

tr -d '\r' < Input_file  OR
awk '{gsub(/\r/,X,$0);print}' Input_file > Input_file1
 
Then can run then following.
awk '($0 ~ /Local Group Memberships /){A=1} /Global Group memberships/{A=0} A{sub(/Local Group Memberships +/,X,$0);gsub(/ +/,X,$0);gsub(/^\*/,Z,$0);gsub(/\*/,"\n",$0);print $0}' Input_file1

Output will be as follows.

Administrators
BackupOperators
Guests
IIS_IUSRS
PowerUsers
RemoteDesktopUsers
Users

EDIT2: Adding once more solution for same.

awk '/Local Group Memberships/ {A=1}
     /Global Group memberships/ {A=0}
     A{
        match($0,/\*.*/);
        B=substr($0,RSTART,RLENGTH);
        sub(/\*/,X,B);
        sub(/[[:space:]]\*/,"\n",B);
        print B
      }
    ' Input_file

Output will be as follows.

Administrators
Backup Operators
Guests
IIS_IUSRS
Power Users
Remote Desktop Users
Users

Thanks,
R. Singh

1 Like

Any attempts from your side?

---------- Post updated at 15:02 ---------- Previous update was at 15:01 ----------

Try

awk '/Global Group/ {exit} /Local Group/,EOF {for (i=2; i<=NF; i++) print $i}' FS="*" /tmp/input.txt 
Administrators       
Backup Operators     
Guests               
IIS_IUSRS            
Power Users          
Remote Desktop Users 
Users                

---------- Post updated at 15:05 ---------- Previous update was at 15:02 ----------

To remove the DOS <CR> chars, put a gsub(/\r/,""); in front of the for statement.

Hi,

One more AWK solution, which uses '*' as delimiter.

awk -F"*" '{for (i=2;i<=NF+1;i++){if ($i != "" && $i !~ /None/){gsub(/ *$/,"",$i); printf "%s\n", $i}}}' file
Administrators
Backup Operators
Guests
IIS_IUSRS
Power Users
Remote Desktop Users
Users

Hope this helps

Hi Ravinder,

Thanks for quick response and sorry for delay in reply but I was unable to reply to this thread and some error occurred everytime I tried to reply.

Please find output(see attachment)

Findings:Backup Operators and Administrators are in same line. Also space is removed from Backup Operators.

Your second approach worked. Thanks a lot

awk '{gsub(/\r/,X,$0);print}' Input_file > Input_file
awk '/Local Group Memberships/ {A=1}      /Global Group memberships/ {A=0}      A{         match($0,/\*.*/);         B=substr($0,RSTART,RLENGTH);         sub(/\*/,X,B);         sub(/[[:space:]]\*/,"\n",B);         print B       }     ' Input_file

sed -e 's/ *$//g' Input_file > Input_file_new

I have added above sed command to remove all leading space character from the end of each line.

would you mind helping me in understanding above 2 commands.

Hi RudiC/binary1,

Thanks for your time and assistance. Unfortunately command failed to execute. see attachment. Any way I tried Ravinder approach and it works fine.

That obviously is a system error message, not issued from the awk scripts. Does d:\input_new.txt exist?

yes the file exist in the path and working fine when tried with Ravinder's command.

@Ravinder,

Your commands were worked fine on windows server If it is installed with any shell application(the machine against which I tested earlier have a shell application installed and thus awk & sed commands were recognized).

But we may have all the machines with that application installed and awk is failed to execute as OS unable to understand this command.

Below is the script working fine w/ machines installed with shell application.

net users Admin  > c:\groupList.txt

awk '{gsub(/\r/,X,$0);print}' c:\groupList.txt > c:\groupList1.txt

awk '/Local Group Memberships/ {A=1}/Global Group memberships/ {A=0} A{ match($0,/\*.*/);B=substr($0,RSTART,RLENGTH);sub(/\*/,X,B);sub(/[[:space:]]\*/,"\n",B);      print B      }' c:\groupList1.txt > c:\groupListTemp.txt

sed -e 's/ *$//g' c:\groupListTemp.txt > c:\groupListTemp1.txt

for /f "usebackq delims=" %%X in ("c:\groupListTemp1.txt") do (NET LOCALGROUP "%%X" Admin /delete)

Would you mind to assist in changing above awk and sed command to windows equivalent commands.

Hello Manoj,

If you want to avoid all the ugliness of the full path to awk, you need to update your PATH variable to include the path to the directory where awk is located, then you can just type awk to run your programs.

Go to Control Panel->System->Advanced and set your PATH environment variable to include "C:\Program Files (x86)\GnuWin32" at the end (separated by a semi-colon) from previous entry. You can download and run the setup file. This should install your AWK in "C:\Program Files (x86)\GnuWin32" . You can run the awk or gawk command from the bin folder or add the folder C:\Program Files (x86)\GnuWin32\bin to your PATH.

NOTE: I have attached the screen shot for same.

Thanks,
R. Singh

Hi Rajesh,

We can't install any 3rd party application in those servers due to organization standard and security policy. So for few servers awk and sed command will not work. We need to find windows equivalence command may be commands that support batch programming syntax.

I find a command like findstr equivalent to awk in shell but it not getting exact syntax to capture all data in between specified strings(Local Group Memberships and Global Group Memberships)

In the previous reply there is a typo. actually I meant to say we may not but mentioned we may.

"We may not have all the machines with that application installed and awk is failed to execute as OS unable to understand this command. "