|
|
|
|
|||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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. |
![]() |
| Viewing: Web Development Archives > FAQs > Unix/Linux > Read file and break the string line |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|