Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formatting a file in 4 columns 1

Status
Not open for further replies.

maguy

Technical User
Sep 17, 2002
4
FR

Input file is like this :

21-MAI-05|CR1-1ER|DEJEUNER|CAROTTES PERSILLES|SALE|1 PART|PREPARATION CHAUD|6


21-MAI-05|CR1-1ER|DINER|CELERI REMOULADE|SALE|1 PART|LABORATOIRE FROID|25


21-MAI-05|CR1-1ER|DEJEUNER|CELERI REMOULADE MIXE|SALE|1 PART|LABORATOIRE FROID|8


21-MAI-05|CR1-1ER|DEJEUNER|COMPOTE DE POIRE SUCRE|MODE UNIQUE|1 PART|MAGASIN|7


21-MAI-05|CR1-1ER|DEJEUNER|CONCOMBRE VINAIGRETTE|SALE|1 PART|LABORATOIRE FROID|1


21-MAI-05|CR1-1ER|DEJEUNER|FAISSELLE NATURE|SANS SUCRE|1 PART|BEURRE OEUF FROMAG
E|2

I need output file like the following :

21-MAI-05 21-MAI-05 21-MAI-05 21-AI-05
CR1-1ER CR1-1ER CR1-1ER CR1-ER
DEJEUNER DINER DEJEUNER DEJEUNER
CAROTTES PERSILLES CELERI REMOULADE COMPOTE DE P COMPOTE
SALE SALE SALE
PREPARATION CHAUD LABORATOIRE FROID LABORATOIRE FROID
6 25 8

21-MAI-05 21-MAI-05
CR1-1ER CR1-1ER
DEJEUNER DEJEUNER
CONCOMBRE VINAIGR FAISELLE NATURE
SALE SANS SUCRE
LABORATOIRE FROID BEURRE OEUF FROMAGE
1 2

I have nearly 1500 lines in my file. It also depends and can change any time But the file format is the same.
I need help to create a script using awk.

Thanks in advance
Maguy
 
A starting point:
awk '
BEGIN{FS="|";OFS="\t"}
NF==8{r=(NR % 4);for(i=1;i<=8;++i)a[r,i]=$i}
!r{for(i=1;i<=8;++i)if(i!=6)print a[1,i],a[2,i],a[3,i],a[0,i];delete a}
END{if(r)for(i=1;i<=8;++i)if(i!=6)print a[1,i],a[2,i],a[3,i],a[0,i]}
' /path/to/input > output

Bonne chance.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Code:
BEGIN {
  FS="[|]"
  OFS=" "
}
NF && !/^[ ]*$/ { colsN=NF; arr[NR]=$0 }

END {
  for(col=1; col <= colsN; col++)
     for(row=1; row <= NR; row++) {
       split(arr[row], tmp, FS)
       printf("%-10s%s", tmp[col],  (row == NR) ? "\n" : OFS)
       split("", tmp)
     }
}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Since your awk version lacks the delete array instruction (thread271-1064242) and you don't have nawk (thread271-1064253), you may try this:
awk -F'|' '
NF==8{r=(NR % 4);for(i=1;i<=8;++i)a[r,i]=$i}
!r{PrintEtiq()}
END{if(r)PrintEtiq()}
function PrintEtiq( i,j){
for(i=1;i<=8;++i){
if(i!=6)
printf "%-19.19s %-19.19s %-19.19s %-19.19s\n",a[1,i],a[2,i],a[3,i],a[0,i]
for(j=0;j<=3;++j)a[j,i]=""
}printf "\n"}
' cdi.txt > mag.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV said:
Since your awk version lacks the delete array instruction...
No one needs that instruction, since one can do as Vlad did:
Code:
split("", tmp)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top