It is currently Mon, 27 Jun 2022 12:22:35 GMT



 
Author Message
 Extract Records from a text file
Hi,

I have a large text file and I need to extract/delete records. How do I
perform following using a script?

(1) Extract lines 100 to 400, both inclusive, and create a new file.
Line ranges could be given as parms to the script.
(ii) Extract all records having the string 'xyz' between cols 8-10 and
create a new file.
(iii) Delete lines from 100-400 and replace the input file.
(iv) Delete all records having the string 'xyz' between cols 8-10 and
replace the input file.

I'm new to Unix and any help is much appreciated.

TIA
Thomas.



 Sun, 26 Aug 2007 17:08:47 GMT   
 Extract Records from a text file

Hi

'info sed' would help you with your homework.

Pip



 Sun, 26 Aug 2007 18:06:59 GMT   
 Extract Records from a text file

It's not really clear whether you want the "extracted" lines to be in
the new file or the remaining lines to be in the new file. If for items
i and ii you want the new file to just contain the selected lines, and
for items 111 and iv you want the input file to end up with the selected
lines removed, then these awk scripts should do the job:

i)
gawk -vstart=100 -vend=400 '
function printout(_str) { _out[++_nr] = _str }
function flushout(  _i) { close("newfile");
                           for (_i=1; _i<=_nr;_i++)
                                  print _out[_i] > "newfile"
                         }
NR>=start && NR<=end { printout( $0 ) }
END { flushout() }'

ii)
gawk -vpat="xyz" -vstart=8 -vend=10'
function printout(_str) { _out[++_nr] = _str }
function flushout(  _i) { close("newfile");
                           for (_i=1; _i<=_nr;_i++)
                                  print _out[_i] > "newfile"
                         }
substr($0,8,(end + 1 - start)) == pat { printout( $0 ) }
END { flushout() }'

iii)
gawk -vstart=100 -vend=400 '
function printout(_str) { _out[++_nr] = _str }
function flushout(  _i) { close(FILENAME);
                           for (_i=1; _i<=_nr;_i++)
                                  print _out[_i] > FILENAME
                         }
NR<start || NR>end { printout( $0 ) }
END { flushout() }'

iv)
gawk -vpat="xyz" -vstart=8 -vend=10'
function printout(_str) { _out[++_nr] = _str }
function flushout(  _i) { close(FILENAME);
                           for (_i=1; _i<=_nr;_i++)
                                  print _out[_i] > FILENAME
                         }
substr($0,8,(end + 1 - start)) != pat { printout( $0 ) }
END { flushout() }'

You could optimise the first 2 of them but they should get the job done.

Regards,

        Ed.



 Tue, 28 Aug 2007 04:03:31 GMT   
 Extract Records from a text file
Ed,

That was a real help for a newbie like me..
really appreciate for your time... and I get the starting point.

Many thanks.

Thomas.



 Tue, 28 Aug 2007 22:15:00 GMT   
 Extract Records from a text file

Since you asked for solutions using a script, the following are ksh93
solutions:

(1)
#!/usr/bin/ksh93
MIN=100
MAX=400
LCNT=0
while IFS="" read -r -- LINE
do
  (( ++LCNT >= MIN && LCNT <= MAX )) && print -r -- "${LINE}"
done < data.in > data.out

(ii)
#!/usr/bin/ksh93
while IFS="" read -r -- LINE
do
  [[ "${LINE:7:3}" = "xyz" ]] && print -r -- "${LINE}"
done < data.in > data.out

(iii)
#!/usr/bin/ksh93
MIN=100
MAX=400
LCNT=0
while IFS="" read -r -- LINE
do
  (( ++LCNT < MIN || LCNT > MAX )) && print -r -- "${LINE}"
done < data.in > data.out
mv -f data.out data.in

(iv)
#!/usr/bin/ksh93
while IFS="" read -r -- LINE
do
  [[ "${LINE:7:3}" != "xyz" ]] && print -r -- "${LINE}"
done < data.in > data.out
mv -f data.out data.in

--
Dana French



 Thu, 30 Aug 2007 09:08:55 GMT   
 
   [ 5 post ] 

Similar Threads

1. Extracting lines from a text file that match a certain criteria to another text file

2. Script to extract portions of text from a text file

3. How to extract the substring from file record

4. Extracting text from a file

5. Postscript file corrupted - extracting text/patching

6. How to extract columns from a text file?

7. Question: Extracting text from a file.

8. Extract specific text from file using sed

9. Urgent, need script or command to extract lines from a huge text file

10. Extracting lines from inside a text file (question for sed expert)


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software