Sponsored Content
Top Forums Shell Programming and Scripting cutting fields and doing trim in awk Post 302239381 by naoseionome on Tuesday 23rd of September 2008 01:26:41 PM
Old 09-23-2008
cutting fields and doing trim in awk

hi,
I have this code:
Code:
#!/usr/bin/ksh

#SERVICES
gzcat *SERVICES* | awk '  
{
SUBSCRIBERNUMBER=substr($0,1,20) 
SUBSCRIBERNUMBER=trim(SUBSCRIBERNUMBER)
SERVICECODE=substr($0,22,61) 
SERVICECODE=trim(SERVICECODE)
STARTDD=substr($0,63,72) 
STARTDD=trim(STARTDD)
STARTDT=substr($0,74,81) 
STARTDT=trim(STARTDT)
ENDDD=substr($0,83,92)
ENDDD=trim(ENDDD)
ENDDT=substr($0,94,101) 
ENDDT=trim(ENDDT)
BASICSERVICE=substr($0,103,150) 
BASICSERVICE=trim(BASICSERVICE)
print SUBSCRIBERNUMBER","SERVICECODE","STARTDD","STARTDT","ENDDD","ENDDT","BASICSERVICE}
function ltrim(s) { gsub(/^ +/, "", s); return s }
function rtrim(s) { gsub(/ +$/, "", s); return s }
function trim(s)  { return rtrim(ltrim(s)); }
'

this code should slice a file that has fixed positions and then clean the spaces for each fields and in the end save it like a csv file.
I'm missing something and it is not creating the final lines correctly, it is replicating fields.
insted of and output like:
Code:
SUBSCRIBERNUMBER","SERVICECODE","STARTDD","STARTDT","ENDDD","ENDDT","BASICSERVICE

i have:
Code:
03002000001         ,NWD                                      2006-04-08 16:14:02 ,2006-04-08 16:14:02                     NORM                            ,16:14:02                     NORM                                            ,                    NORM                                            ,         NORM                                            ,NORM

can someone help me out on this?

best regards,
Ricardo Tomás
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need help cutting a couple of fields from log file.

Data begins as such; Mar 16 03:27:05 afzpimdn01 named: denied update from .3983 for "nnn.nnn.in-addr.arpa" IN Mar 16 03:27:37 afzpimdn01 named: denied update from .4844 for "nnn.nnn.in-addr.arpa" IN Mar 16 03:27:56 afzpimdn01 named: denied update from .2650 for "nnn.nnn.in-addr.arpa" IN ... (4 Replies)
Discussion started by: altamaha
4 Replies

2. Shell Programming and Scripting

Trim inside awk

Hi all, I am using qwk to parse the logfile. The code like awk ' { if($0 > " ") { MSISDN=substr($0,1,10) HOUR=substr($0,11,6); ID_SA_SOURCE=substr($0,17,18); ID_SA_DEST=substr($0,35,18); ... (3 Replies)
Discussion started by: subin_bala
3 Replies

3. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

4. Shell Programming and Scripting

Trim empty fields in a given range

Is there some easy way to trim empty fields but only in a given range? for example say I have csv data that looks like this: apple,,,Granysmith,,2.50,,TimmysGrocers Pear,Bartlett,,,,,Park, peach,,,,Peento,3.00,Garden,TimmysGrocers is there a way of getting the single field with data... (4 Replies)
Discussion started by: cue
4 Replies

5. Shell Programming and Scripting

Cutting fields from lines with multiple spaces

Please see the following code, between "status" and "OK" exists many spaces, I want to get status OK . how to ignore multi spaces? If tab exists in the spaces, how to ignore it ? Is there other commands can replace cut? $ echo 'drv status OK'| cut... (3 Replies)
Discussion started by: 915086731
3 Replies

6. Shell Programming and Scripting

Help with awk trim

I am trying to trim spaces for the fixed width file starting from location 129 and of length 20. I am expecting only around 40 records that will have length greater than 9. But i am getting around 4000 records. Please help me correct the following. nawk '{if (a=length(gsub(/... (2 Replies)
Discussion started by: pinnacle
2 Replies

7. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

8. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

9. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

10. Shell Programming and Scripting

Trim using sed/awk

How to remove using the sed/awk if the rows are starting with any numbers till "-" character. from the below sample output and expected results. Sample looks like below : 704 - sample - test 5500 - line2 449 - line3 44 - line4 Line5 Expected - sample - test line2 line3 line4... (5 Replies)
Discussion started by: kiran_hp
5 Replies
dlanst.f(3)							      LAPACK							       dlanst.f(3)

NAME
dlanst.f - SYNOPSIS
Functions/Subroutines DOUBLE PRECISION function dlanst (NORM, N, D, E) DLANST Function/Subroutine Documentation DOUBLE PRECISION function dlanst (characterNORM, integerN, double precision, dimension( * )D, double precision, dimension( * )E) DLANST Purpose: DLANST returns the value of the one norm, or the Frobenius norm, or the infinity norm, or the element of largest absolute value of a real symmetric tridiagonal matrix A. Returns: DLANST DLANST = ( max(abs(A(i,j))), NORM = 'M' or 'm' ( ( norm1(A), NORM = '1', 'O' or 'o' ( ( normI(A), NORM = 'I' or 'i' ( ( normF(A), NORM = 'F', 'f', 'E' or 'e' where norm1 denotes the one norm of a matrix (maximum column sum), normI denotes the infinity norm of a matrix (maximum row sum) and normF denotes the Frobenius norm of a matrix (square root of sum of squares). Note that max(abs(A(i,j))) is not a consistent matrix norm. Parameters: NORM NORM is CHARACTER*1 Specifies the value to be returned in DLANST as described above. N N is INTEGER The order of the matrix A. N >= 0. When N = 0, DLANST is set to zero. D D is DOUBLE PRECISION array, dimension (N) The diagonal elements of A. E E is DOUBLE PRECISION array, dimension (N-1) The (n-1) sub-diagonal or super-diagonal elements of A. Author: Univ. of Tennessee Univ. of California Berkeley Univ. of Colorado Denver NAG Ltd. Date: November 2011 Definition at line 101 of file dlanst.f. Author Generated automatically by Doxygen for LAPACK from the source code. Version 3.4.1 Sun May 26 2013 dlanst.f(3)
All times are GMT -4. The time now is 02:09 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy