perl, splitting out specific parts of the string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl, splitting out specific parts of the string
# 1  
Old 11-07-2011
perl, splitting out specific parts of the string

Hi there,

I have an output from a command like this

Code:
 
# ypcat -k netgroup.byuser| grep steven
steven.* users_main,users_sysadmin,users_global,users_backup_team

and wanted to pull the 'users' netgroups returned into a perl array, that will look like this

Code:
 
users_main
users_sysadmin
users_global
users_backup_team

I would pretty happy with how to do it if everything in the string was a users_* split by a comma, but because I have the 'steven.*' with a space after it it falls over a bit,

does anyone know how I can only include the bits of the string that start with 'users_' into my array, excluding everything else? so effectively only buildng an array from the red bits below

Code:
 
# ypcat -k netgroup.byuser| grep steven
steven.* users_main,users_sysadmin,users_global,users_backup_team

any help would be greatly appreciated

Last edited by rethink; 11-07-2011 at 09:52 AM..
# 2  
Old 11-07-2011
Something like this:
Code:
 echo 'steven.* users_main,users_sysadmin,users_global,users_backup_team' | \
perl -ne 'print join "\n", grep { /^users/ } split /[ ,]/;'

To get only the array:
Code:
my @array = grep { /^users/ } split /[ ,]/, $line;

# 3  
Old 11-07-2011
Thanks for that Pludi,

Code:
#!/bin/perl
use strict;
my $var = `ypcat -k netgroup.byuser| grep steven`
my @array = grep { /^users/ } split /[ ,]/, $var;

foreach (@array) {
  print "$_\n";
}

returns

# ./test.pl
users_main
users_sysadmin
users_global
users_backup_team
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add specific string to last field of each line in perl based on value

I am trying to add a condition to the below perl that will capture the GTtag and place a specific string in the last field of each line. The problem is that the GT value used is not right after the tag rather it is a few fields away. The values should always be 0/1 or 1/2 and are in bold in the... (12 Replies)
Discussion started by: cmccabe
12 Replies

2. Shell Programming and Scripting

Delete specific parts in a .txt file

Hi all, I desperately need a small script which deletes everything in a particular .txt file when "Abs = {" appears till "},", and also when "B-1 = {" appears till "}," I would like all the text in between of those instances to be deleted, however, other text to be unedited (kept as it is).... (12 Replies)
Discussion started by: c_lady
12 Replies

3. Shell Programming and Scripting

AWK splitting a string of equal parts of 500 chars

Hi , can someone help me how to make an AWK code for splitting a string of equal parts of 500 chars in while loop? Thank you! (4 Replies)
Discussion started by: sanantonio7777
4 Replies

4. UNIX for Dummies Questions & Answers

Help with copying specific parts of a file structure

Hello. I need help with copying part of a file structure to another directory while still keeping the structure. For example look below: ../folder1/sub1/txt.txt ../folder1/sub2/pic.png ../folder2/sub1/pic.png ../folder2/sub2/txt.txt So in this I would like to copy only the directories and... (3 Replies)
Discussion started by: the
3 Replies

5. Shell Programming and Scripting

Truncate Specific Parts of a String

How do you truncate specific parts of a string. Example: 1 This is the string Goal: This is the string As you can see I'm trying to simply remove the first two characters of the string the number one and the space between the one and the word "this." Your help is appreciated. ... (8 Replies)
Discussion started by: royarellano
8 Replies

6. Shell Programming and Scripting

How can i break a text file into parts that occur between a specific pattern

How can i break a text file into parts that occur between a specific pattern? I have text file having various xml many tags like which starts with the tag "<?xml version="1.0" encoding="utf-8"?>" . I have to break the whole file into several xmls by looking for the above pattern. All the... (9 Replies)
Discussion started by: abhinav192
9 Replies

7. Shell Programming and Scripting

Splitting a file into unequal parts

How do I split a file into many parts but with different amounts of lines per part? I looked at the split command but that only splits evenly. I'd like a range specified to determine how many lines each output file should have. For example, if the input file has 1000 lines and the range is... (1 Reply)
Discussion started by: revax
1 Replies

8. Shell Programming and Scripting

Perl - Need help on splitting string

Let's say I have a very long string with no spaces but just words stored in $very_long_string. $very_long_string = "aaaaaaaaaaabbbbbbbbbbbccccccccccccdddddddddddd"; I can do this to split the string into 1 character each and store them in an array: @myArray = split(//, $very_long_string); ... (3 Replies)
Discussion started by: teiji
3 Replies

9. Shell Programming and Scripting

Splitting huge XML Files into fixsized wellformed parts

Hi, I need to split xml-files with sizes greater than 2 gb into smaler chunks. As I dont want to end up with billions of files, I want those splitted files to have configurable sizes like 250 MB. Each file should be well formed having an exact copy of the header (and footer as the closing of the... (0 Replies)
Discussion started by: Malapha
0 Replies

10. Shell Programming and Scripting

Removing parts of a specific field

All, I have a field in a comma seperated file with hundreds of lines and about 20 columns and I wish to remove all numbers after the decimal point in field 4 on each line and output the rest to another file or write it back to itself. File is like this 20070126, 123.0, GBP, 1234.5678,... (9 Replies)
Discussion started by: kieranh
9 Replies
Login or Register to Ask a Question