Unix/Linux Go Back    


UNIX Desktop Questions & Answers Discuss UNIX and Linux user interfaces like GNOME, KDE, CDE, and Open Office here. All UNIX and Linux Newbies Welcome !!

Help with Fixed width File Parsing

UNIX Desktop Questions & Answers


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-29-2008   -   Original Discussion by sate911
sate911 sate911 is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 29 April 2008, 12:00 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Help with Fixed width File Parsing

I am trying to parse a Fixed width file with data as below. I am trying to assign column values from each record to variables. When I parse the data, the spaces in all coumns are dropped. I would like to retain the spaces as part of the dat stored in the variables. Any help is appreciated.

I would like to redirect this data into a csv file like below for each record
2,6138023380020080422120104828,60061395297943,0000006824, ,C, , ,POS .......etc

Input File

26138023380020080422120104828600613952979430000006824C POS13MMAHONIA NANCY 4189928286000422120104CO OP 2529 HOLIDAY FLCO-OP #2529

My Script:

while read LINE
do
a=`echo "$(echo $LINE |cut -c1-1)"`
b=`echo "$(echo $LINE |cut -c2-32)"`
c=`echo "$(echo $LINE |cut -c33-43)"`
d=`echo "$(echo $LINE |cut -c44-53)"`
e=`echo "$(echo $LINE |cut -c54-55)"`
f=`echo "$(echo $LINE |cut -c55-74)"`
#echo $a,$b,$c,$d,$e,"$f >> Int_4_final.dat
done < $1
Sponsored Links
    #2  
Old Unix and Linux 05-19-2008   -   Original Discussion by sate911
divh18 divh18 is offline
Registered User
 
Join Date: May 2008
Last Activity: 19 May 2008, 4:24 PM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,
I would put a space after each )

Quote:
Originally Posted by sate911 View Post

a=`echo "$(echo $LINE |cut -c1-1)"`
Sponsored Links
    #3  
Old Unix and Linux 05-19-2008   -   Original Discussion by sate911
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
You need to use proper quoting to preserve spaces in variables. See UNIX Shell Quote
(via Answers to Frequently Asked Questions - The UNIX Forums)
    #4  
Old Unix and Linux 05-19-2008   -   Original Discussion by sate911
joeyg's Unix or Linux Image
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 11 November 2017, 3:20 PM EST
Location: Within two miles of a Dunkin donuts.
Posts: 2,466
Thanks: 131
Thanked 194 Times in 175 Posts
Hammer & Screwdriver Double-quote the variable

Here are a couple of examples from one of my working scripts.


Code:
   year=$(echo "$zf" | cut -c185-188)
   car=$(echo "$zf" | cut -c189-230)



The key is the variable zf is in double-quotes, and thus maintains all spacing.
Sponsored Links
    #5  
Old Unix and Linux 05-19-2008   -   Original Discussion by sate911
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 11 Times in 9 Posts
If the result contains spaces, you will need to quote the backticks, too.


Code:
year="$(echo "$zf" | cut -c185-188)"
car="$(echo "$zf" | cut -c189-230)"

echo `echo` as seen above is obviously a redundant, Useless Use of Echo in Backticks.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Comparing two fixed width file anshul_er Shell Programming and Scripting 10 05-19-2011 01:59 AM
sorting a fixed width seq file issaq84mohd Shell Programming and Scripting 5 12-28-2010 02:01 AM
Fixed-Width file from Oracle Amit.Sagpariya Shell Programming and Scripting 5 04-20-2010 08:44 AM
Changing particular field in fixed width file dsravan Shell Programming and Scripting 4 02-11-2008 06:08 PM
Fixed Width file using AWK alok.benjwal UNIX for Dummies Questions & Answers 2 12-05-2005 11:39 AM



All times are GMT -4. The time now is 05:25 PM.