formatting textfile inside ksh script using awk not working


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users formatting textfile inside ksh script using awk not working
# 1  
Old 07-01-2007
formatting textfile inside ksh script using awk not working

I cannot seem to get this text file to format. Its as if the awk statement is being treated as a simple cat command.
I manned awk and it was very confusing. I viewed previous posts on this board and I got the same results as with the
the awk command statement shown here. Please help.

<<<contents of msgfile.txt>>>
$ cat msgfile.txt
Firstname1,Lastname1,Initial
Firstname2,Lastname2,Initial2
Firstname3,Lastname3,Initial3
Firstname4,Lastname4,Initial4
Firstname5,Lastname5,Initial5


<<<program code - this needs to be within ksh>>>
#!usr/bin/ksh




awk -F":" '{ print $1 "\t" $2 "\t" $3 }' msgfile.txt

<<<script output>>>
$ ksh tmail2.ksh
Firstname1,Lastname1,Initial
Firstname2,Lastname2,Initial2
Firstname3,Lastname3,Initial3
Firstname4,Lastname4,Initial4
Firstname5,Lastname5,Initial5
# 2  
Old 07-01-2007
Quote:
awk -F":" '{ print $1 "\t" $2 "\t" $3 }' msgfile.txt

should be,

Code:
awk -F"," '{ print $1 "\t" $2 "\t" $3 }' msgfile.txt

because the columns are seperated by a comma.

Output:

Quote:
Firstname1 Lastname1 Initial
Firstname2 Lastname2 Initial2
Firstname3 Lastname3 Initial3
Firstname4 Lastname4 Initial4
Firstname5 Lastname5 Initial5
kamitsin
# 3  
Old 07-01-2007
Since i like using printf for formatting in awk , you can try this also,

Code:
awk -F"," '{printf"%-20s %-20s %-20s\n",$1,$2,$3}' msgfile.txt

Output:

Quote:
Firstname1 Lastname1 Initial
Firstname2 Lastname2 Initial2
Firstname3 Lastname3 Initial3
Firstname4 Lastname4 Initial4
Firstname5 Lastname5 Initial5
kamitsin
# 4  
Old 07-01-2007
Or:

Code:
{ IFS=',';printf "%s\t" $(<infile);printf "\n";}

# 5  
Old 07-01-2007
Code:
# awk 'BEGIN{FS=",";OFS="\t"}{print $1,$2,$3}' file
Firstname1      Lastname1       Initial
Firstname2      Lastname2       Initial2
Firstname3      Lastname3       Initial3
Firstname4      Lastname4       Initial4
Firstname5      Lastname5       Initial5

# 6  
Old 07-03-2007
Thanks for all the help! It works now.
# 7  
Old 07-03-2007
Another way though your problem is solved using awk

Code:
$ tr ',' '\t' < msgfile.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nslookup on a variable inside script not working

I am new at scripting. I have a file that each line is the shortname for a server. ie - Server1 Server2 Server3 I want to read in that file, do a nslookup and print the FQDN to a file. I added an echo statement to make sure my variable was being read and changing. But the nslookup... (5 Replies)
Discussion started by: kellyhkc
5 Replies

2. Shell Programming and Scripting

Expect not working inside my Bash script

I am trying to execute expect command inside by small bash script to login into servers using key authentication method. My script is as follows: #!/bin/bash HOST=$1 /usr/bin/expect -c " spawn ssh -i /root/.ssh/id_rsa root@$HOST expect -exact "Enter... (3 Replies)
Discussion started by: John Wilson
3 Replies

3. Shell Programming and Scripting

awk working inside specific pattern ranges

Hi, I have a text file, which I am trying to parse. File contents: BEG Id Job1 Id Stage1 1 EN Id Job2 Id Stage2 BEG Id2 Job3 Id Stage4 2 EN I have to process the data in this between every BEG and EN. so I am trying to restrict the range and inside every... (1 Reply)
Discussion started by: Kulasekar
1 Replies

4. Shell Programming and Scripting

sed command not working inside ksh script but works fine outside

Hi, I am a bit confused ,why would a sed command work fine outside of ksh script but not inside. e.g I want to replace all the characters which end with a value and have space at end of it. so my command for it is : sed -i "s/$SEPARATOR /$SEPARATOR/g" file_name This is working fine in... (8 Replies)
Discussion started by: vital_parsley
8 Replies

5. Ubuntu

Top output to textfile not working in cronjob

Hello, If i run the following script from the cmdline it works just fine however if i run it via root cronjob it doesn't write to the file at all. It is chmod 777 now so shouldnt be any right issues. Shellscript looks like this: /usr/bin/top -b -n 1 -u nagios >> /var/log/test.log Cronjob... (7 Replies)
Discussion started by: heady89
7 Replies

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

7. Shell Programming and Scripting

expect script inside shell script not working.

Shell Scipt: temp.sh su - <$username> expect pass.exp Expect script: pass.exp #!/usr/bin/expect -f # Login ####################### expect "Password: " send "<$password>\r" it comes up with Password: but doesnt take password passed throguh file. (2 Replies)
Discussion started by: bhavesh.sapra
2 Replies

8. Shell Programming and Scripting

How to call an sql script inside a while statement in KSH

Hi all, I'm trying to run an sql inside a loop which looks like this #!bin/ksh while IFS=, read var1 var2 do sqlplus -s ${USERNAME}/${PASSWORD}@${ORACLE_SID} << EOF insert into ${TABLE} ( appt_date ) values ( '${var1 }' ); ... (6 Replies)
Discussion started by: ryukishin_17
6 Replies

9. Shell Programming and Scripting

Getting cntrl-c entered inside a ksh script

hi guys, my ksh script is calling another script. The other script expects user to press CNTR-C, and does not return to the prompt. in my script, I want to call the other script, but somehow don't want it to wait forever, I want to return to my script. e.g. script2.ksh outputs: "No... (2 Replies)
Discussion started by: JamesByars
2 Replies

10. Shell Programming and Scripting

expr inside a ksh script Solaris

Hi; If I do something like this. dftotalsize=0;export dftotalsize;df -k | grep \/db001 | awk '{print $4}' | while read theinput \ ; do export $theinput; dftotalsize=`expr $dftotalsize + $theinput`; export dftotalsize; echo $dftotalsize; done ; echo `expr $dftotalsize \/ 1024 \/ 1024 "GB" Is... (4 Replies)
Discussion started by: myjess
4 Replies
Login or Register to Ask a Question

Featured Tech Videos