The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 12-04-2008 04:04 PM
sed replace with fixed length McLan Shell Programming and Scripting 7 05-15-2008 02:57 AM
Awk with fixed length files c2b2 Shell Programming and Scripting 7 01-06-2007 12:57 PM
fixed length fields in awk roopla Shell Programming and Scripting 2 11-13-2006 09:12 PM
creating a fixed length output from a variable length input r1500 Shell Programming and Scripting 2 12-03-2003 01:09 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-27-2006
george_ george_ is offline
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.
  #2 (permalink)  
Old 03-27-2006
gauravgoel gauravgoel is offline
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
  #3 (permalink)  
Old 03-27-2006
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
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
  #4 (permalink)  
Old 03-27-2006
george_ george_ is offline
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.
  #5 (permalink)  
Old 03-27-2006
gauravgoel gauravgoel is offline
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
  #6 (permalink)  
Old 03-27-2006
george_ george_ is offline
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0