Question about formatting output using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question about formatting output using AWK
# 1  
Old 07-09-2008
Question about formatting output using AWK

Hi everyone.

I've got this fille...

Code:
0
5000001
5000002
5000003
5000006
5000007
5000219
11000156
11003130
11003132

and this script...

Code:
#!/bin/ksh

FILE_ALERT_CONTACT_LIST=users.txt

userID=`awk -F"=" '{printf $1 ", "}' $FILE_ALERT_CONTACT_LIST`

echo "User is: " $userID

which outputs the following...

0, 5000001, 5000002, 5000003, 5000006, 5000007, 5000219, 11000156, 11003130, 11003132,

My question is, how do I remove the last comma? I'd like it to look like this...

0, 5000001, 5000002, 5000003, 5000006, 5000007, 5000219, 11000156, 11003130, 11003132

Thanks!
# 2  
Old 07-09-2008
Code:
awk '{
    if (rec)
       printf("%s, ", rec)
    rec = $0
} END {
    print rec
}' file

# 3  
Old 07-09-2008
Thanks for your help shamrock. Your script worked fine.

However my file has changed to the following...

Code:
0=Administrator
5000001=user1
5000002=user2
5000003=user3
5000006=user4
5000007=user5
5000219=user6
11000156=user7
11003130=user8
11003132=user9

so when I rec = $0 to rec = $1 I loose the first row. This is what I get...

5000001, 5000002, 5000003, 5000006, 5000007, 5000219, 11000156, 11003130, 11003132

Why am I loosing the first row?
# 4  
Old 07-09-2008
Quote:
Originally Posted by Fatbob
..so when I rec = $0 to rec = $1 I loose the first row. This is what I get...
....
Why am I loosing the first row?
Why you change rec value from $0 to $1? Smilie
# 5  
Old 07-09-2008
Code:
awk -F= 'NR==1{printf("%s",$1);next}{printf(", %s",$1)}' file

# 6  
Old 07-09-2008
awk -F"=" 'NR==1{printf $1} NR!=1{printf " ,"$1}' abc.txt
# 7  
Old 07-09-2008
Quote:
Originally Posted by Fatbob
Thanks for your help shamrock. Your script worked fine.

However my file has changed to the following...

Code:
0=Administrator
5000001=user1
5000002=user2
5000003=user3
5000006=user4
5000007=user5
5000219=user6
11000156=user7
11003130=user8
11003132=user9

so when I rec = $0 to rec = $1 I loose the first row. This is what I get...

5000001, 5000002, 5000003, 5000006, 5000007, 5000219, 11000156, 11003130, 11003132

Why am I loosing the first row?
That's because when variable rec is zero the if statement evaluates to false and the printf() is skipped. Change it instead to test if the string is null.

Code:
awk '{
    if (rec"")
       printf("%s, ", rec)
    rec = $1
} END {
    print rec
}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in formatting output in sed / awk

I have a file like this : ! 1 ! 542255 ! 50,140.00 ! ! 2 ! 551717 ! 5,805.00 ! ! 3 ! 551763 ! 8,130.00 ! ! 4 ! 551779 ! 750.00 ! ! 5 ! 551810 ! 56,580.00 ! ! 6 ! 551816 ! 1,350.00 ! ! 7 ! 551876 ! 360.00 ! ! 8 ! 551898 ! ... (10 Replies)
Discussion started by: adam1969in
10 Replies

2. Shell Programming and Scripting

awk Script Output in Outlook Formatting Issue

When i execute the below shell script with 2 different Input files, for one of the data files (datafile1) my email message body in the outlook messes up and every thing comes up in one line. May i please know what i am doing wrong here or how to fix this? The only difference in data files is one is... (1 Reply)
Discussion started by: Ariean
1 Replies

3. Shell Programming and Scripting

Formatting output using awk in ksh

:oi was trying to write a script to format output of a command in ksh which has output as below: so i used : to get which i require at all times. But problem occurs when status part changes. above output i get when status is SU (success).If the status is IN (inactive), output of... (1 Reply)
Discussion started by: pr5439
1 Replies

4. Shell Programming and Scripting

Formatting grep and awk output

Hi there. I have a very large file and I am trying to format it so that I can pull out certain pieces of data/info and report it in spreadsheet format/style. The file has ###### which will separate each line that will be listed in the spreadsheet. Whenever I find "No" at the end of a line I want... (7 Replies)
Discussion started by: kieranfoley
7 Replies

5. Shell Programming and Scripting

awk question : system output to awk variable.

Hi Experts, I am trying to get system output to capture inside awk , but not working: Please advise if this is possible : I am trying something like this but not working, the output is coming wrong: echo "" | awk '{d=system ("date") ; print "Current date is:" , d }' Thanks, (5 Replies)
Discussion started by: rveri
5 Replies

6. Shell Programming and Scripting

Formatting of output from awk

Hi; i m running an command like; getfacl $(find /home/fstl/testShell/ -type f -ls | awk '{print $11}') and i m getting outpiut for all files in "testShell" folder as; # file: home/fstl/testShell/.script1.sh.swp # owner: root # group: root user:fstl:rw- user:ajay:rw- group:root:r--... (1 Reply)
Discussion started by: ajaypadvi
1 Replies

7. Shell Programming and Scripting

output formatting with awk.

# echo $PATH /usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin How would i write a script to display permission on each folders in $PATH variable below format. drwxr-xr-x 2 0 root 4096 Nov 24 14:51 /usr/kerberos/sbin drwxr-xr-x 2 0... (2 Replies)
Discussion started by: pinga123
2 Replies

8. Shell Programming and Scripting

formatting output

my script is as follows cnt=`ps -ef |grep pmon|grep -v grep|awk 'END {{print NR}}'` cnt2=`ps -ef |grep tns|grep -v grep|awk 'END {{print NR}}'` if then if then rman target/ catalog recdb/recdb@recdb cmdfile report_need_backup.sql > report_need_backup.txt ... (1 Reply)
Discussion started by: swkambli
1 Replies

9. Shell Programming and Scripting

formatting the output

Is it possible to convert the attached file to the format mentioned. Here for a particular job the table name and the corresponding instance name from one test run "X" is compared with the table name and the instance name from the second test run "Y" for output rows,affected rows,applied... (1 Reply)
Discussion started by: ragavhere
1 Replies

10. UNIX for Dummies Questions & Answers

Question about formatting the output

I need to ask a question on how to format the output in a csv format. Right now i am running a shell script which executes a command and the following output is append through a unix script in a .csv file. So the output of xyz.csv is as follow :- 1. Number = 25 Amount $84,132.22 2.... (1 Reply)
Discussion started by: chris1234
1 Replies
Login or Register to Ask a Question