Perl line switching


 
Thread Tools Search this Thread
Top Forums Programming Perl line switching
# 1  
Old 12-02-2010
Perl line switching

Hi all

i have a text file with lots of data regarding the machine we have, each line is a seperate machine
for example here are a line ( all the data has been replaced with symbols for data protection)

Code:
@@@@@,£££££,%%%,^^^^,&&&&&&,*****,(((((,))))),------

!!!!!!!,£££££,%%%,^^^^,&&&&&&,*****,(((((,))))),------

as you can see each field id broken up with commas, i want a way to switch all the fields after the %%%, with another line

so i could type switch @ !!! and every after %%%, would switch around

thanks
Adam

Last edited by fpmurphy; 12-02-2010 at 10:00 AM.. Reason: fixed many typos
# 2  
Old 12-03-2010
For the given input, what should your output look like ?

tyler_durden
# 3  
Old 12-07-2010
sorry for the late reply

if i change the lines a bit to make it easier

Before


line one:

@@@@,@@@@@,-----,-----,------,------,------,-----,------

line two:


!!!!,!!!!!!,^^^,^^^,^^^,^^^,^^^,^^^,^^^^^

after the swtich


line one:

@@@@,@@@@@,^^^,^^^,^^^,^^^,^^^,^^^,^^^^^

line two:


!!!!,!!!!!!,-----,-----,------,------,------,-----,------


i am looking for a way that i could just go

swtich ! @and it would swrtich
# 4  
Old 12-07-2010
Your requirements are still unclear and have changed since your first post. Do you want to switch lines every time you find a match or just once?

Perhaps a simple example will help you clarify your requirements:
Code:
#!/usr/local/bin/perl

use strict;
use warnings;

my $filename = "example.txt";

my $found0 = 0;
my $found1 = 0;
my $line0;
my $line1;

open(FILE, "<$filename");

while (<FILE>) {
   my ($lookup, $junk) = split(/,/, $_, 2);
   if ($lookup == $ARGV[0] && $found0 == 0) {
       $found0 = $.;
       $line0 = $_;
   }
   if ($lookup == $ARGV[1] && $found1 == 0) {
       $found1 = $.;
       $line1 = $_;
   }
}
close(FILE);

print "Matched \'$ARGV[0]\' at line $found0\n";
print "Matched \'$ARGV[1]\' at line $found1\n\n";

open(FILE, "<$filename");
while (<FILE>) {
   my $line = $_;
   if ($. == $found0)  {
      print $line1;
   } elsif ($. == $found1) {
      print $line0;
   } else {
      print "$line";
   }
}

close(FILE);

here is the contents of example.txt
Code:
1111,@@@@@,^^^,^^^,^^^,^^^,^^^,^^^,^^^^^
2222,@@@@@,^^^,^^^,^^^,^^^,^^^,^^^,^^^^^
3333,@@@@@,^^^,^^^,^^^,^^^,^^^,^^^,^^^^^
4444,!!!!!,-----,-----,------,------,------,-----,------
5555,@@@@@,^^^,^^^,^^^,^^^,^^^,^^^,^^^^^

and here is the output when the above script is executed
Code:
$ ./example.pl 2222 4444
Matched '2222' at line 2
Matched '4444' at line 4

1111,@@@@@,^^^,^^^,^^^,^^^,^^^,^^^,^^^^^
4444,!!!!!,-----,-----,------,------,------,-----,------
3333,@@@@@,^^^,^^^,^^^,^^^,^^^,^^^,^^^^^
2222,@@@@@,^^^,^^^,^^^,^^^,^^^,^^^,^^^^^
5555,@@@@@,^^^,^^^,^^^,^^^,^^^,^^^,^^^^^
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. Shell Programming and Scripting

Perl to extract information from a file line by line

In the below perl code I am using tags within each line to extract certain information. The tags that are used are: STB >0.8 is STRAND BIAS otherwise GOOD FDP is the second number GO towards the end of the line is read into an array and the value returned is outputed, in the first line that... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Perl how to compare two pdf files line by line

Hi Experts, Would really appreciate if anyone can guide me how to compare two pdf files line by line and report the difference to another file. (3 Replies)
Discussion started by: prasanth_babu
3 Replies

4. Shell Programming and Scripting

Make Multile line is one line using window Perl

Hi All I have a .csv file which is showing data as ESP Client,ESP Engagement,Misc_Projects_120101,DEFAULT,HA,Unknown,No,Unknown,201704,4.1,Unknown,AAA,Collected-Done,"she,joy.",200111,Unknown,Full Time,,Delivery_DONE AMO,Approved,2012-12-03,2012-12-06,2012-12-06,"Occupied Hours ... (7 Replies)
Discussion started by: adisky123
7 Replies

5. Shell Programming and Scripting

perl: comparision of field line by line in two files

Hi everybody, First I apologize if my question seems demasiad you silly, but it really took 4 days struggling with this, I looked at books, forums ... And Also ask help to a friend that is software developer and he told me that it is a bad idea do it by perl... but this is my problem. I moved to... (8 Replies)
Discussion started by: Thelost
8 Replies

6. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

7. Programming

concatinate all lines from second line to end of line in perl

I have datafile like ~dta.yunm shhshsgggssssjsksjs sggsjshsjsjssss shshshhshshshs i wish to take only first line and all other lines in concatenated form in second line what should I do??? output like ~dta.yunm shhshsgggssssjsksjssggsjshsjsjssssshshshhshshshs please tell me how... (7 Replies)
Discussion started by: sujit_singh
7 Replies

8. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

9. Shell Programming and Scripting

How to use Perl to join multi-line into single line

Hello, Did anyone know how to write a perl script to merge the multi-line into a single line where each line with start at timestamp Input--> timestamp=2009-11-10-04.55.20.829347; a; b; c; timestamp=2009-11-10-04.55.20.829347; aa; bb; cc; (5 Replies)
Discussion started by: happyday
5 Replies

10. Shell Programming and Scripting

Perl script to search a line and copy it to another line

Hi I have a log file (say log.txt). I have to search for a line which has the string ( say ERROR) in the log file and copy 15 lines after this into another file (say error.txt). Can someone give me the code and this has to be in PERL Thanks in advance Ammu (3 Replies)
Discussion started by: ammu
3 Replies
Login or Register to Ask a Question