The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Special Forums > UNIX Desktop for Dummies Questions & Answers
Google UNIX.COM


UNIX Desktop for Dummies Questions & Answers Questions regarding GNOME, KDE, CDE, Open Office, etc go here. All UNIX and Linux Newbies Welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Changing particular field in fixed width file dsravan Shell Programming and Scripting 4 02-11-2008 02:08 PM
Extracting records with unique fields from a fixed width txt file sitney Shell Programming and Scripting 8 02-09-2008 11:18 PM
Converting a Delimited File to Fixed width file raghavan.aero Shell Programming and Scripting 2 06-06-2007 11:44 AM
adding delimiter to a fixed width file sumeet Shell Programming and Scripting 2 03-21-2007 06:19 AM
Fixed Width file using AWK alok.benjwal UNIX for Dummies Questions & Answers 2 12-05-2005 07:39 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-28-2008
Registered User
 

Join Date: Apr 2008
Posts: 1
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-18-2008
Registered User
 

Join Date: May 2008
Posts: 7
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hi,
I would put a space after each )

Quote:
Originally Posted by sate911 View Post

a=`echo "$(echo $LINE |cut -c1-1)"`
Reply With Quote
  #3 (permalink)  
Old 05-18-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
You need to use proper quoting to preserve spaces in variables. See UNIX Shell Quote
(via Answers to Frequently Asked Questions - The UNIX Forums)
Reply With Quote
  #4 (permalink)  
Old 05-19-2008
joeyg's Avatar
premier etoile de match
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 394
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Wink 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.
Reply With Quote
  #5 (permalink)  
Old 05-19-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 2,203
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
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.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:23 AM.


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

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101