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
TRIM(3) 								 1								   TRIM(3)

trim - Strip whitespace (or other characters) from the beginning and end of a string

SYNOPSIS
string trim (string $str, [string $character_mask = " 0r B"]) DESCRIPTION
This function returns a string with whitespace stripped from the beginning and end of $str. Without the second parameter, trim(3) will strip these characters: o " " (ASCII 32 ( 0x20)), an ordinary space. o " " (ASCII 9 ( 0x09)), a tab. o " " (ASCII 10 ( 0x0A)), a new line (line feed). o " " (ASCII 13 ( 0x0D)), a carriage return. o "" (ASCII 0 ( 0x00)), the NUL-byte. o "x0B" (ASCII 11 ( 0x0B)), a vertical tab. PARAMETERS
o $str - The string that will be trimmed. o $character_mask - Optionally, the stripped characters can also be specified using the $character_mask parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters. RETURN VALUES
The trimmed string. EXAMPLES
Example #1 Usage example of trim(3) <?php $text = " These are a few words :) ... "; $binary = "x09Example stringx0A"; $hello = "Hello World"; var_dump($text, $binary, $hello); print " "; $trimmed = trim($text); var_dump($trimmed); $trimmed = trim($text, " ."); var_dump($trimmed); $trimmed = trim($hello, "Hdle"); var_dump($trimmed); $trimmed = trim($hello, 'HdWr'); var_dump($trimmed); // trim the ASCII control characters at the beginning and end of $binary // (from 0 to 31 inclusive) $clean = trim($binary, "x00..x1F"); var_dump($clean); ?> The above example will output: string(32) " These are a few words :) ... " string(16) " Example string " string(11) "Hello World" string(28) "These are a few words :) ..." string(24) "These are a few words :)" string(5) "o Wor" string(9) "ello Worl" string(14) "Example string" Example #2 Trimming array values with trim(3) <?php function trim_value(&$value) { $value = trim($value); } $fruit = array('apple','banana ', ' cranberry '); var_dump($fruit); array_walk($fruit, 'trim_value'); var_dump($fruit); ?> The above example will output: array(3) { [0]=> string(5) "apple" [1]=> string(7) "banana " [2]=> string(11) " cranberry " } array(3) { [0]=> string(5) "apple" [1]=> string(6) "banana" [2]=> string(9) "cranberry" } NOTES
Note Possible gotcha: removing middle characters Because trim(3) trims characters from the beginning and end of a string, it may be confusing when characters are (or are not) removed from the middle. trim('abc', 'bad') removes both 'a' and 'b' because it trims 'a' thus moving 'b' to the beginning to also be trimmed. So, this is why it "works" whereas trim('abc', 'b') seemingly does not. SEE ALSO
ltrim(3), rtrim(3), str_replace(3). PHP Documentation Group TRIM(3)
All times are GMT -4. The time now is 06:53 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy