LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-19-2012, 10:13 AM   #1
corfuitl
Member
 
Registered: Mar 2012
Posts: 38

Rep: Reputation: Disabled
Language pairing of files


hi,

I have in a directory a couple of files in different languages with names like:

bb_aaaaaa-aaaa-en.html
bb_aaaaaa-aaaa-es.html
bb_aaaaaa-aaaa-de.html
vv_aaaaaa-aaaa-en.html
vv_aaaaaa-aaaa-es.html
vv_aaaaaa-aaaa-de.html
ff_aaaaaa-aaaa-en.html
ff_aaaaaa-aaaa-es.html
ff_aaaaaa-aaaa-de.html

I want to create a 2 columns list with for a language pair. For example for the en de language pair.

bb_aaaaaa-aaaa-en.html bb_aaaaaa-aaaa-de.html
vv_aaaaaa-aaaa-en.html vv_aaaaaa-aaaa-de.html
ff_aaaaaa-aaaa-en.html ff_aaaaaa-aaaa-de.html

I have a bash script but it doesn't work.

Code:
#!/bin/sh

L1=en
L2=de

cat list.txt | awk '/*__.*__'$L1'.*__'$L2'/{
  for (i=1; i <= NF; i++)
    if (index($i, "__'$L1'.") > 0) { printf("%s", $i); break; }
  for (   ; i <= NF; i++)
    if (index($i, "__'$L2'.") > 0) printf(" %s", $i);
  printf("\n"); }'
Could you help me please? Thank you in advance!
 
Old 12-19-2012, 08:05 PM   #2
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Well, there are several problems with your code. For example:
  1. You are using cat to send your data to awk, but awk can do it's own input. The form awk '<code>' <input> is normally preferred to cat <input> | awk '<code>' since it avoids a unnecessary process creation and pipe.
  2. The RE you're using ('/*__.*__'$L1'.*__'$L2'/') does not match any line in your (sample) input file. I suspect you may have meant to use something like '/^[[:alpha:]_][[:alnum:]-_]*[_-]+'${L1}'[.]+.* +[[:alpha:]_][[:alnum:]-_]*[_-]+'${L2}'[.]*[[:alnum:]]*$'
  3. If you pasted your code into you post, perhaps the backslashes escaping your dots were stripped, but, if not, and you want a literal dot in your RE, the [.] form should work.
  4. The logic in you code assumes that $L1 will always proceed $L2 for any pair, but you don't check that your input file list is sorted, nor that $L1 sorts before $L2, nor do you check the value of LC_NAME or LC_COLLATE. (Check the locale command output.)

Last edited by PTrenholme; 12-19-2012 at 08:11 PM.
 
Old 12-19-2012, 11:59 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,167

Rep: Reputation: 306Reputation: 306Reputation: 306Reputation: 306
Try this ...
Code:
join -j2 $InFile $InFile                                         \
|sed 's/\( .._\)\(.*\)\( .._\)\(.*\)/\1 \3 \1\2\3\4/'            \
|awk -F " " '{if ($1==$2 && $3!=$4) print $3 " " $4}'            \
|sed 's/\(.*\)\(\-..\.\)\(.*\)\(\-..\.\)\(.*\)/\1 \2 \3 \4 \5/'  \
|sort -k2,2                                                      \
|awk -F " " '{print $1$2$3 " " $4$5$6}'                          \
> $OutFile
Daniel B. Martin
 
  


Reply

Tags
awk, bash, files


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to read/write files in C language? majia Linux - Newbie 2 09-08-2009 09:53 AM
reading excel files in c language MiniGopal Programming 5 03-24-2009 10:09 AM
Japanese Language Files Inexactitude Linux - General 2 08-11-2008 03:36 AM
Reading excel files from c language rajesh_b Programming 4 11-25-2004 07:26 AM
changing language of new files Randy C Linux - Newbie 4 10-05-2004 05:15 PM


All times are GMT -5. The time now is 06:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration