Operating System - HP-UX
1833996 Members
3024 Online
110063 Solutions
New Discussion

Stripping certain lines from a file

 
SOLVED
Go to solution
Dave Walley
Frequent Advisor

Stripping certain lines from a file

Hi.

I have a requirement to select from a file all lines following the first line with the numbers 0 and 8 in positions 61 and 62 respectively.


I have tried some sed functions
sed -n '.{60}08' filename
without success.


Any suggestions

Dave
why do i do this to myself
16 REPLIES 16
Steve Sauve
Frequent Advisor

Re: Stripping certain lines from a file

You could use "cut -c 61,62" and an it/then routine to check the results of the cut. Then write the result to a new file if it doesn't equal the 08. That would give you a new file with only the lines you want.

Hope this helps,
Steve
James R. Ferguson
Acclaimed Contributor

Re: Stripping certain lines from a file

Dave:

cat /tmp/in|cut -c61-62|grep "08" > /tmp/out

...JRF...
Emilio Sierra
Advisor

Re: Stripping certain lines from a file

Test with
grep '^.{60}08' filename
Jaroslaw Tomkiewicz
Occasional Advisor

Re: Stripping certain lines from a file

Hi Dave,

try this command:
awk '{if (substr($0,60,2)=="08") print }' name_of_your_file


regards

Jaroslaw
Tom Danzig
Honored Contributor

Re: Stripping certain lines from a file

Try:

awk '{if(NR>1){if(substr($0,61,2)=="08"){print $0}}}' input_file_name
Dave Walley
Frequent Advisor

Re: Stripping certain lines from a file

Thank you all for your suggestions but I require every line from the first occurence of 08 in 61 and 62 regardless of what is in columns 61 and 62 thereafter.
why do i do this to myself
Tom Danzig
Honored Contributor

Re: Stripping certain lines from a file

awk '{if(substr($0,61,2)=="08"){flag=1}{if(flag==1){print $0}}' input_file_name


Tom Danzig
Honored Contributor

Re: Stripping certain lines from a file

Sorry. My last post had a syntax error:

awk '{if(substr($0,61,2)=="08"){flag=1}{if(flag==1){print $0}}}' input_file_name
Emilio Sierra
Advisor

Re: Stripping certain lines from a file

a=`grep -n '^.{9}08' filename | cut -d':' -f1|head -1`
b=wc -l filename
c=`expr b - a`
tail -$c filename

?
Victor BERRIDGE
Honored Contributor

Re: Stripping certain lines from a file

TOT=`cat gogo|wc -l`
let COUNT=0
for LINE in `cat gogo`
do
COUNT=`expr $COUNT + 1`
if [ "`echo $LINE|cut -c 61-62 | grep 08`" -eq 08 ]
then
TAIL=`expr $TOT - $COUNT `
tail -n`expr $TAIL` gogo>gugi
exit
fi
done
Emilio Sierra
Advisor
Solution

Re: Stripping certain lines from a file

In the previous one there was an error:

a=`grep -n '^.{60}08' filename | cut -d':' -f1|head -1`
b=`cat filename | wc -l`
c=`expr $b - $a`
tail -$c filename

Explanation:
in $a there is the number of the first coincident line
in $b the line number of the file
in $c the subtraction between the total number and first

Excuse by my English, is poor.
Peter Scheer
New Member

Re: Stripping certain lines from a file

Try:

sed -n '/^.{60}08/,$p' filename

This gives sed the address-range from (first line with 08 in cols 61,62) to ($ meaning last line) to p (print)

Regards,
Peter
Peter Scheer
New Member

Re: Stripping certain lines from a file

Additional Info to previous post:
The Backslashes before the two curly braces got eaten.

Peter
John Hall
Frequent Advisor

Re: Stripping certain lines from a file

All above responses are close but no cigar! They do not take into account that the lines to be extracted might NOT contain 08 in columns 61 & 62. Dave said he wanted the lines AFTER the first line that met the test condition. Unfortunately Dave did not specify that these lines did or did not contain 08 (in col 61&62). If I were to cover all the bases I would have to assume Dave meant ALL lines after the line that met his criteria. The following code is more complete given the program specs Dave supplied. In my example, the input file is /tmp/abc and the output file is /tmp/xyz:

awk '{if(substr($0,61,2)=="08")
{while (getline){print $0}}
}' /tmp/abc > /tmp/xyz

Bye!
John Hall
Frequent Advisor

Re: Stripping certain lines from a file

All above responses are close but no cigar! They do not take into account that the lines to be extracted might NOT contain 08 in columns 61 & 62. Dave said he wanted the lines AFTER the first line that met the test condition. Unfortunately Dave did not specify that these lines did or did not contain 08 (in col 61&62). If I were to cover all the bases I would have to assume Dave meant ALL lines after the line that met his criteria. The following code is more complete given the program specs Dave supplied. In my example, the input file is /tmp/abc and the output file is /tmp/xyz:

awk '{if(substr($0,61,2)=="08")
{while (getline){print $0}}
}' /tmp/abc > /tmp/xyz

Bye!
Dave Walley
Frequent Advisor

Re: Stripping certain lines from a file

Thank you for you ideas, I was able to use parts from different people.

Thank you again.

Dave
why do i do this to myself