How do I create an array from a file using every 3rd line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How do I create an array from a file using every 3rd line
# 8  
Old 08-03-2009
Code:
my %hash;
local $/="RUNNING";
while(<DATA>){
	if(/ceName: ([^ \n]*).*processName: ([^ \n]*).*Status: ([^ \n]*)/ms){
		$hash{$2}->{$1}=$3;
	}
}
print "processName        Status Node-1                   Status Node-2\n";
print "-----------        -------------------              -------------------\n";
foreach my $key (keys %hash){
	print $key,"  ";
	foreach my $k(keys %{$hash{$key}}){
		print $hash{$key}->{$k},"   ";
	}
	print "\n";
}
__DATA__
ceName: Node-1
processName: tzMgmt
Status: PROCESS_NOT_RUNNING
ceName: Node-2
processName: tzMgmt
Status: PROCESS_RUNNING
ceName: Node-1
processName: XDM
Status: PROCESS_NOT_RUNNING
ceName: Node-2
processName: XDM
Status: PROCESS_RUNNING

# 9  
Old 08-03-2009
Quote:
Originally Posted by edidataguy
Try this and see if you can modify it to your requirement:
Code:
sed 'N;N;N;N;N;s/\n/\t\t/gp' file

FYI: You put your data / input / output etc also between [code]
This will make it easy to read.

---------- Post updated at 11:33 PM ---------- Previous update was at 05:27 PM ----------

This works, but not the best way.
Lets see if any one else comes up with a better one.
Code:
sed  '/ceName:.*/d; s/^[^:]*: //' 3rdline.txt | sed 'N;N;N;s/\n/\t/g; s/\(^[^\t]*\t[^\t]*\t\)[^\t]*\t\([^\t]*\)$/\1\2/'

Thanks for your efforts, edidataguy. However, the result is the following:

Code:
sed  '/ceName:.*/d; s/^[^:]*: //' 3rdline.txt | sed 'N;N;N;s/\n/\t/g; s/\(^[^\t]*\t[^\t]*\t\)[^\t]*\t\([^\t]*\)$/\1\2/'
tzMgmttPROCESS_NOT_RUNNINGttzMgmttPROCESS_RUNNING
XDMtPROCESS_NOT_RUNNINGtXDMtPROCESS_RUNNING

Regards,

Bjoern

---------- Post updated at 01:32 PM ---------- Previous update was at 01:29 PM ----------

Quote:
Originally Posted by RadRod
Hi I just did a simalar thing and ended up using awk you might find this a lot simpler Eg:

Code:
awk 'ORS=NR%3?"~":"\n"' alltr.out > alltr.sql

Thanks for the reply, RadRod. However, this command resulted in a syntax error:

Code:
# awk 'ORS=NR%3?"~":"\n"' alltr.out
awk: syntax error near line 1
awk: bailing out near line 1

Regards,

Bjoern

---------- Post updated at 01:56 PM ---------- Previous update was at 01:32 PM ----------

Quote:
Originally Posted by summer_cherry
Code:
my %hash;
local $/="RUNNING";
while(<DATA>){
    if(/ceName: ([^ \n]*).*processName: ([^ \n]*).*Status: ([^ \n]*)/ms){
        $hash{$2}->{$1}=$3;
    }
}
print "processName        Status Node-1                   Status Node-2\n";
print "-----------        -------------------              -------------------\n";
foreach my $key (keys %hash){
    print $key,"  ";
    foreach my $k(keys %{$hash{$key}}){
        print $hash{$key}->{$k},"   ";
    }
    print "\n";
}

------- SNIP--------

Hi summer_cherry.

Thanks for your help. Which shell was this run on? My system is running on Solaris 10. I called the script with your code 'procheck'; here are my results:

Code:
# ./procheck
./procheck[3]: my:  not found
./procheck[3]: $/=RUNNING: is not an identifier


Regards,

Bjoern
# 10  
Old 08-04-2009
Quote:
# awk 'ORS=NR%3?"~":"\n"' alltr.out
awk: syntax error near line 1
awk: bailing out near line 1
Try to use gawk or nawk instead of awk.
# 11  
Old 08-04-2009
Quote:
Originally Posted by edidataguy
This is a little more refined:
Code:
sed  '/ceName:.*/d; s/^[^:]*: //' 3rdline.txt | sed 'N;N;N;s/\n/\t/g; s/\([^\t]*\t\)//3'

Code:
 
Thanks for your efforts, edidataguy. However, the result is the following:
tzMgmttPROCESS_NOT_RUNNINGttzMgmttPROCESS_RUNNINGXDMtPROCESS_NOT_RUNNINGtXDMtPROCESS_RUNNING


Sorry, could not come back to you early. Got tied up.
OK. Looks like there is as issue with your version of sed recognizing the "\t" (tab) and \n (new line). Try it this way, replace all \t with \\t and \n with \\n. See if it works.
# 12  
Old 08-04-2009
Quote:
Originally Posted by BRH
...
Hi summer_cherry.

Thanks for your help. Which shell was this run on? My system is running on Solaris 10. I called the script with your code 'procheck'; here are my results:

Code:
# ./procheck
./procheck[3]: my:  not found
./procheck[3]: $/=RUNNING: is not an identifier

...
Hi,

If I may chip in here, summer_cherry's script is written in Perl. So you will have to feed your "procheck" file to the perl interpreter.

First check if perl exists in your system. On my box:

Code:
$ 
$ which perl
/usr/bin/perl
$ 
$ perl -v

This is perl, v5.10.0 built for i486-linux-gnu-thread-multi

Copyright 1987-2007, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

$

And then run the "procheck" file thusly:

Code:
$ 
$ cat procheck
my %hash;
local $/="RUNNING";
while(<DATA>){
    if(/ceName: ([^ \n]*).*processName: ([^ \n]*).*Status: ([^ \n]*)/ms){
        $hash{$2}->{$1}=$3;
    }
}
print "processName        Status Node-1                   Status Node-2\n";
print "-----------        -------------------              -------------------\n";
foreach my $key (keys %hash){
    print $key,"  ";
    foreach my $k(keys %{$hash{$key}}){
        print $hash{$key}->{$k},"   ";
    }
    print "\n";
}
__DATA__
ceName: Node-1
processName: tzMgmt
Status: PROCESS_NOT_RUNNING
ceName: Node-2
processName: tzMgmt
Status: PROCESS_RUNNING
ceName: Node-1
processName: XDM
Status: PROCESS_NOT_RUNNING
ceName: Node-2
processName: XDM
Status: PROCESS_RUNNING
$ 
$ perl procheck
processName        Status Node-1                   Status Node-2
-----------        -------------------              -------------------
tzMgmt  PROCESS_NOT_RUNNING   PROCESS_RUNNING   
XDM  PROCESS_NOT_RUNNING   PROCESS_RUNNING   
$ 
$

Alternatively, if you add the perl "shebang" at the top of your file and if you have made it executable, then you will be able to run it successfully the way you invoked it.

Code:
$ 
$ head -3 procheck
#!/usr/bin/perl
my %hash;
local $/="RUNNING";
$ 
$ chmod 744 procheck
$ 
$ ./procheck
processName        Status Node-1                   Status Node-2
-----------        -------------------              -------------------
tzMgmt  PROCESS_NOT_RUNNING   PROCESS_RUNNING   
XDM  PROCESS_NOT_RUNNING   PROCESS_RUNNING   
$ 
$

HTH,
tyler_durden
# 13  
Old 08-05-2009
Quote:
Originally Posted by edidataguy
Code:
 
Thanks for your efforts, edidataguy. However, the result is the following:
tzMgmttPROCESS_NOT_RUNNINGttzMgmttPROCESS_RUNNINGXDMtPROCESS_NOT_RUNNINGtXDMtPROCESS_RUNNING


Sorry, could not come back to you early. Got tied up.
OK. Looks like there is as issue with your version of sed recognizing the "\t" (tab) and \n (new line). Try it this way, replace all \t with \\t and \n with \\n. See if it works.

Hi editaguy. I don't think the problem's with sed:

Code:
 
# sed  '/ceName:.*/d; s/^[^:]*: //' 3rdline.txt | sed 'N;N;N;s/\n/\\t/g; s/\([^\\t]*\\t\)//3'
tzMgmt\tPROCESS_NOT_RUNNING\ttzMgmtPROCESS_RUNNING
XDM\tPROCESS_NOT_RUNNING\tPROCESS_RUNNING

There are additional '\t's now. I think I may have to resort to arrays.


Bjoern
# 14  
Old 08-05-2009
Thanks tyler, it works now!

Code:
# ./procheck
processName        Status Node-1                   Status Node-2
-----------        -------------------              -------------------
tzMgmt  PROCESS_NOT_RUNNING   PROCESS_RUNNING
XDM  PROCESS_NOT_RUNNING   PROCESS_RUNNING

One question: I'm not familiar with perl, if the _DATA_ is an external file, how do I reference it in the script?

Thanks again,

Bjoern
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Create a bash array from a flat file of whitespaces only.

Hi guys and gals... MacBook Pro. OSX 10.13.2, default bash terminal. I have a flat file 1920 bytes in size of whitespaces only. I need to put every single whitespace character into a bash array cell. Below are two methods that work, but both are seriously ugly. The first one requires that I... (7 Replies)
Discussion started by: wisecracker
7 Replies

2. Shell Programming and Scripting

From 2 files create 3rd file with uncommon data

Hi All, I have two files. File1 and File2. Want to create another file with all the records of File1 those are not present in File2. Please guide. Thanks in advanced. Anupam (3 Replies)
Discussion started by: Anupam_Halder
3 Replies

3. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

4. Shell Programming and Scripting

Perl - use search keywords from array and search a file and print 3rd field when matched

Hi , I have been trying to write a perl script to do this job. But i am not able to achieve the desired result. Below is my code. my $current_value=12345; my @users=("bob","ben","tom","harry"); open DBLIST,"<","/var/tmp/DBinfo"; my @input = <DBLIST>; foreach (@users) { my... (11 Replies)
Discussion started by: chidori
11 Replies

5. Shell Programming and Scripting

Display 3rd line of a file using cut only

Hello, i want to use new line character in cut command i want to display 3rd line of a file using cut only( not by sed or head -tail command) can anyone suggest me ? Regards (12 Replies)
Discussion started by: Deepak Dutt
12 Replies

6. Solaris

Configure disk array in RAID5 and create file system

I'm new to forums, it's my first time posting. I have a sun v490 server. I just installed solaris 10.6, on the local drives. I'm being asked to do the following: For Oracle install I need “oracle” user that belong to “dba” and “oinstall” groups. File system /u01/app/oracle, 10GB (if... (6 Replies)
Discussion started by: Kjons76
6 Replies

7. Shell Programming and Scripting

How to extract 3rd line 4th column of a file

Hi, Shell script: I would need help on How to extract 3rd line 4th column of a file with single liner Thanks in advance. (4 Replies)
Discussion started by: krishnamurthig
4 Replies

8. Shell Programming and Scripting

Print starting 3rd line until end of the file.

Hi, I want to Print starting 3rd line until end of the file. Pls let me know the command. Thanks in advance. (1 Reply)
Discussion started by: smc3
1 Replies

9. Shell Programming and Scripting

create array holding characters from sring then echo array.

Hi, I wish to store $string1 in $string1array a character in each array element. Then i wish to echo the entire array to the screen so that it reads as the normal string again. I have been trying with the code below but does not work. Please help... To put string into array: ... (5 Replies)
Discussion started by: rorey_breaker
5 Replies

10. Shell Programming and Scripting

How to print 3rd to last line of file?

Hi, I have a ksh script I would like to modify. What I need it to do is look at an ever changing log file and print the 3rd to last line. Is there a command that will display this? I can not use line numbers because the file is always growing. Thanks for any help (2 Replies)
Discussion started by: NivekRaz
2 Replies
Login or Register to Ask a Question