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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read all data after a specific string from a text file ?
# 1  
Old 12-12-2014
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!!!
# 2  
Old 12-12-2014
Hello Manoj2014,

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

Code:
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.
Code:
Administrators
BackupOperators
Guests
IIS_IUSRS
PowerUsers
RemoteDesktopUsers
Users

EDIT2: Adding once more solution for same.
Code:
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.
Code:
Administrators
Backup Operators
Guests
IIS_IUSRS
Power Users
Remote Desktop Users
Users

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-12-2014 at 12:05 PM.. Reason: Added code for removing garbage chars Thanks to RudiC + Added one more solution
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 12-12-2014
Any attempts from your side?

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

Try
Code:
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.
# 4  
Old 12-12-2014
Hi,

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

Code:
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
# 5  
Old 12-15-2014
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)
Image

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


Your second approach worked. Thanks a lot
Code:
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.
How to read all data after a specific string from a text file ?-outputjpg

Last edited by Monoj2014; 12-15-2014 at 07:16 AM.. Reason: Additional command required
# 6  
Old 12-15-2014
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.
# 7  
Old 12-15-2014
That obviously is a system error message, not issued from the awk scripts. Does d:\input_new.txt exist?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read data from tab delimited file after a specific position?

Hi Experts, I have a tab deliminated file as below myfile.txt Local Group Memberships *Administrators *Guests I need data in below format starting from 4th position. myfile1.txt Administrators Guests the above one is just an example and there could... (15 Replies)
Discussion started by: Litu1988
15 Replies

2. Windows & DOS: Issues & Discussions

Removing anything from text file except specific string

So, I have a text file that looks like this: 0,0: (168,168,176) #A8A8B0 srgb(168,168,176) 1,0: (168,168,176) #A8A8B0 srgb(168,168,176) 2,0: (166,166,174) #A6A6AE srgb(166,166,174) 3,0: (166,166,174) #A6A6AE srgb(166,166,174) 4,0: (168,168,176) #A8A8B0 srgb(168,168,176) 5,0:... (0 Replies)
Discussion started by: pasc
0 Replies

3. Shell Programming and Scripting

how read specific line in a file and write it in a new text file?

I have list of files in a directory 'dir'. Each file is of type HTML. I need to read each file and get the string which starts with 'http' and write them in a new text file. How can i do this shell scripting? file1.html <head> <url>http://www.google.com</url> </head> file2.html <head>... (6 Replies)
Discussion started by: vel4ever
6 Replies

4. Shell Programming and Scripting

read data from file from a specific duration

Hi, i have log file which keeps on updating almost ever minute but i have certain string in log which will only be displayed between specific time. so i need to search for that string and copy the details starting from that string and stop at certain string below that. example: log file... (2 Replies)
Discussion started by: gpk_newbie
2 Replies

5. Shell Programming and Scripting

Read Write byte range/chunk of data from specific location in file

I am new to Unix so will really appreciate if someone can guide me on this. What I want to do is: Step1: Read binary file - pick first 2 bytes, convert from hex to decimal. Read the next 3 bytes as well. 2 bytes will specify the number of bytes 'n' that I want to read and write... (1 Reply)
Discussion started by: Kbenipel
1 Replies

6. Shell Programming and Scripting

Reading data from a specific line in a text file

Hello, I have a problem which is giving me headache for days, can some please help. Please see code and text fiel below. Please see text in red for the problem I am facing # Program gets an input x from user while read line ; do echo... (4 Replies)
Discussion started by: jermaine4ever
4 Replies

7. Shell Programming and Scripting

Reading data from a specific line in a text file

hello, I have got the following problem that I am hoping someone can help with please. 1. I have got the following text file (below) , the columns data are 'Test Day', 'Board', 'Betting Number'. TEXT FILE ============================================ 1 3 02-01-27-28-29-30 0 1... (1 Reply)
Discussion started by: jermaine4ever
1 Replies

8. Shell Programming and Scripting

read space filled file and replace text at specific position

Hi I have a spaced filled file having records like below: What I want is to read line having RT3 at position 17-19 then go to position 2651 check the 18 characters (might be space filled till 18 characters). This position should have a... (6 Replies)
Discussion started by: COD
6 Replies

9. Shell Programming and Scripting

read specific text from a log file

Hi guys I need to retrieve the values in BOLD that I have mentioned in the below log file. I want to store those values in a variable, preferably the same name as the column name in the log file. if you paste the below mentioned log file in a notepad and remove the word wrap.. u will get a... (4 Replies)
Discussion started by: ragha81
4 Replies
Login or Register to Ask a Question