String formatting using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String formatting using AWK
# 1  
Old 04-01-2010
String formatting using AWK

Hi,
I need to insert a line at a particular line number. I am using the below code:

Code:
sed $REV_LINO_NO" i\\
# $CURRENT_DATE $NAME           Changed pwd for cindy\'s id" file > file1

This code works, but the formatting is not as I expected. For example, I get lines as shown below according to the length of the variable '$NAME'

Code:
# 20100401 Sudha           Changed pwd for cindy's id
# 20100401 Sudhaaaaaa       Changed pwd for cindy's id
# 20100401 Su           Changed pwd for cindy's id

I have to format the line that I am inserting as shown below:
Code:
20100401 Sudha        Changed pwd for cindy's id
20100401 Sudhaaaaaa   Changed pwd for cindy's id
20100401 Su           Changed pwd for cindy's id

i.e I want the 4th feild, "Changed pwd for cindy's id" to start at a fixed place. I tried something with awk as shown below:

Code:
echo hi hellll hi | awk '{printf "# %s %s %30s\n", $1,$2,$3}'

but this does not work as I expect.

Your input will be of great help.

Thanks,
Sugan.
# 2  
Old 04-01-2010
Quote:
Originally Posted by sugan
...
Code:
echo hi hellll hi | awk '{printf "# %s %s %30s\n", $1,$2,$3}'

but this does not work as I expect.

...
Code:
$
$
$ echo hi hellll hi | awk '{printf "# %s %s %30s\n", $1,$2,$3}'
# hi hellll                             hi
$
$

Well, what did you expect ?

tyler_durden

Not sure if awk allows you to print like that without typing all fields explicitly; maybe you could use the Bash shell:

Code:
$
$
$ cat f4
# 20100401 Sudha           Changed pwd for cindy's id
# 20100401 Sudhaaaaaa       Changed pwd for cindy's id
# 20100401 Su           Changed pwd for cindy's id
$
$ while read hsh num name txt
> do
>   printf "%s %s %-30s %-s\n" $hsh $num $name "$txt"
> done <f4
# 20100401 Sudha                          Changed pwd for cindy's id
# 20100401 Sudhaaaaaa                     Changed pwd for cindy's id
# 20100401 Su                             Changed pwd for cindy's id
$
$

tyler_durden

Last edited by durden_tyler; 04-01-2010 at 11:47 AM..
# 3  
Old 04-01-2010
Great! It works. Thank you Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

2. UNIX for Dummies Questions & Answers

awk formatting

Hi all, I'm writing a simple awk code: awk 'BEGIN {FS="|"};{print "Type\tNumber\ttypes\tTotal";};{print $1, "\t", $2, "\t", $3, "\t", $4, "\t";}' db_query.txt it gives me the result: Type Number types Total XXX 498.0 5100.0 5274.661 Type Number types Total... (7 Replies)
Discussion started by: messi777
7 Replies

3. Shell Programming and Scripting

formatting awk

when i try this awk its giving out put as below. awk '!(/^$/||/--/||/selected/||/^ *$/){print $1}' tmp.txt output ===== 1 2010-08-03-12.31.26.126000 how excluede the 1st line ? i mean i want output only 2nd line i.e 2010-08-03-12.31.26.126000; (5 Replies)
Discussion started by: rocking77
5 Replies

4. Shell Programming and Scripting

AWK formatting help.

Dear all I require help with AWK regarding this situation Input is : fn1 12345 fn1 23456 fn3 231513 fn1 22325 fn3 123125 Desired output is fn1 12345 23456 22325 fn3 231513 123125 (5 Replies)
Discussion started by: Peasant
5 Replies

5. UNIX for Dummies Questions & Answers

string formatting

how can I format this so the output looks like so: 811420 -rwx------ 1 cw33221 student 8952 Jan 26 2011 a.out I keep getting this 811420 -rwx------ 1 cw33221 student 8952 Jan 26 17:31 a.out Here's the code I want to minuplate: printf( "%d\t" , info_p->st_ino); (4 Replies)
Discussion started by: l flipboi l
4 Replies

6. Shell Programming and Scripting

Formatting a string

hi, i am relatively new to unix. i have a doubt i have a string in the format "2010-10-27 15:03:56" i need to format that to "10/27/2010 15:03:56" this format how to do that in unix script ? any help is appreciated.. (7 Replies)
Discussion started by: mssajith
7 Replies

7. Shell Programming and Scripting

String formatting using awk printf

Hi Friends, I am trying to insert lines of the below format in a file: # x3a4914 Joe 2010/04/07 # seh Lane 2010/04/07 # IN01379 Larry 2010/04/07 I am formatting the strings as follows using awk printf: awk 'printf "# %s %9s %18s\n", $2,$3,$4}' ... (2 Replies)
Discussion started by: sugan
2 Replies

8. Shell Programming and Scripting

Help formatting a string. Something like printf?

Hi I'm having a problem with converting a file: ID X 1 7 1 8 1 3 2 5 2 7 2 2 To something like this: ID X1 X2 X3 1 7 8 3 2 5 7 2 I've tried the following loop: for i in `cat tst.csv| awk -F "," '{print $1}'| uniq`;do grep -h $i... (4 Replies)
Discussion started by: flotsam
4 Replies

9. Shell Programming and Scripting

SH string formatting

Hi all, my syslog.txt is a file like this: Jan 20 10:55:19 cerbero | process1: process1.--: |Info |xyxyxyxyxyxyxx | Jan 20 10:59:45 cerbero | processlong2 : threadlong1.0 : |Info |ughxueeughxueeuxexuexueuxexuexue | I need a .sh script to print to screen something like this. Jan... (3 Replies)
Discussion started by: ric79
3 Replies

10. Shell Programming and Scripting

awk printf formatting using string format specifier.

Hi all, My simple AWK code does C = A - B If C can be a negative number, how awk printf formating handles it using string format specifier. Thanks in advance Kanu :confused: (9 Replies)
Discussion started by: kanu_pathak
9 Replies
Login or Register to Ask a Question