The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 7
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.
Reply With Quote
Forum Sponsor
  #2  
Old 01-23-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
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
Or:

Code:
% awk '{printf "%10s\n",$5}' file
   99960.7
  100126.7
  100214.5
Oops, just saw the ORS:

Code:
% paste -sd, <( printf "%10s\n" $(cut -d" " -f5 file))
   99960.7,  100126.7,  100214.5
Reply With Quote
  #3  
Old 01-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 7
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
Reply With Quote
  #4  
Old 01-23-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
?

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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:30 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0