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
sed replace with fixed length McLan Shell Programming and Scripting 7 05-14-2008 11:57 PM
What the command to find out the record length of a fixed length file? tranq01 UNIX for Dummies Questions & Answers 3 10-19-2007 11:16 AM
Awk with fixed length files c2b2 Shell Programming and Scripting 7 01-06-2007 09:57 AM
fixed length fields in awk roopla Shell Programming and Scripting 2 11-13-2006 06:12 PM
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 03-27-2006
Registered User
 

Join Date: Mar 2006
Posts: 16
fixed record length

hello!

I have a file with fixed record length...

format:

123445asdfg 4343777 sfgg

I wanna convert it to

123445,asdfg ,4343,777 ,sfgg

is there any way to do it?

sed/grep/awk??

at the moment I use sed -e 's_ \([^ ]\)_,\1_g'
but it works only if there are spaces between records...

any idea to deal with??

thanks for any help.
Reply With Quote
Forum Sponsor
  #2  
Old 03-27-2006
Registered User
 

Join Date: Dec 2005
Location: India
Posts: 218
it seems you know awk,
so you can use substr function of awk,
do man awk for more details

Gaurav
Reply With Quote
  #3  
Old 03-27-2006
Klashxx's Avatar
HP-UX/Linux/Oracle
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 383
Code:
echo "123445asdfg 4343777 sfgg"|awk '{print substr($1,1,6)","substr($1,7,11)" ,"substr($2,1,4)","substr($2,5,7)" ,"$3}'

123445,asdfg ,4343,777 ,sfgg
Reply With Quote
  #4  
Old 03-27-2006
Registered User
 

Join Date: Mar 2006
Posts: 16
thanks a lot!

is there a more generic way to do it?
instead of $1/$2....to parse the whole line?
becouse like this I will have to change the script each time I use it for another file.

thanks for any advise.
Reply With Quote
  #5  
Old 03-27-2006
Registered User
 

Join Date: Dec 2005
Location: India
Posts: 218
if you are sure that the no. of characters in the string is going to be constant, then you can first use sed to remove the blank spaces fron it and then use the awk substr() fn to print the required output.

Quote:
sed 's/[ ]*//g' filename > newfile
awk '{print substr(....)}' newfile
post back if still some confusion
Gaurav
Reply With Quote
  #6  
Old 03-27-2006
Registered User
 

Join Date: Mar 2006
Posts: 16
it doesn't solve the problem...

in each file I have to change
substr parameters
instead of substr($1,7,11)" ,"substr($2,1,4)","substr($2,5,7)"

I want substr($the_whole_line,7,11),substr($the_whole_line,12,17)

ls that possible?

thanks.
Reply With Quote
  #7  
Old 03-27-2006
Registered User
 

Join Date: Mar 2006
Posts: 16
fields in a record which is lets say 30 chars another is 40 may contain spaces...
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 10:22 PM.


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