SharkM
January 23, 2008, 8:52am
1
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.
Do you mean something like this?
% while read a b c d e j;do printf "%10s\n" "$e";done<file
99960.7
100126.7
100214.5
Or:
% awk '{printf "%10s\n",$5}' file
99960.7
100126.7
100214.5
Oops, just saw the ORS:
% paste -sd, <( printf "%10s\n" $(cut -d" " -f5 file))
99960.7, 100126.7, 100214.5
SharkM
January 23, 2008, 10:04am
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
?
I'll give you both (right and left):
% 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