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!

reformat file 1

Status
Not open for further replies.

subok

MIS
Feb 21, 2005
37
BE
Hi,
I have the following output :

MSIN: 7100000000



NETWORK ACCESS: GSM


NUMTYP: SINGLE
MSCAT: ORDINSUB
SUBRES: ONAOFPLM
BAOC: OCROUT
BAIC: ICROUT
BAROAM: NONE
BAGPRS: NONE
BAPRC: NONE
BASSM: NONE
BASPH: NONE

NDC: 721



BSV SN BCN BCN
--------+----------------+--------+--------
TELEPHON 247307
TS21 247307
TS22 247307
BS26 247307

INTERRUPTION TEXT JOB 6035

Until the next MSIN if found, I want the succeeding lines to be in a single record.

Thanks.


 
Code:
#!/usr/bin/awk -f

BEGIN { FS = ":?\t+" } # "[ \t:]+"

$1 == "MSIN" {
    if (A1[1]) _print()
    x = 1 
}

$1 && x {
    data = getlast()
    #printf "\"%s\" - \"%s\"\n", $0, data
    A1[++fcount] = $0
    A2[$0] = data
}

function getlast(last) {
    last = $NF; $NF = ""; gsub(/[ \t:]*$/, ""); return last
}

function _print(i) {
    while (A1[++i]) printf "%s: %s   ", A1[i], A2[A1[i]]
    printf "\n"
    delete A1; delete A2; fcount = 0 
}

END { if (A1[1]) _print() }

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
FYI,
not all awk-s allow deleting entire arrays like so:
delete A1

a somewhat portable workaround for this is:
split("",A1)

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Nice.. =p i was having serval issues with that some long time ago, but when i tried lately, it even worked with the 'original-awk', 2004er version though. Surprised me.

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Until the next MSIN if found, I want the succeeding lines to be in a single record.
Something like this ?
awk '
/^MSIN:/{if(f++)printf "\n"}
{printf "%s ",$0}
END{printf "\n"}
' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Doh!

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Hi all Awk gurus,

I have a simple (i hope) query here. I have this awk script which I want to be able to reformat a file. The file looks like this:

Manufactured Item | ZLDRS1 ULTRA·STRIP III,DUAt
0649-1046-48 | BOX,275 X 197 X| BN |
2450-0054-01 | LBL,BLNK,PAP TH| A0 |
3200-0111-03 | TAPE,TISSUE PAP| D0 |
3200-0125-02 | LAM,PET/PE,.004| B0 |
Manufactured Item | ZLDRS2 ULTRASTRIP III,DUAL t
0649-1046-48 | BOX,275 X 197 X| BN |
2450-0054-01 | LBL,BLNK,PAP TH| A0 |
3200-0111-03 | TAPE,TISSUE PAP| D0 |

I want the file to look like

0649-1046-48 | BOX,275 X 197 X| BN | ZLDRS1
2450-0054-01 | LBL,BLNK,PAP TH| A0 | ZLDRS1
3200-0111-03 | TAPE,TISSUE PAP| D0 | ZLDRS1
3200-0125-02 | LAM,PET/PE,.004| B0 | ZLDRS1
0649-1046-48 | BOX,275 X 197 X| BN | ZLDRS2
2450-0054-01 | LBL,BLNK,PAP TH| A0 | ZLDRS2
3200-0111-03 | TAPE,TISSUE PAP| D0 |ZLDRS2

To achive this I used this script...
# extract bom lines in excel format
BEGIN { FS = "|" }
/Manufactured/ { mitem = substr($2, 1, 30) }
{ print $1 "," $2 "," $3 "," $7 "," $9 "," $mitem }

But when I run it I get the error message:
awk: Field $( ZLDRS1 ) is not correct.
The input line number is 1. The file is summboms.2.
The source line number is 8.

Could anyone tell me why this is comming and help me get this running please?
Thanks in advance
Bhaswar
-:)
 
pls don't piggy-back your question on top of the existing threads. Start a new thread.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
BhasWar..

Code:
awk 'BEGIN { FS = "[ \t|]+"; OFS = "|" }
/Manufactured/ { mitem = $3 }
! /Manufactured/ { print $0 "\t" mitem }'

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Yes, i was referring to EXIT as it doesnt appear anywhere in your snippet.
 
Caps Lock

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
xmb and Vlad: I'd better wake up before I post any more code. Here's what I thought I was doing:
Code:
BEGIN {ORS=""}
(/^MSIN/&&NR>1) || [COLOR=red yello]END[/color] { print "\n" }
1
I was surprised that Awk accepted it! (Awk doesn't accept it now that I've changed it.)
 
:) + ORS = " "
[Using BEGIN and END in conditions is not possible, as you saw.]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top