PERL Text Munging


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL Text Munging
# 1  
Old 12-17-2008
PERL Text Munging

I have a file like this:

Code:
host1,neighbor1
host3,neighbor4
host2,neighbor1
host2,neighbor3
host1,neighbor3
host3,neighbor1
host1,neighbor2
host3,neighbor2

I need some PERL magic that will generate output like this:
Code:
host1,neighbor1,neighbor3,neighbor2
host2,neighbor1,neighbor3
host3,neighbor4,neighbor1,neighbor2

What's happening here is the script looks at the first csv value and get's unique host IDs throughout the whole file, then loops through to get all of the neighbors associated with that Host ID and outputs a line in the form of:
host,neighborX,neighborX,neighborX,...
So there's only one line per actual host and all of the neighbors of that host follow it. The order of those neighbors after the host doesn't matter as long as there is only one instance of each neighbor.

Here is it in quick-n-dirty shell. I have more to do with the data afterward which is why I need it in PERL but it's been kicking my butt thus far.

Code:
bash-2.05a$ cat inputfile 
host1,neighbor1
host3,neighbor4
host2,neighbor1
host2,neighbor3
host1,neighbor3
host3,neighbor1
host1,neighbor2
host3,neighbor2
bash-2.05a$ cat itinshell.sh 
#!/bin/sh
for k in `cat inputfile | awk -F, '{print $1}' | sort -u`
do
  /usr/ucb/echo -n $k
  for i in `grep $k inputfile | awk -F, '{print $2}'`
  do
    /usr/ucb/echo -n ",$i"
  done
  echo
done
bash-2.05a$ ./itinshell.sh 
host1,neighbor1,neighbor3,neighbor2
host2,neighbor1,neighbor3
host3,neighbor4,neighbor1,neighbor2

Thanks,
Stateful
# 2  
Old 12-17-2008
Code:
$ cat file
host1,neighbor1
host3,neighbor4
host2,neighbor1
host2,neighbor3
host1,neighbor3
host3,neighbor1
host1,neighbor2
host3,neighbor2
$ perl -F, -ane'
  chomp $F[1];$_{$F[0]}.=",".$F[1];
  print map "$_$_{$_}\n", sort keys %_ if eof
  ' file
host1,neighbor1,neighbor3,neighbor2
host2,neighbor1,neighbor3
host3,neighbor4,neighbor1,neighbor2

# 3  
Old 12-17-2008
Code:
#! usr/bin/perl
open FH,"<a.txt";
while(<FH>){
	my @arr=split(",",$_);
	map {tr/\n//d} @arr;
	$hash{$arr[0]}=$hash{$arr[0]}.",".$arr[1];
}
for $key (keys %hash){
	print $key,$hash{$key},"\n";
}

# 4  
Old 12-17-2008
Thanks

Thanks for the help, guys. Good stuff.

Stateful
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl text from web

perl -MLWP::Simple -le '$s=shift;$c=get("http://genetics.emory.edu/egl/tests/view.php?testid=4125/$s/");$c=~/meta content=(.*?)name=\"Genes\"/msg; print length($1),"\t$1"' "Test Description" >output.txt I am having trouble with this code: Can it be modified for the desired output? I attached... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

TEXT to CSV using Perl

Hi Folks Need some help with this and my Perl isnt the hottest I also have text::csv installed on my perl install The large text with a few million entries is in a format below example text file Fig Leafs Cake No: 0000001 Author: King s. Record No: 995-34343-232-232... (5 Replies)
Discussion started by: messiah1
5 Replies

3. Shell Programming and Scripting

Text allignment using PERL

Hi Friends, For daily reports i make perl script like below. @dirlist = `cat out.txt |cut -d "|" -f1 >create.txt`; @dirlist1 = `cat out.txt|wc -l *e* >create2.txt`; open FILE, ">OUTPUT.txt"; @command = `cat out.txt |cut -d "|" -f1`; print FILE map{$_-2 ."\n"}@command; @dirlist2 =... (1 Reply)
Discussion started by: adaleru
1 Replies

4. Shell Programming and Scripting

Perl Text Manipulation

I'm in need of help for a project that I'm working on. I believe Perl would be the best way of handling the string manipulation, however, I've barely used perl, and I'm used to BASH scripting. Another note is, this project is in a Windows environment, so I can use Perl, but I do not have shell... (1 Reply)
Discussion started by: drewrockshard
1 Replies

5. Shell Programming and Scripting

Perl Text manipulation

Hello All, I have been working on a great script to remotely gather server info and store it in a .txt that can be imported to .xls I have been reading the hostnames that are in the /.shh/known_hosts file so I don't have to mess with passing a password - via ssh (not easy to do , by the... (1 Reply)
Discussion started by: dfezz1
1 Replies

6. Shell Programming and Scripting

Perl - Inserting text

Hey, I have 10 lines of text ... And I would like to Insert prefix for each line with static text. perl -pi -e 's/()/$1 TEST$./' data.txt Each line will have different static prefix, Code above works perfectly for 1st line ... I'm just not sure how I can run same command for 2nd line 3rd... (4 Replies)
Discussion started by: NDxiak
4 Replies

7. Shell Programming and Scripting

perl script - data munging

Hi guys I'm trying to print the keys and values of data by using hashes and so on I was able to retrieve the key but not values. I'm getting result like this wrong -output chr1, 1 HASH(0x800d80)-> {arrayid} chr2, 1 HASH(0x800c9c)-> {arrayid} chr3, 1 HASH(0x80177c)-> {arrayid} chr4, 1 ... (2 Replies)
Discussion started by: nogu0001
2 Replies

8. UNIX for Advanced & Expert Users

Need help take out few text in perl

I have perl program and I know that while sending an e-mail the following code returns "Remote (/opt/seasoft/db/nervecenter.nms00tst1):" in an email, I need to take out the entire "opt/seasoft/db/nervecenter" and leave with "ms00tst1" only. How do I do it: So the output would be "Remote... (3 Replies)
Discussion started by: amir07
3 Replies

9. Shell Programming and Scripting

Text formatting in Perl.

Hiho, I've got a variable $sth = `du -sh /home/$whoami`; where $whoami is actually logged user. I want to have only the size of this directory eg. 2,7G. I know its lame, but anyway.. how to do it? (2 Replies)
Discussion started by: fenox
2 Replies

10. Shell Programming and Scripting

Perl text file

I've got a csv file called "passlist.txt" which looks like: firstname1,lastname1,checkin1,numberofbags1,destination1 firstname2,lastname2,checkin2,numberofbags2,destination2 firstname3,lastname3,checkin3,numberofbags3,destination3 " " " " ... (5 Replies)
Discussion started by: Sn33R
5 Replies
Login or Register to Ask a Question