The UNIX and Linux Forums  

Go Back   UNIX e Linux Forum > Inizio Forum > Shell scripting e di programmazione
.
google unix.com



Shell scripting e di programmazione Pubblica domande su KSH, CSH, SH, Bash, Perl, PHP, sed, awk e da altri script di shell e linguaggi di scripting shell qui.

Più di UNIX e Linux Forum Argomenti potreste trovare utili
Filo Thread Starter Forum Risposte Ultimo Post
La fusione delle linee utilizzando AWK senthil_is Shell scripting e di programmazione 6 03-05-2008 12:40 PM
La fusione delle linee - Mettere a punto il copione senthil_is Shell scripting e di programmazione 3 03-05-2008 03:24 AM
fondendo due righe in un file thaduka UNIX for Dummies Domande & Risposte 6 07-11-2007 10:27 AM
Linee di fusione in un unico Foxgard UNIX for Dummies Domande & Risposte 8 06-19-2005 07:36 AM
Contare le linee ed i file jorge.ferreira UNIX for Dummies Domande & Risposte 6 12-11-2003 11:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Cerca in questo Thread Rate Thread Modalità di visualizzazione
  #1 (permalink)  
Old 01-28-2008
Meert's Avatar
Meert Meert is offline
Utente Registrato
  
 

Iscriviti Data: Sep 2007
Posti: 8
Unire i file con AWK di filtraggio e di conteggio delle linee

Hi there,

Ho un paio di file ho bisogno di unione. Non posso fare un semplice da concatenare fondere in un unico grande file.

Ma poi ho bisogno di filtrare i file per ottenere un risultato desiderato.

L'output è simile a questa:

Codice:
TRNH 0000000010941
ORDH
OADR
OADR
ORDL
ENDT    1116399         000000003 000000001
TRLR 0000000010941 000000003 000000001
TRNH 0000000010942
ORDH
OADR
OADR
ORDL
ENDT    1116400         000000003 000000001
TRLR 0000000010942 000000003 000000001
TRNH 0000000010943
ORDH
OADR
OMSG
ORDL
ORDL
ENDT    1116399         000000004 000000001
TRLR 0000000010943 000000003 000000001
TRNH 0000000010944
ORDH
OADR
OADR
ORDL
ENDT    1116400         000000003 000000001
ORDH                                       
OADR                                       
OADR
ORDL                                       
ORDL                                       
ENDT    1116400         000000004 000000001
TRLR 0000000010944 000000007 000000002

e il filtro dovrebbe lasciare la prima e l'ultima linea con la TRNH e TRLR (dando l'ultimo TRLR la stessa sequenza come il primo TRNH). Il resto del TRNH e TRLR linee devono essere omessi.

Poi il finale TRLR dovrebbe rappresentare la quantità di linee ORDH e l'importo della OADR, OMSG e ORDL linee.

Non ho ottenuto lo scopo di sopprimere l'extra TRNH e linee TRLR ancora, questo è il filtro che ho finora:


Codice:
BEGIN {
#	 define two counters 
	ordh_cnt = 0;
	ordl_total_cnt = 0;
}

#  Start filter 

#  if line start with ORDH add 1 to counters 
$1 == "ORDH" {
	ordh_cnt++;
}

#  if line starts with TRLR, adjust line to reflect new count of ORDH in order
$1 == "TRLR" {
	printf "%s%9.9d%s\n", substr($0, 0, 31), ordh_cnt, substr($0, 39);
#	 line has been printed, next rule 
	next;
}

#  if line start with ORDL add 1 to counters 
$1 == "ORDL" {
	ordl_total_cnt++;
}

#  if line start with OADR add 1 to counters 
$1 == "OADR" {
	ordl_total_cnt++;
}

#  if line start with OMSG add 1 to counters 
$1 == "OMSG" {
	ordl_total_cnt++;
}

#  if line starts with TRLR, adjust line to reflect new total ORDL, OADR and OMSG in complete file 
$1 == "TRLR" {
	printf "%s%9.9d%s\n", substr($0, 0, 19), ordl_total_cnt, substr($0, 29);
#	 line has been printed, next rule 
	next;
}

#  Line has not changed, print normal line 
{
	print $0;
}

Ora la quantità di linee ORDH è uscita al mio nuovo file, in modo che sembra funzionare. Tuttavia, la quantità di OADR, OMSG e ORDL linee non è stato corretto in uscita.

Il risultato finale dovrebbe essere simile al seguente:


Codice:
TRNH 0000000010941
ORDH
OADR
OADR
ORDL
ENDT    1116399         000000003 000000001
ORDH
OADR
OADR
ORDL
ENDT    1116400         000000003 000000001
ORDH
OADR
OMSG
ORDL
ORDL
ENDT    1116399         000000004 000000001
ORDH
OADR
OADR
ORDL
ENDT    1116400         000000003 000000001
ORDH                                       
OADR                                       
OADR
ORDL                                       
ORDL                                       
ENDT    1116400         000000004 000000001
TRLR 0000000010941 000000017 000000005

Qualsiasi aiuto sarebbe molto apprezzato
  #2 (permalink)  
Old 01-28-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Iscriviti Data: gennaio 2007
Ubicazione: Варна, България / Milano, Italia
Messaggi: 2.928

Codice:
awk 'NR == 1 { trnh = $2; print }
!/^TR(NH|LR)/ { 
  if ($1 == "ORDH") 
    ordh ++ 
  if ($1 ~ /^O(ADR|MSG|RDL)/) 
    ordl ++
  print 
} END {
printf "TRLR %s %.9d %.9d\n", trnh, ordl, ordh
}' filename

  #3 (permalink)  
Old 01-28-2008
Meert's Avatar
Meert Meert is offline
Utente Registrato
  
 

Iscriviti Data: Sep 2007
Posti: 8
La ringrazio molto, funziona come un fascino!
Closed Thread

Segnalibri

Thread Tools Cerca in questo Thread
Cerca in questo Thread:

Ricerca Avanzata
Modalità di visualizzazione Vota questo thread
Vota questo thread:

Distacco regolamento
Tu non può post nuovo thread
Tu non può inviare una risposta
Tu non può postare allegati
Tu non può modificare i tuoi post

BB codice è Su
Smilies sono Su
[IMG] codice Su
Codice HTML è Chiuso
Trackbacks sono Su
Pingbacks sono Su
Refbacks sono Su




Tutti gli orari sono GMT -4. La data di oggi è 08:59 PM.


Powered by: vBulletin, Copyright © 2000 - 2006, Jelsoft Enterprises Limited. Traduzioni Powered by .
vBCredits v1.4 Copyright © 2007 - 2008, PixelFX Studios
UNIX e Linux Forum Content Copyright © 1993-2009. Tutti i diritti Reserved.Ad di gestione da RedTyger

Contenuti pertinenti URL da vBSEO 3.2.0