Computing.Net > Forums > Unix > read text file, till str, write res

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

read text file, till str, write res

Reply to Message Icon

Name: tgkprog
Date: July 18, 2004 at 12:32:15 Pacific
OS: unix
CPU/Ram: sun
Comment:

- I need to read and ignore a file till i find a certain string and then write out the rest of the file in to another file using sh ... can some one help ? thanks

i tried using cat and a for loop but that breaks up the lines into words ... and the echo -n option does not work with my sh ... so it puts the words in seperate lines .

I also tried using cut - but that cuts all lines and not line by line .

Would be grateful for Any help would :).

The file is an XML file and i need to ignore text till the first line - but in fact a xml file could have a comment on the top - between which are optional then read and ignore the first true tag between < and >

i would like a script that can read a file line by line and the sample code to extract ( get sub strings of this line char by char ) i know the logic of id then fi ... thanks

this is for production - wish i could do it wothperl or java but for some crazy reason that the unix box admin only knows it ahs to be in sh




Sponsored Link
Ads by Google

Response Number 1
Name: tgkprog
Date: July 18, 2004 at 12:43:04 Pacific
Reply:

also please let the var of the in file name be InFIle
the new file be outFile , let the line that is currently read be currLine, and the logic to search in this line be shown - i searched the other posts that show how to read a file line by line but could not figure out where they are specifying the in file - not using $0 ... $1 .


0

Response Number 2
Name: Jim Boothe
Date: July 19, 2004 at 12:19:41 Pacific
Reply:

Based on what I think you said, I would use awk (which is a component of sh).

If you could be more clear about what you want to eliminate from the file, someone may help with a script - such as eliminate all lines beginning with a line that contains "whatever" through and including a line that contains "whatever". And are we keeping or discarding only whole lines, or is there partial line work involved?

And an example of your data always helps.


0

Response Number 3
Name: Dlonra
Date: July 19, 2004 at 13:37:49 Pacific
Reply:

picky,picky- "awk (which is a component of sh)". to me,"component" implies built-in.
awk is one of the numerous *nix utilities which
can be invoked from shells such as sh,rsh,ksh, bash ....

is this a homework assignment which requires a "pure
shell" solution?


0

Response Number 4
Name: tcsh
Date: July 20, 2004 at 11:24:16 Pacific
Reply:


Please try something like this:

#!/bin/ksh

FILE1="testfile"
FFLAG=0

while read -r TESTDATA
do
if (( $FFLAG == 0 ))
then
if ((` echo $TESTDATA | grep -c "KEY:0"` ))
then
FFLAG=1
TESTDATA=`echo $TESTDATA | sed -e 's/^.*KEY:0/KEY:0/g'`
echo $TESTDATA
fi
else
echo $TESTDATA
fi
done < $FILE1

Assuming the file you are trying to filter is 'testfile' and the pattern you are looking for is 'KEY:0'



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: read text file, till str, write res

Reading text file in Bourne Shell www.computing.net/answers/unix/reading-text-file-in-bourne-shell/4445.html

Rearrange the text file. www.computing.net/answers/unix/rearrange-the-text-file/5171.html

Read input file for deleting images. www.computing.net/answers/unix/read-input-file-for-deleting-images/2862.html