Sponsored Content
Top Forums Shell Programming and Scripting Removing carriage returns with sed Post 97068 by stevefox on Thursday 26th of January 2006 12:02:26 AM
Old 01-26-2006
Question

Thanks Ygor.
I have another question.

I have two text files below:

Code:
$ more list1.txt
TopGroup1 User1 Role1
TopGroup1 User1 Role2
TopGroup2 User1 Role1
TopGroup2 User2 Role3

$ more list2.txt
TopGroup1 SubGroup1 User1
TopGroup1 SubGroup2 User1
TopGroup2 SubGroup3 User1
TopGroup2 SubGroup3 User2

I want create a list below containing "TopGroup SubGroup User Role" by comparing the two files above. (I want the Users in the SubGroups to have the same Roles as the TopGroups)

TopGroup1 SubGroup1 User1 Role1
TopGroup1 SubGroup1 User1 Role2
TopGroup1 SubGroup2 User1 Role1
TopGroup1 SubGroup2 User1 Role2
TopGroup2 SubGroup3 User1 Role1
TopGroup2 SubGroup3 User2 Role3


I am able to get the above result by the K Shell script below but can this be done with less lines of code using awk?

Code:
$ more compare
#!/bin/ksh

while read group user role
do
   while read group2 subgroup user2
   do
        if [[ $group = $group2 && $user = $user2 ]] ; then

        echo $group $subgroup $user $role

        fi;
   done < list2.txt
done < list1.txt

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed removing carriage return and newline

Hi, I'm not very familiar with unix shell. I want to replace the combination of two carriage returns and one newline with one carriage return and one newline. I think the best way to do this is to use sed. I tried something like this: sed -e "s#\#\#g" file.txt but it doesn't work. Thanx... (2 Replies)
Discussion started by: mored
2 Replies

2. Shell Programming and Scripting

spaces and carriage returns in 'here documents'

As the title suggests, i am having some trouble figuring out how to pass spaces and carriage returns to a 'here document' ie #!/bin/bash /usr/local/install_script.sh <<SCRIPT yes no <pass carriage retun here> yes no <pass a space and then a carriage return here> exit SCRIPT any... (0 Replies)
Discussion started by: hcclnoodles
0 Replies

3. Shell Programming and Scripting

Remove Carriage returns between strings in a field

Is there any way to remove carriage retuns between the records? We have input records separated by TABS and have carriage returns as below: 123 456 789 ABC "1952.00" 678 "abcdef ghik lmno" Above we... (10 Replies)
Discussion started by: acheepi
10 Replies

4. Shell Programming and Scripting

removing thousand of carriage returns using sed

I need to replace thousands of carriage returns/line breaks in a large xml file and with spaces. I hope to do so with a script, called, for example, "removeCRs." I would invoke this at the command line as ml5003$ sed -f /Users/ml5003/removeCRs oldFile > newFile The script, I presume, would... (4 Replies)
Discussion started by: ml5003
4 Replies

5. Shell Programming and Scripting

Replacing Carriage returns without loosing EOL

Hello, I have read a few threads on this subject and tried a few things out, but still come up short. There was one good example, then the last reply was something to the effect of 'Use Sed' & 'Read a book'... Well I read a bunch of online tutorials on sed, awk, tr, but still can't get the... (2 Replies)
Discussion started by: Majiktom
2 Replies

6. Shell Programming and Scripting

removing carriage returns in text file

Hi I have a text file that looks like this: A B C D E F G H I I want it to be reformatted to A;B;C; D;E;F; G;H;I; (4 Replies)
Discussion started by: coolnfunky
4 Replies

7. Emergency UNIX and Linux Support

Adding carriage returns to file using sed/awk

Hello, I need help adding carriage returns at specific intervals (say 692 characters) to a text file that's one continous string. I'm working in AIX5.3. Any quick help is appreciated. Thanks! (2 Replies)
Discussion started by: bd_joy
2 Replies

8. Shell Programming and Scripting

TR not removing carriage returns

I have a CSV with carriage returns in place of newlines. I am trying to use tr to remove them, but it isn't working. Academic year,Term,Course name,Period,Last name,Nickname 2012-2013,First Semester,English 12,4th Period,Arnold,Adam 2012-2013,First Semester,English 12,4th Period,Adams,Jim... (1 Reply)
Discussion started by: nextyoyoma
1 Replies

9. Shell Programming and Scripting

Removing carriage returns from multiple lines in multiple files of different number of columns

Hello Gurus, I have a multiple pipe separated files which have records going over multiple Lines. End of line separator is \n and records going over multiple lines have <CR> as separator. below is example from one file. 1|ABC DEF|100|10 2|PQ RS T|200|20 3| UVWXYZ|300|30 4| GHIJKL|400|40... (7 Replies)
Discussion started by: dJHa
7 Replies

10. Shell Programming and Scripting

Remove carriage returns from awk output

I'm on Linux version 2.6.32-696.3.1.el6.x86_64, using the Ksh shell. I'm working with the input file: John Daggett, 341 King Road, Plymouth MA Alice Ford, 22 East Broadway, Richmond VA Orville Thomas, 11345 Oak Bridge Road, Tulsa OK Terry Kalkas, 402 Lans Road, Beaver Falls PA Eric Adams,... (2 Replies)
Discussion started by: prooney
2 Replies
MooseX::Declare(3pm)					User Contributed Perl Documentation				      MooseX::Declare(3pm)

NAME
MooseX::Declare - Declarative syntax for Moose SYNOPSIS
use MooseX::Declare; class BankAccount { has 'balance' => ( isa => 'Num', is => 'rw', default => 0 ); method deposit (Num $amount) { $self->balance( $self->balance + $amount ); } method withdraw (Num $amount) { my $current_balance = $self->balance(); ( $current_balance >= $amount ) || confess "Account overdrawn"; $self->balance( $current_balance - $amount ); } } class CheckingAccount extends BankAccount { has 'overdraft_account' => ( isa => 'BankAccount', is => 'rw' ); before withdraw (Num $amount) { my $overdraft_amount = $amount - $self->balance(); if ( $self->overdraft_account && $overdraft_amount > 0 ) { $self->overdraft_account->withdraw($overdraft_amount); $self->deposit($overdraft_amount); } } } DESCRIPTION
This module provides syntactic sugar for Moose, the postmodern object system for Perl 5. When used, it sets up the "class" and "role" keywords. KEYWORDS
class class Foo { ... } my $anon_class = class { ... }; Declares a new class. The class can be either named or anonymous, depending on whether or not a classname is given. Within the class definition Moose and MooseX::Method::Signatures are set up automatically in addition to the other keywords described in this document. At the end of the definition the class will be made immutable. namespace::autoclean is injected to clean up Moose and other imports for you. Because of the way the options are parsed, you cannot have a class named "is", "with" or "extends". It's possible to specify options for classes: extends class Foo extends Bar { ... } Sets a superclass for the class being declared. with class Foo with Role { ... } class Foo with Role1 with Role2 { ... } class Foo with (Role1, Role2) { ... } Applies a role or roles to the class being declared. is mutable class Foo is mutable { ... } Causes the class not to be made immutable after its definition. Options can also be provided for anonymous classes using the same syntax: my $meta_class = class with Role; role role Foo { ... } my $anon_role = role { ... }; Declares a new role. The role can be either named or anonymous, depending on whether or not a name is given. Within the role definition Moose::Role and MooseX::Method::Signatures are set up automatically in addition to the other keywords described in this document. Again, namespace::autoclean is injected to clean up Moose::Role and other imports for you. It's possible to specify options for roles: with role Foo with Bar { ... } Applies a role to the role being declared. before / after / around / override / augment before foo ($x, $y, $z) { ... } after bar ($x, $y, $z) { ... } around baz ($x, $y, $z) { ... } override moo ($x, $y, $z) { ... } augment kuh ($x, $y, $z) { ... } Add a method modifier. Those work like documented in Moose, except for the slightly nicer syntax and the method signatures, which work like documented in MooseX::Method::Signatures. For the "around" modifier an additional argument called $orig is automatically set up as the invocant for the method. clean Sometimes you don't want the automatic cleaning the "class" and "role" keywords provide using namespace::autoclean. In those cases you can specify the "dirty" trait for your class or role: use MooseX::Declare; class Foo is dirty { ... } This will prevent cleaning of your namespace, except for the keywords imported from "Moose" or "Moose::Role". Additionally, a "clean" keyword is provided, which allows you to explicitly clean all functions that were defined prior to calling "clean". Here's an example: use MooseX::Declare; class Foo is dirty { sub helper_function { ... } clean; method foo ($stuff) { ...; return helper_function($stuff); } } With that, the helper function won't be available as a method to a user of your class, but you're still able to use it inside your class. NOTE ON IMPORTS
When creating a class with MooseX::Declare like: use MooseX::Declare; class Foo { ... } What actually happens is something like this: { package Foo; use Moose; use namespace::autoclean; ... __PACKAGE__->meta->make_immutable; } So if you declare imports outside the class, the symbols get imported into the "main::" namespace, not the class' namespace. The symbols then cannot be called from within the class: use MooseX::Declare; use Data::Dump qw/dump/; class Foo { method dump($value) { return dump($value) } # Data::Dump::dump IS NOT in Foo:: method pp($value) { $self->dump($value) } # an alias for our dump method } To solve this, only import MooseX::Declare outside the class definition (because you have to). Make all other imports inside the class definition. use MooseX::Declare; class Foo { use Data::Dump qw/dump/; method dump($value) { return dump($value) } # Data::Dump::dump IS in Foo:: method pp($value) { $self->dump($value) } # an alias for our dump method } Foo->new->dump($some_value); Foo->new->pp($some_value); NOTE that the import "Data::Dump::dump()" and the method "Foo::dump()", although having the same name, do not conflict with each other, because the imported "dump" function will be cleaned during compile time, so only the method remains there at run time. If you want to do more esoteric things with imports, have a look at the "clean" keyword and the "dirty" trait. SEE ALSO
o Moose o Moose::Role o MooseX::Method::Signatures o namespace::autoclean o vim syntax: <http://www.vim.org/scripts/script.php?script_id=2526> o emacs syntax: http://github.com/jrockway/cperl-mode <http://github.com/jrockway/cperl-mode> o Geany syntax + notes: http://www.cattlegrid.info/blog/2009/09/moosex-declare-geany-syntax.html <http://www.cattlegrid.info/blog/2009/09/moosex-declare-geany-syntax.html> AUTHORS
o Florian Ragwitz <rafl@debian.org> o Ash Berlin <ash@cpan.org> o Chas. J. Owens IV <chas.owens@gmail.com> o Chris Prather <chris@prather.org> o Dave Rolsky <autarch@urth.org> o Devin Austin <dhoss@cpan.org> o Hans Dieter Pearcey <hdp@cpan.org> o Justin Hunter <justin.d.hunter@gmail.com> o Matt Kraai <kraai@ftbfs.org> o Michele Beltrame <arthas@cpan.org> o Nelo Onyiah <nelo.onyiah@gmail.com> o nperez <nperez@cpan.org> o Piers Cawley <pdcawley@bofh.org.uk> o Rafael Kitover <rkitover@io.com> o Robert 'phaylon' Sedlacek <rs@474.at> o Stevan Little <stevan.little@iinteractive.com> o Tomas Doran <bobtfish@bobtfish.net> o Yanick Champoux <yanick@babyl.dyndns.org> COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Florian Ragwitz. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.12.4 2011-08-23 MooseX::Declare(3pm)
All times are GMT -4. The time now is 05:17 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy