Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting lines from a file (awk)

Status
Not open for further replies.

GuillermoVasquez

Programmer
Feb 8, 2000
1
US
Thanks in advance for your help.<br>
<br>
1. How can I delete a line from a file (given a pattern) ?<br>
<br>
2. How can I capture the environment's date, and pass it to two variables <br>
(date yyyy-mm-ddd and time hh:mm:ss) ?<br>
<br>
3. How could I use a parameter to replace a pattern ?<br>
e.g.: /30003/ {print $1} (use a variable which contains the value 30003)<br>
<br>

 
Here are some possible solutions which I think do what you want:<br>
<br>
1. Output the lines which do NOT contain the pattern eg use the negation operator as part of relational expression with the pattern<br>
eg $0 !~ /30003/ {print $0;}<br>
would output all lines from the file except thos containing 30003<br>
<br>
2. Depends upon the environment being used. One option is to embed your awk script in a shell or bat file. On the command line pass the sysdate in as a variable to the awkscript (use -v option), then in the BEGIN block use substring functions to break out into other variables as required.<br>
<br>
3. Not quite sure what is required here do you want to change the pattern value at runtime by a variable value? If so simply use the variable name in the pattern part (presumably the variable would have been initialised to some value in the BEGIN block. Also have a look at using associative arrays - these can be very powerful.<br>
<br>
Hope this helps<br>
<br>
Malc
 
Here is an example program along with some<br>input to quickly test it with.<br><br>I hope this helps you.<br><br>flogrr<br><A HREF="mailto:flogr@yahoo.com">flogr@yahoo.com</A><br><br>#!/usr/bin/sh<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name: testit<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Purpose: 1. Delete a line from file given a pattern.<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2. Capture system date and time and pass<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to two variables ( &quot;date&quot; and &quot;time&quot; ).<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3. Pass in a variable to be used for<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pattern matching within the program<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Usage: testit infile outfile pattern parameter<br>#<br>#<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Shell variables containing data to be<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;passed into program from command line.<br>#<br>#<br><br>pattern=$3<br>dnt=`date '+%Y-%m-%d %T'`<br>parameter=$4<br><br>nawk '<br><br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;if ( $0 ~ /'&quot;$pattern&quot;'/ ) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# does not output matched line;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# reads next line, effectively<br>&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# deleting the matched line<br><br>&nbsp;&nbsp;&nbsp;&nbsp;if ((!date)&&(!time)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;date = substr(&quot;'&quot;$dnt&quot;'&quot;,1,10)&nbsp;&nbsp;# expand shell variable in nawk<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time = substr(&quot;'&quot;$dnt&quot;'&quot;,12)&nbsp;&nbsp;&nbsp;&nbsp;# take substrings as needed<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;The date today is:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot; date &quot;\n&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;The time is now:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot; time &quot;\n&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>if ($0 ~ /'&quot;$parameter&quot;'/) {print $1 ; next }<br><br>print<br><br>}' $1 &gt; $2<br><br>clear<br><br>more $2<br><br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sample input file<br><br># bla bla bla bla bla bla bla<br># bla bla bla bla bla bla bla<br># bleep bla bla bla bla bla bla&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# question (1)<br># bla bla bla bla bla bla bla<br># bla bla bla bla bla bla bla<br># 21993 30134 49539 56213<br># 21993 30135 49540 56214<br># 30003 30136 49541 56215&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# question (3)<br># 21993 30137 49542 56216<br># 21993 30138 49543 56217<br><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top