![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| What the command to find out the record length of a fixed length file? | tranq01 | UNIX for Dummies Questions & Answers | 9 | 17 Hours Ago 01:04 PM |
| How to read a line and put it into 3 variables | Mandab | Shell Programming and Scripting | 8 | 04-20-2007 09:50 AM |
| fixed length fields in awk | roopla | Shell Programming and Scripting | 2 | 11-13-2006 06:12 PM |
| fixed record length | george_ | Shell Programming and Scripting | 16 | 03-28-2006 02:41 AM |
| creating a fixed length output from a variable length input | r1500 | Shell Programming and Scripting | 2 | 12-03-2003 10:09 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Read variables from line to fixed length
I would like to make a script to read three variables (no fixed length or position) from a line and write them into a file, with fixed length and right-justified in each column. The fixed text (text1-text4) prior to the thee variables and the variables themselves are originally separated by spaces only.
The input lines (LINE1) can look like this: text1 text2 text3 text4 99960.7 10.1 21.3 text1 text2 text3 text4 100126.7 5.4 4.5 text1 text2 text3 text4 100214.5 4.8 350.5 This is my script sofar: set var1 = `echo $LINE1 | awk '{print $5}'` echo $var1,$var2,$var3 >> data.out Can anyone help me to fill the columns to a fixed length (10 bytes) and the right justification? Thanks in advance. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Do you mean something like this?
Code:
% while read a b c d e j;do printf "%10s\n" "$e";done<file 99960.7 100126.7 100214.5 Code:
% awk '{printf "%10s\n",$5}' file
99960.7
100126.7
100214.5
Code:
% paste -sd, <( printf "%10s\n" $(cut -d" " -f5 file)) 99960.7, 100126.7, 100214.5 |
|
#3
|
|||
|
|||
|
I would like to have three columns of fixed width (10) with the values of the three variables justified at the right. The text can be skipped:
timestamp var1 var2 var3 timestamp var1 var2 var3 timestamp var1 var2 var3 |
|
#4
|
||||
|
||||
|
?
I'll give you both (right and left): Code:
% cat file
text1 text2 text3 text4 99960.7 10.1 21.3
text1 text2 text3 text4 100126.7 5.4 4.5
text1 text2 text3 text4 100214.5 4.8 350.5
% awk '{printf "%10s %10s %10s %10s\n", ts, $5,$6,$7}' ts="$(date +%F)" file
2008-01-23 99960.7 10.1 21.3
2008-01-23 100126.7 5.4 4.5
2008-01-23 100214.5 4.8 350.5
% awk '{printf "%-10s %-10s %-10s %-10s\n", ts, $5,$6,$7}' ts="$(date +%F)" file
2008-01-23 99960.7 10.1 21.3
2008-01-23 100126.7 5.4 4.5
2008-01-23 100214.5 4.8 350.5
|
||||
| Google The UNIX and Linux Forums |