greping word after new line character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting greping word after new line character
# 8  
Old 06-18-2011
I believe you'll need sed or awk to help. Given your input, this sed
Code:
sed -n -r '/\\$/ { s/\\$//; H; b; }; x; G; s/\n//g; s/,[ \t]*/,/g; p; s/.//g; h'<input-file

will concatenate all lines that have a terminating backslant (\) and close spaces that follow commas. Output would be all lines and the alias lines would be like this:

Code:
User_Alias      ADMIN1 = horacel,matthes,caseyl,alexl2,jackl,johnnyzh,maheshm,jihuih,davidw,christh,williaml,jasminez

Lines that don't have a final backslant will have any blanks following commas deleted, but I think that is an ok side effect here.

If you are looking for one set of users, then you can add a grep, and maybe a sed to delete the leading constant part, and put each user name on a new line.

Code:
 
sed -n -r '/\\$/ { s/\\$//; H; b; }; x; G; s/\n//g; s/,[ \t]*/,/g; p; s/.//g; h' <input-file | grep ADMIN1|sed 's/^.*= *//; s/,/\n/g'


Last edited by agama; 06-22-2011 at 12:46 AM.. Reason: learn something new every day; my example was so wrong it was painful.
# 9  
Old 06-20-2011
Not yet get the desired output. The input files is having userid in diffrerent line as below:

Code:
User_Alias      ADMIN1 = horacel,matthes,caseyl,alexl2,\
                         jackl,johnnyzh,maheshm,jihuih,davidw,\
                         christh,williaml,jasminez

so while greping ADMIN1 we are getting the first lines only as part of ADMIN1 group as below

Code:
horacel,matthes,caseyl,alexl2,\

but we need all the user id as below

Code:
horacel,matthes,caseyl,alexl2, jackl,johnnyzh,maheshm,jihuih,davidw,christh,williaml,jasminez


Last edited by pludi; 06-20-2011 at 08:58 AM..
# 10  
Old 06-20-2011
Code:
% ADMIN=ADMIN1; perl -lne '$s .= $_;  
  if (!/\\$/) {
        $s =~ s/ |\\//g;
        $s =~ /'$ADMIN'/ && $s =~ s/^.*'$ADMIN'=// && print $s;
        $s=""}' testfile
horacel,matthes,caseyl,alexl2,jackl,johnnyzh,maheshm,jihuih,davidw,christh,williaml,jasminez


Last edited by yazu; 06-20-2011 at 09:18 AM..
# 11  
Old 06-20-2011
See, was that so hard? Give us enough information, and we'll be able to help you. Ask ambiguous questions, and you'll get answers that aren't helpful at all.

Save this as a file, and invoke it as perl script.pl -group=<Group you're looking for> <sudoers file>
Code:
#!/usr/bin/perl -Ws

use strict;
use warnings;

local $/;
our $group;

my $file = <>;

if ( $file =~ /User_Alias\s+$group\s+=\s+(.+?)[^\\]$/ms ) {
    my $users = $1;
    $users =~ s/[\n \\]//g;
    print $users, "\n";
}

Could be shortened to a single line, but that would be fugly and untypeable.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add a character after the first word/space on each line?

Hi:) I have a large file ( couple thousand lines ) and I'm trying to add a character after the first word/space on each line. eg: First line of text Second line of text Third line of text Fourth line of text Fifth line of text I'd like to accomplish: First - line of text Second... (8 Replies)
Discussion started by: martinsmith
8 Replies

2. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

3. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

4. Shell Programming and Scripting

Greping entire XML which has special character

I have an XML with has special character Â. I wrote a Grep command to find out the special character grep -i  Filename | grep ShipAddress2 I need the help to know how to find out special character such as  and get the whole XML listed assuming there are more xml data of similar sort for... (3 Replies)
Discussion started by: murali1687
3 Replies

5. UNIX for Advanced & Expert Users

Count specific word or character per line

Hi, I need help regarding counting specific word or character per line and validate it against a specific number i.e 10. And if number of character equals the specific number then that line will be part of the output. Specific number = 6 Specific word or char = || Sample data:... (1 Reply)
Discussion started by: janzper
1 Replies

6. Shell Programming and Scripting

Need Help with greping two line from a file Pls help ASAP

Hi all - I''m in a little bit of jam - If you can please help I have a file that has the following content ( please see below) I need to read the file and then get this result in this format ------------- To put out in this format name: sophis Total: 22328 name: tca ... (2 Replies)
Discussion started by: mnassiri
2 Replies

7. UNIX for Dummies Questions & Answers

Greping next line after grep match

Hi All I have a file with tha same line multiple times and its easy to grep out those lines using grep "pattern" filealthough I need to know exactly what the next line after those lines are Can anyone please shed some light on this on how i can simultaneously grep the pattern and the next line... (9 Replies)
Discussion started by: pawannoel
9 Replies

8. Shell Programming and Scripting

Command to parse a word character by character

Hi All, Can anyone help me please, I have a word like below. 6,76 I want to read this word and check if it has "," (comma) and if yes then i want to replace it with "." (dot). That means i want to be changed to 6.76 If the word doesnot contain "," then its should not be changed. Eg. ... (6 Replies)
Discussion started by: girish.raos
6 Replies

9. Shell Programming and Scripting

Print word 1 in line 1 and word 2 in line 2 if it matches a pattern

i have a file in this pattern MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH1 word1 IMAGE word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1 word2 word3 word4 MATCH2 word1... (7 Replies)
Discussion started by: bangaram
7 Replies

10. Shell Programming and Scripting

Looking for a single line to count how many times one character occurs in a word...

I've been looking on the internet, and haven't found anything simple enough to use in my code. All I want to do is count how many times "-" occurs in a string of characters (as a package name). It seems it should be very simple, and shouldn't require more than one line to accomplish. And this is... (2 Replies)
Discussion started by: Shingoshi
2 Replies
Login or Register to Ask a Question