Sponsored Content
Top Forums Shell Programming and Scripting awk - ignore metacharacters, search shell variables Post 302659375 by DSommers on Wednesday 20th of June 2012 08:16:14 PM
Old 06-20-2012
Thanks Chubler XL, ygemici.

I've carefully copied and pasted your codes, they do not return any result.
I'm using the debian version of awk (mawk), does that affect your suggestions?

Thank you for trying. I'm going to give it one last attempt to try and combine your suggestions into one.

1. The best way to avoid metacharacters is to use the 'index' function
2. The best way to approach comparing 2 files is to use an 'array'

With that in mind, to simplify, i've made all files tab seperated and removed the .mp3 .

Use 2 arrays from file1 "Playlist"...
  • a for <field1> "Artist"
  • t for <field2> "Title"
Example Line from Playlist;
  • Shaggy Angel (Bonus Track)

Then;
1. compare the indexed Title "Angel (Bonus Track)" to file2 "Master-Music-List" <field2> "Title"
2. check that the indexed Artist "Shaggy" matches "Master-Music-List" <field1> "Artist"

If 'both' array1 (title) , and array2 (artist) match, then print the result.

Example;

Match this from "Playlist" to "Master-Music-List";
  • Shaggy Angel (Bonus Track)
Master-Music-List Lines:
  • Shaggy Angel (Bonus Track) 32000 /Music/S/Shaggy/Angel (Bonus Track.mp3
  • The Rolling Stones Angel 128000 /Music/R/Rolling Stones/Angel.mp3

This way, the song 'Angel (Bonus Track)' will match for Shaggy and not for Rolling Stones.

Could someone please help to include the arrays and index functions ?

Thank you.

Last edited by DSommers; 06-21-2012 at 10:37 AM.. Reason: refined formatting
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using shell variables In awk

Oh its not my day for syntax... cat gzipsize.txt | awk '{print "echo",$1,$2} > master.txt I have read a lot about the awk -v but haven't been able to get it to work. I have a variable in my script and I'm looking just to push it into the awk after the $2 (or anywhere would do)!!! Every... (11 Replies)
Discussion started by: nortypig
11 Replies

2. Shell Programming and Scripting

Modify shell variables with AWK

Dear Folks, I have a command output something like: And I want to store PIN0 and SIG0 in two shell variables, now I do a double awk: PIN=`gsmctl -d /dev/ttyS0 pin sig | awk '/PIN0/ { print $2}'` SIG=`gsmctl -d /dev/ttyS0 pin sig | awk '/SIG0/ { print $2}'` It's possible to... (4 Replies)
Discussion started by: Santi
4 Replies

3. Shell Programming and Scripting

Awk, shell variables

Hello, I've been trying to figure out how to use variables inside the AWK command and use it back in the korn shell sript. in my script I have lots of awk commands like this grep Listen /etc/ssh/sshd_config | \ awk '{ if ($2 == "22" ) print "OK"; else print "not OK" }' ... (3 Replies)
Discussion started by: mirusko
3 Replies

4. Shell Programming and Scripting

search and replace using awk with variables

Hi, I have been trying to use awk with variables that needs to search for a pattern and replace it with another pattern, the patterns are supplied in a variable. I have tried several different ways without success and hope that someone can help me. Here are the details echo $UPC 07007457809... (2 Replies)
Discussion started by: jerardfjay
2 Replies

5. UNIX for Advanced & Expert Users

use of variables in awk to search for pattern from a file

Hi, I need to extract all the content between two strings stored in two variables "startstring" and "endstring" startstring=hello enstring=world #notworking awk '/$startstring/, $NF ~ /$endstring/ ' file > file2 The above code is not working with variables. It works when actual string... (2 Replies)
Discussion started by: jeanjkj
2 Replies

6. Shell Programming and Scripting

awk - take variables out to shell

Hi, How could we take the value of awk variables out to shell? I know the following methods 1. awk '{print $1}' < file | read a echo $a 2. a=`awk '{print $1}' < file` echo $a Please let me know if there are any other methods. Also, how do we take more than 1 variable value... (4 Replies)
Discussion started by: Thumban
4 Replies

7. Shell Programming and Scripting

escaping metacharacters in paths for a shell command

I have a file which contains a list of paths separated by a new line character. e.g /some/path/to/a/file.png /some/path to/another/file.jpeg /some path/to yet/another/file Notice that these paths may contain metacharacters, the spaces for example are also not escaped. If I wanted... (5 Replies)
Discussion started by: cue
5 Replies

8. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

9. UNIX for Dummies Questions & Answers

Command line not recognizing metacharacters in awk

Hello, I'm new to command line coding (and coding in general) and have run into a problem. I'm using awk to perform a global find and replace in a text file in the Terminal provided by Mac. Here is a sample of my textfile where the fields are separated by tabs. 1Ps 1,1 VWB/(J VWB VWB... (7 Replies)
Discussion started by: jvoot
7 Replies

10. Shell Programming and Scripting

Using Shell variables in awk

Hi All, I have a file which I am reading and looking for "EXIT" statement. I want to insert ":JCWPROD" after each EXIT statement only if ":JCWPROD" doesn't exist. Here is the sample file : EXIT Testing EXIT tesing123 EXIT Desired file : EXIT :JCWPROD Testing EXIT :JCWPROD... (7 Replies)
Discussion started by: nua7
7 Replies
DBIx::Class::Manual::QuickStart(3)			User Contributed Perl Documentation			DBIx::Class::Manual::QuickStart(3)

NAME
DBIx::Class::Manual::QuickStart - up and running with DBIC in 10 minutes DESCRIPTION
This document shows the minimum amount of code to make you a productive DBIC user. It requires you to be familiar with just the basics of database programming (what database tables, rows and columns are) and the basics of Perl object-oriented programming (calling methods on an object instance). It also helps if you already know a bit of SQL and how to connect to a database through DBI. Follow along with the example database shipping with this distribution, see directory examples/Schema. This database is also used through- out the rest of the documentation. Preparation First, install DBIx::Class like you do with any other CPAN distribution. See <http://www.cpan.org/modules/INSTALL.html> and perlmodinstall. Then open the distribution in your shell and change to the subdirectory mentioned earlier, the next command will download and unpack it: $ perl -mCPAN -e'CPAN::Shell->look("DBIx::Class")' DBIx-Class$ cd examples/Schema Inspect the database: DBIx-Class/examples/Schema$ sqlite3 db/example.db .dump You can also use a GUI database browser such as SQLite Manager <https://addons.mozilla.org/firefox/addon/sqlite-manager>. Have a look at the schema classes files in the subdirectory MyApp. The "MyApp::Schema" class is the entry point for loading the other classes and interacting with the database through DBIC and the "Result" classes correspond to the tables in the database. DBIx::Class::Manual::Example shows how to write all that Perl code. That is almost never necessary, though. Instead use dbicdump (part of the distribution DBIx::Class::Schema::Loader) to automatically create schema classes files from an existing database. The chapter "Resetting the database" below shows an example invocation. Connecting to the database A schema object represents the database. use MyApp::Schema qw(); my $schema = MyApp::Schema->connect('dbi:SQLite:db/example.db'); The first four arguments are the same as for "connect" in DBI. Working with data Almost all actions go through a resultset object. Adding data Via intermediate result objects: my $artist_ma = $schema->resultset('Artist')->create({ name => 'Massive Attack', }); my $cd_mezz = $artist_ma->create_related(cds => { title => 'Mezzanine', }); for ('Angel', 'Teardrop') { $cd_mezz->create_related(tracks => { title => $_ }); } Via relation accessors: $schema->resultset('Artist')->create({ name => 'Metallica', cds => [ { title => q{Kill 'Em All}, tracks => [ { title => 'Jump in the Fire' }, { title => 'Whiplash' }, ], }, { title => 'ReLoad', tracks => [ { title => 'The Memory Remains' }, { title => 'The Unforgiven II' }, { title => 'Fuel' }, ], }, ], }); Columns that are not named are filled with default values. The value "undef" acts as a "NULL" in the database. See the chapter "Introspecting the schema classes" below to find out where the non-obvious source name strings such as "Artist" and accessors such as "cds" and "tracks" come from. Set the environment variable "DBI_TRACE='1|SQL'" to see the generated queries. Retrieving data Set up a condition. my $artists_starting_with_m = $schema->resultset('Artist')->search( { name => { like => 'M%' } } ); Iterate over result objects of class "MyApp::Schema::Result::Artist". Result objects represent a row and automatically get accessors for their column names. for my $artist ($artists_starting_with_m->all) { say $artist->name; } Changing data Change the release year of all CDs titled ReLoad. $schema->resultset('Cd')->search( { title => 'ReLoad', } )->update_all( { year => 1997, } ); Removing data Removes all tracks titled Fuel regardless of which CD the belong to. $schema->resultset('Track')->search( { title => 'Fuel', } )->delete_all; Introspecting the schema classes This is useful for getting a feel for the naming of things in a REPL or during explorative programming. From the root to the details: $schema->sources; # returns qw(Cd Track Artist) $schema->source('Cd')->columns; # returns qw(cdid artist title year) $schema->source('Cd')->relationships; # returns qw(artist tracks) From a detail to the root: $some_result->result_source; # returns appropriate source $some_resultset->result_source; $some_resultsource->schema; # returns appropriate schema Resetting the database # delete database file DBIx-Class/examples/Schema$ rm -f db/example.db # create database and set up tables from definition DBIx-Class/examples/Schema$ sqlite3 db/example.db < db/example.sql # fill them with data DBIx-Class/examples/Schema$ perl ./insertdb.pl # delete the schema classes files DBIx-Class/examples/Schema$ rm -rf MyApp # recreate schema classes files from database file DBIx-Class/examples/Schema$ dbicdump -o dump_directory=. MyApp::Schema dbi:SQLite:db/example.db Where to go next If you want to exercise what you learned with a more complicated schema, load Northwind <http://code.google.com/p/northwindextended/> into your database. If you want to transfer your existing SQL knowledge, read DBIx::Class::Manual::SQLHackers. Continue with DBIx::Class::Tutorial and "WHERE TO START READING" in DBIx::Class. perl v5.18.2 2014-01-05 DBIx::Class::Manual::QuickStart(3)
All times are GMT -4. The time now is 08:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy