Operating System - HP-UX
1834462 Members
2861 Online
110067 Solutions
New Discussion

Re: Line matching with sed

 
SOLVED
Go to solution
Declan Heerey
Frequent Advisor

Line matching with sed

Hello,

I am trying to run sed against a file (200 lines long) where the first three characters of the file should always be HDR, POL and OBJ. What i want is to run sed on the file, check the three first characters of each line and if it is not HDR, POL or OBJ then error out! Can anyone help, it'd be much appreciated!
4 REPLIES 4
Justo Exposito
Esteemed Contributor

Re: Line matching with sed

Hi Declan,

I Think that is better to use grep for this job, for instance:

grep -v -e "^HDR" -e "^POL" -e "OBJ"

Regards,

Justo.
Help is a Beatiful word
Justo Exposito
Esteemed Contributor

Re: Line matching with sed

Sorry mismatch

grep -v -e "^HDR" -e "^POL" -e "^OBJ"

Regards,

Justo.
Help is a Beatiful word
Steve Steel
Honored Contributor
Solution

Re: Line matching with sed

Hi

This is clean

cat filein|while read line
do
for i in $(echo HDR POL OBJ)
do
echo |grep ^$i >> fileout 2>/dev/null
done
done


filein = old fileout=new


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Declan Heerey
Frequent Advisor

Re: Line matching with sed

Thanks for that guys - both methods worked, i'm spoiled for choice as to which to use! I'll compare both for speed on bigger files!!!

Thanks again