Format problems with fields


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Format problems with fields
# 1  
Old 07-10-2009
Format problems with fields

The following output has a space as the Field Separator.

I need:

$1 Set the field width to 15 then zero-fill to the right.
$6 Set the field width to 15 then zero-fill to the left.

Code:
01-10016 1000 MV010 20090708 12003 $NK0015101 01
01-100161 12000 MV070 20090708 12003 $NK0015201 01
01-100162 10000 MV010 20090708 12003 $NK0015301 01
01-100163 200 MV010 20090708 12003 $NK0015401 01
01-100165 1200 MV003 20090708 12003 $NK0015501 01

Both fields in the input file are strings.

Thanks,
Pawee

Last edited by vgersh99; 07-10-2009 at 02:03 PM.. Reason: code tags, PLEASE!
# 2  
Old 07-10-2009
'zero-fill' or 'pad-with-spaces'?
# 3  
Old 07-11-2009
Sorry for the mixup.
I meant Pad with zeros Smilie

Hope it's clearer
# 4  
Old 07-11-2009
You can try something like this:

Code:
awk 'BEGIN{z="0000000000"}{
  $1=$1 substr(z,1,15-length($1))
  $6=substr(z,1,15-length($6)) $6
}1' file

Regards
# 5  
Old 07-11-2009
Response:

Hi,

Not quite there.

I'm getting an error:

awk: Syntax error near line 4
awk: bailing out near line 4

Regards,
# 6  
Old 07-11-2009
Try it with nawk or /usr/xpg4/bin/awk on Solaris.

Regards
# 7  
Old 07-12-2009
Quote:
Originally Posted by Franklin52
Try it with nawk or /usr/xpg4/bin/awk on Solaris.

Regards

Hi,

Much Regards and really appreciated.

Nawk worked it outSmilie

Ahsante Sana - That's, Thank You Very Much in Swahili...!
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

awk to format file and combine two fields using comma

I am trying to use awk to format the file below, which is tab-delimited. The desired out is space delimited and is in the order of $9 $13 $2 $10-$11.$10 and $11 are often times multiple values separated by a comma, so the value in $10 is combined with the first value from $11 using the comma.... (5 Replies)
Discussion started by: cmccabe
5 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 - problems by converting date-format

Hi i try to change the date-format from DD/MM/YYYY into MM/DD/YY. Input-Data: ... 31/12/2013,23:40,198.00,6.20,2,2,2,1,11580.0,222 31/12/2013,23:50,209.00,7.30,2,2,3,0,4380.0 01/01/2014,00:00,205.90,8.30,2,2,3,1,9360.0,223 ... Output-Data should be: ...... (7 Replies)
Discussion started by: IMPe
7 Replies

5. UNIX for Dummies Questions & Answers

STDOUT redirect to file and format problems

Hi All, I am using centOS. When I try to redirect STDOUT to a file, it ends up in getting some funny characters. For example ... STDOUT of the command as follows. $ ls H3k27me3 H3k36me3 H3k4me1 H3k4me2 H3k4me3 H3k9ac H4k20me1 $ ls >test $ cat test ^ (1 Reply)
Discussion started by: Chulamakuri
1 Replies

6. Shell Programming and Scripting

Format Fields

I have a CSV file like this: abc ,def ghi ,jkl mno ,pqr stu ,vwx Desired Output: Field 1: abc mno Field 2: def ghi pqr stu Field 3: jkl vwx This does not work: for i in `cat file.txt` do Field1=`echo $i | awk -F ',' '{print $1}'| sed 's/^ *// ; s/ *$//'`... (8 Replies)
Discussion started by: shellguy
8 Replies

7. Shell Programming and Scripting

Using sed to format several fields

I have data that is tab delimited and looks like: /dev/dsk/c0t0d0s1 - - swap - no - /dev/dsk/c0t0d0s0 /dev/rdsk/c0t0d0s0 / ufs 1 no - /dev/dsk/c0t0d0s6 /dev/rdsk/c0t0d0s6 /usr ufs 1 no -... (5 Replies)
Discussion started by: bradg
5 Replies

8. Shell Programming and Scripting

awk,cut fields by change field format

Hi Everyone, # cat 1.txt 1321631,77770132976455,19,20091001011859,20091001011907 1321631,77770132976455,19,20091001011859,20091001011907 1321631,77770132976455,19,20091001011859,20091001011907 # cat 1.txt | awk -F, '{OFS=",";print $1,$3,$4,$5}' 1321631,19,20091001011859,20091001011907... (7 Replies)
Discussion started by: jimmy_y
7 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

mail format problems

Hi, I have a script that runs off the cron and creates a report, carries out a few checks and then sends the info in a email (i'm using mailx). To exectute the sending from the script all I do is, cat $reportfile | mailx -s "The Subject" my.email@work.com When I test running this... (4 Replies)
Discussion started by: nhatch
4 Replies
Login or Register to Ask a Question