Unix/Linux
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives FAQs Unix/Linux

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old July 30th, 2008, 01:01 AM
Beto
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Read file and break the string line

Hi All,

I have a file with several lines and i would like to read it and break
the lines string in many parts and pass it as parameter and also skip
the first and the last line, as example.

File example.txt
1 #first line
XX12345678ABCDEYYYYY
XX99999999KKKKKMMMM
3 #last line

NUMBER=12345678
LETTERS=ABCDE
YEAR=YYYYY

and next

NUMBER=99999999
LETTERS=KKKKK
YEAR=MMMM


while read file; do
break the strings
NUMBER=12345678
LETTERS=ABCDE
YEAR=YYYYY
done < example.txt

Thanks in advance

Reply With Quote
  #2  
Old July 30th, 2008, 07:19 AM
amateur
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Read file and break the string line

Considering that the data has fixed length for the desired fields,
this script, though very crude, should work fine:-

#!/bin/bash

LINES=$(( `cat testdata | wc -l` - 1 ))
for i in `cat testdata | sed -n "2,$LINES p"`
do
echo -ne "$i\t"
NUMBER=`echo ${i:2:8}`
LETTERS=`echo ${i:10:5}`
YEAR=`echo ${i:15:4}`
echo -e "$NUMBER\t$LETTERS\t$YEAR"
done

Reply With Quote
  #3  
Old July 30th, 2008, 10:01 AM
Ed Morton
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Read file and break the string line



7/29/2008 11:33 PM, Beto wrote:
Hi All,
>

I have a file with several lines and i would like to read it and break
the lines string in many parts and pass it as parameter and also skip
the first and the last line, as example.
>

File example.txt
1 #first line
XX12345678ABCDEYYYYY
XX99999999KKKKKMMMM
3 #last line
>

NUMBER=12345678
LETTERS=ABCDE
YEAR=YYYYY
>

and next
>

NUMBER=99999999
LETTERS=KKKKK
YEAR=MMMM
>
>

while read file; do
break the strings
NUMBER=12345678
LETTERS=ABCDE
YEAR=YYYYY
done < example.txt
>

Thanks in advance

awk 'NR>1{printf "%s",s;
s=sprintf("NUMBER=%s\nLETTERS=%s\nYEAR=%s\n",substr($0,3,8),substr($0,11,5),substr($0,16))}'
file

With GNU awk you could use fixed width fields in stead of ths substr()s.

Regards,

Ed.


Reply With Quote
Reply

Viewing: Web Development Archives FAQs Unix/Linux > Read file and break the string line


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

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek