formatting differently in last loop


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers formatting differently in last loop
# 1  
Old 10-03-2008
formatting differently in last loop

hi. i'm trying to create a list users that is formatted for an SQL query. it should take the form:

Code:
'user1',
'user2',
'user3'

as you can see, there is no comma after the last value. could someone help me revise my code to do this?


Code:
  while read user; do
    print "'"$userid"'," >> formatted_user_list
  done < user_list

thanks for your help!
# 2  
Old 10-03-2008
Oops, misread question ...Smilie

Code:
awk -v q=\' 'NR>1{print s}{s=q$0q","}END{print q$0q}' user_list > formatted_user_list

for older awk,

Code:
awk -v c="$(wc -l <user_list)" -v q=\' 'NR>1{print s}{s=q$0q","}NR==c{print q$0q}' user_list > formatted_user_list


Last edited by rubin; 10-03-2008 at 08:53 PM.. Reason: Oops, misread question
# 3  
Old 10-03-2008
i think you can use this also

for i in `cat user_list` do echo "'"$i"'," >> formatted_user_list;done
# 4  
Old 10-03-2008
thanks, pistachio! much simpler code.

i had to tweak it slightly by adding a semi-colon:

Code:
for i in `cat user_list`; do echo "'"$i"'," >> formatted_user_list; done

but it still doesn't answer my question about removing the "," from the last entry. do i need to use something like sed or awk?

thanks
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why awk perform differently when using variable?

Hi Gurus, I hit a hard block in my script. when using awk command with variable, I got different result. Please see below: my test file as below: $ cat demofile.txt filename-yyyy-abcd filename-xxx-week-pass filename-xxx-week-run for testing purpose, I put 3 awk command in one script.... (7 Replies)
Discussion started by: ken6503
7 Replies

2. Shell Programming and Scripting

su - user -c 'command' behaves differently

I notice that su - user (note with dash) brings in more of the user's environment than does su - user -c 'command'. For example, if root does an su - user, and types "umask" to the prompt, one umask is displayed; yet, if instead the command is su - user -c 'umask', the value is different. I thought... (2 Replies)
Discussion started by: drokerm
2 Replies

3. Shell Programming and Scripting

sed and cut behaving differently

I have attached a file with few records. First 2 characters of each record are binary characters. I can remove it by and it works fine. But is behaving differently and removing more than expected characters. Can someone help me in accomplishing it through sed? Thanks in advance. (13 Replies)
Discussion started by: amicon007
13 Replies

4. Solaris

swap displays differently

A new x86 server was installed with 16G of memory. The swap space assigned in the prtvtoc is also 16G. But after the installation of the OS and verifying, noticed df -k output for swap shows as 30G. Other systems do not have this characteristic. Whats wrong in here?:eek: (4 Replies)
Discussion started by: incredible
4 Replies

5. Shell Programming and Scripting

Why does IF loop behave differently in different shells

Hi , I have a script that compares two string and prints the larger string , This is an extract of a biggers script that i have. #! /bin/ksh DT_STRING_CMP=20081221223440 DT_STRING=20071221223440 if ; then echo "20081221223440" fi echo... (5 Replies)
Discussion started by: amit1_x
5 Replies

6. Shell Programming and Scripting

Why does IF loop behave differently in different shells

Hi , I have a script that compares two string and prints the larger string , This is an extract of a biggers script that i have. #! /bin/ksh DT_STRING_CMP=20081221223440 DT_STRING=20071221223440 if ; then echo "20081221223440" fi echo... (1 Reply)
Discussion started by: amit1_x
1 Replies

7. Shell Programming and Scripting

Paste the files contents differently

Hello , I want to segregate by file contents in a much simpler format ie input file Wednesday May 16 11:59:44 IST 2007 90376 44136 process1 pid1 90200 43208 process1 pid2 90200 43208 process1 pid3 90200 43208 process1 pid4 Wednesday May 16 12:00:45 IST 2007 90376 44136 process1 pid1... (1 Reply)
Discussion started by: er_aparna
1 Replies

8. Shell Programming and Scripting

Script behaving differently in Crontab..

Hi, I wrote a script to stop a process,truncate its log files and re-start the process... We are using Progress Software in Unix ( Sun Sparc) When ever I start this progress program , it should kick off a C pgm in the background.. The script work perfectly fine when I run it from command... (4 Replies)
Discussion started by: newtoxinu
4 Replies

9. UNIX for Advanced & Expert Users

Script behaving differently in Crontab..

I posted this in Shell scripting... maybe I'll try it in this forum.. ***************** I wrote a script to stop a process,truncate its log files and re-start the process... We are using Progress Software in Unix ( Sun Sparc) When ever I start this progress program , it should kick off a... (1 Reply)
Discussion started by: newtoxinu
1 Replies

10. Shell Programming and Scripting

Why does cron run this differently?

test.ksh =================== #!/usr/bin/ksh APPLICATION=hr_app APPLICATION_UPPER=`echo $APPLICATION | tr ` echo $APPLICATION_UPPER > /tmp/test.txt echo $APPLICATION >> /tmp/test.txt which tr >> /tmp/test.txt =================== When I run this from the shell: /home/natter> more... (6 Replies)
Discussion started by: natter
6 Replies
Login or Register to Ask a Question