Expand string using nawk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Expand string using nawk
# 1  
Old 12-21-2012
Expand string using nawk

Hello

I'm manipulating a file, taking certain variables & printing them to an output file using awk (actually nawk because I'm using -v).

I now want to take a field from the input file & expand it to a pre-defined length by preceding the string with spaces. The field I'm picking up can be of variable length.

So some possible values of the field ($29 in my line of code below) are:

5.60
7.80
12.90
121.50

The resulting field has to be 20 chars in length. The first & second fields need 16 spaces at the start, the 3rd needs 15 & the last 14.

Can I do this manipulation in one nawk statement?

I currently have this ($MON is calculated earlier in the script). I know I can use length($29) but not sure where to go from there.

Code:
cat inputfile | nawk -F= -v mondate=$MON '{ printf "02CF"mondate"%s              SPSSP%s\n",$4,$29 }' > outputfile

Am I approaching this correctly?

I guess an alternative is to process the file after I've built up all the fields I need in nawk but would be good to do it in one line if that's possible (& maintainable ...).

My OS is Solaris 10.

Thanks, Chris
# 2  
Old 12-21-2012
nawk

Hey,

To format the output in nawk, you can use printf,

Here is the sample implementation,

Code:
echo 'Input string'|nawk '{printf("%s %20s\n",$1,$2);}'

Ouput:

Input               string

If you want the strings to be left aligned, then probably you should use %-20s in printf,

Code:
echo 'Input string sample'|nawk '{printf("%s %-20s%s\n",$1,$2,$3);}'

Output:

Input string              sample

Use printf based on your requirement.

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
# 3  
Old 12-21-2012
Smilie

I never knew that syntax! Much simpler than what I'd been expecting.

Thank you
# 4  
Old 12-21-2012
And you can remove the cat inputfile - nawk '...' inputfile > outputfile will do and will do better...
# 5  
Old 12-24-2012
Smilie UUOC - yes I know, bad practice .. I just find it easier to maintain like that.

Thanks though RudiC
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

2. Shell Programming and Scripting

Adding a String after a text in a Line (using nawk)

Hello I need to add a String after a text in a line. The Unix file is huge and I think nawk would be quick. Current: -name FILTER -node 60265 -cob 31/01/2013 -risktype + -change 1 -filter ALL_NODES -toponly -warnings OFF -delimiter "|" -noheader -select... (4 Replies)
Discussion started by: filter
4 Replies

3. AIX

Expand LV

Under the df command, one of my logical volumes, /dev/hd4, was showing 100% Used, so I added 2 logical partitions, but its still showing 100% used. Is there a separate command I need to execute to reallocate? (5 Replies)
Discussion started by: markper
5 Replies

4. Shell Programming and Scripting

Nawk - print only a string containing a regex.

Hi again, I'm looking for some help with nawk, I can print a line which has a regex match in it from a file using /pattern/ but I'm looking for a way to only print the $tring which contains the pattern, rather than the whole line. This $tring may be of variable length, may occur at any point... (1 Reply)
Discussion started by: spynappels
1 Replies

5. Shell Programming and Scripting

NAWK: changing string-format with split

Hi all, I try to make a awk-script, which counts lines, summarized by pdf and xml. So far it works, but for sorting reasons, I'd like to change the format from the field $1 from dd-mm-yyyy to yyyy-mm-dd. This works find, but: split() and sprintf() prints its output (for no reason, the results... (2 Replies)
Discussion started by: regisl67
2 Replies

6. UNIX for Dummies Questions & Answers

How to expand who command?

When I do the who command it doesn't show all my info: $w gscn 6:08PM up 4 days, 20:33, 177 users, load averages: 7.46, 3.78, 3.43 USER TTY FROM LOGIN@ IDLE WHAT gscn R1 pool-92-199-17-1 5:46PM - w gscn Like in the 'From' column.... (8 Replies)
Discussion started by: guitarscn
8 Replies

7. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

8. Shell Programming and Scripting

expand logic for > and <

Hi Input A12345678901234567890 < A12345678901234567890 AND C12345678901234567890 < D12345678901234567890 AND E12345678901234567890 > F12345678901234567890 If the length of the line at any point exceed more than 60 chars it should come to next line but it should not break a variable... (0 Replies)
Discussion started by: pbsrinivas
0 Replies

9. UNIX for Advanced & Expert Users

Terminal Expand

hi guys. i have written a script/program and i would strongly prefer if this script automatically expands the user's terminal whenever he or she starts the script. this is so the script/program can fit on the user's screeen without user having to manually drag and spread his screen. what... (2 Replies)
Discussion started by: Terrible
2 Replies
Login or Register to Ask a Question