Sponsored Content
Full Discussion: Perl line switching
Top Forums Programming Perl line switching Post 302478239 by fpmurphy on Tuesday 7th of December 2010 11:56:55 AM
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,@@@@@,^^^,^^^,^^^,^^^,^^^,^^^,^^^^^
$

 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
SYSTEMD-MACHINE-ID-SETUP(1)				     systemd-machine-id-setup				       SYSTEMD-MACHINE-ID-SETUP(1)

NAME
systemd-machine-id-setup - Initialize the machine ID in /etc/machine-id SYNOPSIS
systemd-machine-id-setup DESCRIPTION
systemd-machine-id-setup may be used by system installer tools to initialize the machine ID stored in /etc/machine-id at install time with a randomly generated ID. See machine-id(5) for more information about this file. This tool will execute no operation if /etc/machine-id is already initialized. If a valid D-Bus machine ID is already configured for the system, the D-Bus machine ID is copied and used to initialize the machine ID in /etc/machine-id. If run inside a KVM virtual machine and a UUID is passed via the -uuid option, this UUID is used to initialize the machine ID instead of a randomly generated one. The caller must ensure that the UUID passed is sufficiently unique and is different for every booted instanced of the VM. Similarly, if run inside a Linux container environment and a UUID is set for the container this is used to initialize the machine ID. For details see the documentation of the Container Interface[1]. OPTIONS
The following options are understood: -h, --help Prints a short help text and exits. --version Prints a short version string and exits. EXIT STATUS
On success, 0 is returned, a non-zero failure code otherwise. SEE ALSO
systemd(1), machine-id(5), dbus-uuidgen(1) NOTES
1. Container Interface http://www.freedesktop.org/wiki/Software/systemd/ContainerInterface systemd 208 SYSTEMD-MACHINE-ID-SETUP(1)
All times are GMT -4. The time now is 06:54 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy