1834018 Members
2058 Online
110063 Solutions
New Discussion

Ignore 1st adn 2nd

 
SOLVED
Go to solution
chinnaga
Advisor

Ignore 1st adn 2nd

Hi ,

I am trying to loop through a text file and I want to ignore the 1st and the last line in the file. HOw can I achive this without using tail and head.

for example

START|
1|RECV1_PLCMM_1_YYYYMMDD_HHMMSS_DATA.TXT|
2|RECV1_PLCMM_2_YYYYMMDD_HHMMSS_DATA.TXT|
3|RECV1_PLCMM_3_YYYYMMDD_HHMMSS_DATA.TXT|
4|RECV1_PLCMM_4_YYYYMMDD_HHMMSS_DATA.TXT|
5|RECV1_PLCMM_5_YYYYMMDD_HHMMSS_DATA.TXT|
END|


I want to ignore START and END in the file.

Could u pls help me with this
8 REPLIES 8
Oviwan
Honored Contributor

Re: Ignore 1st adn 2nd

Hey

perl -lane 'print if !/START/ && !/END/' < yourfile

you can bind this in a loop...

Regards
SANTOSH S. MHASKAR
Trusted Contributor

Re: Ignore 1st adn 2nd

awk -F | '$1 != "START" && $1 != "END" '{print $0}' ur_file_name

-Santosh
Peter Godron
Honored Contributor
Solution

Re: Ignore 1st adn 2nd

Hi,
elimination before processing:
grep -v -e'^START' -e'^END' data.lis

Eliminiation during processing:
while read record
do
if [ $"record" = "START" ] -o [ "$record" = "END" ]
then
echo skipped record
else
echo process record
fi
done < data.lis
Peter Godron
Honored Contributor

Re: Ignore 1st adn 2nd

Hi,
or if your requirement is not tied to START/END phrase:

# Get the number of lines in data file
a=`wc -l g.lis | cut -d' ' -f1`
# Subtract one for the last line
b=`echo "$a - 1" | bc`
# Echo all but the first and last line
sed -n "2,$b p" data.lis

Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.

So far you have not awarded any points !

Hein van den Heuvel
Honored Contributor

Re: Ignore 1st adn 2nd

I think the easiest way it to stash the current record away and process the prior.
Start processing after the second to skip the first.

In perl:

perl -ne "print $last if $.>2; $last=$_" some-text-file

fwiw,
Hein.

James R. Ferguson
Acclaimed Contributor

Re: Ignore 1st adn 2nd

Hi:

Here's another generalized way to skip the first and the last record in a file. Notice that this is not dependent upon any pattern matching:

# perl -ne 'print if $. > 1 and !eof' file

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Ignore 1st adn 2nd

Hi (again):

If you won't accept a particular language for your solution, then *say so* when you ask for help so that we don't waste our time!!!

...JRF...
chinnaga
Advisor

Re: Ignore 1st adn 2nd

I am sorry for the inconvience .