Computing.Net > Forums > Unix > Need help printing in awk using

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Need help printing in awk using

Reply to Message Icon

Name: balmbkm
Date: May 2, 2009 at 04:39:47 Pacific
OS: SUN
CPU/Ram: 2G
Product: Sun / Ultraax-i2
Subcategory: General
Comment:

I am writing script that reclaim storage that is mapped behind FAs. I am stuck on this step. I can read the first field of each line to execute a command or take up to 12 lines of field1 separated by a comma (,) and run command. U can see the diff.

My input file is file1.out and has this:
0011 Not Visible ***:* 02B:C0 2-Way Mir N/Grp'd RW 668
0013 Not Visible ***:* 08B:C0 2-Way Mir N/Grp'd RW 668
0014 Not Visible ***:* 01B:C0 2-Way Mir N/Grp'd RW 668
0015 Not Visible ***:* 02A:C0 2-Way Mir N/Grp'd RW 668
007F Not Visible ***:* 01B:D1 2-Way Mir N/Grp'd (M) RW 51789
0085 Not Visible ***:* 10A:D5 2-Way Mir N/Grp'd RW 8632
0086 Not Visible ***:* 07A:D0 2-Way Mir N/Grp'd RW 8632
0087 Not Visible ***:* 10B:D3 2-Way Mir N/Grp'd RW 8632
0088 Not Visible ***:* 16B:D2 2-Way Mir N/Grp'd RW 8632
0089 Not Visible ***:* 07B:CA 2-Way Mir N/Grp'd (M) RW 17263
008B Not Visible ***:* 07A:DA 2-Way Mir N/Grp'd (M) RW 17263
008D Not Visible ***:* 07B:D1 2-Way Mir N/Grp'd (M) RW 17263

This file can be hundred of lines.

I want an output that lookslike this:
0011,0013,0014,0015,007F,0085,0086,0087
0088,0089,008B,008D,008F,0093,0097009F
00E3,013A ,0140,0162,017D,0188,018B
so my command then will read each line as a parameter like this:

symmaskdb -sid xxxx list assign -dev < line generated above >

Any help I would appreciate.

I am onlble to print all field1 in a single line. like this: awk '{printf("%s,",$1) }' vlud.out

Btw this is not homework. Just want to simplify my daily routines at work.

Thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: May 2, 2009 at 11:38:55 Pacific
Reply:


I am not certain of your requirements concerning how many arguments should print. This script accepts a variable:

#!/bin/ksh

norec=$(wc -l < vlud.out)
awk '
{
if(NR % cnt == 0 || NR == myrec)
   printf("%s\n",$1)
else
   printf("%s,",$1)

} ' cnt=7 myrec="$norec" vlud.out


0

Response Number 2
Name: balmbkm
Date: May 2, 2009 at 14:57:01 Pacific
Reply:

It worked and that is exactly what I wanted. Perfect and I appreciated. Moh.


0

Response Number 3
Name: balmbkm
Date: May 2, 2009 at 15:47:24 Pacific
Reply:

Nail - one more thing in this logic. Now dumpued it in a file but the last character in the file is , as you can see in output.

0011,0013,0014,0015,007F,0085,0086
0087,0088,0089,008B,008D,008F,0093
0097,009F,00E3,013A,0140,0162,017D
0188,018B,019E,01AA,01B1,01B6,01BC
01BD,01BE,01BF,01C0,01C1,01C2,01C3
01D9,01DA,01DB,01DC,032A,0330,0336
0370,0509,050A,055B,

Any way using sed to remove that last , at the end last line?

Thanks


0

Response Number 4
Name: nails
Date: May 2, 2009 at 17:46:49 Pacific
Reply:

For some reason, the solaris/sun version of awk is not passing the 2nd variable, norec, correctly. It looks like a bug. Change to nawk, and the script appears to work:

#!/bin/ksh

norec=$(wc -l < vlud.out)

nawk '
{
if(NR % cnt == 0 || NR == myrec)
   printf("%s\n",$1)
else
   printf("%s,",$1)

} ' cnt=7 myrec="$norec" vlud.out

Check out this link on how to pass shell variables to awk:

http://www.tek-tips.com/faqs.cfm?fi...

BTW, sed can remove the last comma of the last line of a
file:

sed '$s/,$//' datafile


0

Response Number 5
Name: balmbkm
Date: May 2, 2009 at 19:04:42 Pacific
Reply:

Great. Thank again


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

Remove 'blanks' in a file... how to fetch the fileds f...


Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Need help printing in awk using

Help in AWK www.computing.net/answers/unix/help-in-awk/8018.html

NEED TO PRINT HTML IN UNIX! HELP! www.computing.net/answers/unix/need-to-print-html-in-unix-help/2834.html

URGENT! help with sorting in awk www.computing.net/answers/unix/urgent-help-with-sorting-in-awk/4442.html