formatting the fields

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting formatting the fields
# 1  
Old 08-04-2009
formatting the fields

hi i am using the following code in my shell script
awk -F":" '{printf("%-20.20s:%-20.20s:%-20.20s:%-20.20s:%-15.15s:%-3.3s:%-19.19s:%-2.2s:%-20.20s:%-15.15s:%-2.2s\n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)}' $DIALPBIN/temp1.txt > ${DIALPBIN}/temp2.txt

actually the 5th field should not contain spaces or any special character.Smilie
what is the solution?

Last edited by zaxxon; 08-04-2009 at 08:45 AM.. Reason: CODE tags
# 2  
Old 08-04-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums


Can you please give an excerpt of your input file and the wanted output? Use code tags please.
# 3  
Old 08-04-2009
what my code is doing, it is executing a sql file and the resullset of the query is getting stored in the text file in a fixed format. for that fixed format i have used the following code::

awk -F":" '{printf("%-20.20s:%-20.20s:%-20.20s:%-20.20s:%-15.15s:%-3.3s:%-19.19s:%-2.2s:%-20.20s:%-15.15s:%-2.2s\n", $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11)}' $DIALPBIN/temp1.txt > ${DIALPBIN}/temp2.tx

now 5th filed is a phone number. in the database it contains brackets like (0124)-23456
but in the file the output should be like 012423456

Could you please help????
# 4  
Old 08-04-2009

echo "(0124)-23456"| awk '{gsub(/[()-]/,""); print}'

So in your awk line you might change following part:
... $2,$3,$4,(gsub(/[()-]/,"",$5)),$6,$7,...

Have currently no example to test it but it should be alike, sorry.

Last edited by zaxxon; 08-04-2009 at 10:34 AM..
# 5  
Old 08-04-2009
this is not working..
# 6  
Old 08-04-2009
I missed a closing bracket. Maybe try this again. Corrected it in my former post. If it still does not work, give a complete example line, ty.

---------- Post updated at 02:41 PM ---------- Previous update was at 02:34 PM ----------

Ok, found the real problem you can see by the example how it works:

echo "aa (0124)-23456 bb"| awk '{gsub(/[()-]/,"",$2); printf("%s %s %s\n", $1,$2,$3)}'
aa 012423456 bb

So keep your line as is but add in front of the printf add the gsub part. Just change the $2 in the gsub into $5.

Last edited by zaxxon; 08-04-2009 at 10:49 AM..
# 7  
Old 08-04-2009

i am getting these two errors

awk: syntax error near line 1
awk: illegal statement near line 1

---------- Post updated at 07:55 AM ---------- Previous update was at 07:53 AM ----------

gsub is not found..actually mine is a solaris OS.
could u suggest somne other option
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Is there a UNIX command that can compare fields of files with differing number of fields?

Hi, Below are the sample files. x.txt is from an Excel file that is a list of users from Windows and y.txt is a list of database account. $ head -500 x.txt y.txt ==> x.txt <== TEST01 APP_USER_PROFILE USER03 APP_USER_PROFILE TEST02 APP_USER_EXP_PROFILE TEST04 APP_USER_PROFILE USER01 ... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Print . in blank fields to prevent fields from shifting

The below code works great, kindly provided by @Don Cragun, the lines in bold print the current output. Since some of the fields printed can be blank some of the fields are shifted. I can not seem too add . to the blank fields like in the desired output. Basically, if there is nothing in the field... (10 Replies)
Discussion started by: cmccabe
10 Replies

3. 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

4. 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

5. 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

6. Shell Programming and Scripting

Formatting and combining fields of the input file

Hi, I have a file of the following format: AV 103 AV 104 AV 105 AV 308 AV 517 BN 210 BN 211 BN 212 BN 218 and the desired output is : AV 103-105 3 AV 308 1 AV 517 1 BN 210-212 3 (5 Replies)
Discussion started by: rochitsharma
5 Replies

7. Shell Programming and Scripting

Add fields in different files only if some fields between them match

Hi everybody (first time posting here) I have a file1 that looks like > 1,101,0.1,0.1 1,26,0.1,0.1 1,3,0.1,0.1 1,97,0.5,0.5 1,98,8.1,0.218919 1,99,6.2,0.248 2,101,0.1,0.1 2,24,3.1,0.147619 2,25,23.5,0.559524 2,26,34,0.723404with 762 lines.. I have another 'similar' file2 > ... (10 Replies)
Discussion started by: murpholinox
10 Replies

8. UNIX for Dummies Questions & Answers

Formatting Multiple fields on 1 line to multiple rows

I'm trying extract a number of filename fields from a log file and copy them out as separate rows in a text file so i can load them into a table. I'm able to get the filenames but the all appear on one line. I tried using the cut command with the -d (delimiter) option but cant seem to make it... (1 Reply)
Discussion started by: Sinbad-66
1 Replies

9. 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

10. Shell Programming and Scripting

Formatting fields of a file

I have a file with n number of cols. I need to modify the size of each field. it may be either increase or decrease the present size of the field. can anybody help me in this plz thanks in advance (4 Replies)
Discussion started by: kolvi
4 Replies
Login or Register to Ask a Question