Computing.Net > Forums > Unix > delete a line ina file using a serach pattern

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.

delete a line ina file using a serach pattern

Reply to Message Icon

Name: vijay (by bvijays)
Date: September 14, 2009 at 11:30:27 Pacific
OS: unix
Product: Sun microsystems Unix essentials featuring the solaris 10 operating system -
Subcategory: General
Tags: awk, sed, UNIX, shell, script
Comment:

hi,

i need to search for a pattern in files for example the content of the file is below

3555005!K!00630000078!C!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!3555005!D!16042296!DUMMY!20090805235959!0!47001231000000!0!336344324!1!1!POST!USAGE!336344324!0!3555005!C!336344324!1!!!EUR!1!1!!I!3555005!S!00630000078!20090805172515!LF010300!

here i want to search for lines with !D! and the 7th field in the line is less than the system date, then i want to delet the line and save the file.

is that possible? please help.

thanks,
Vijay



Sponsored Link
Ads by Google

Response Number 1
Name: thepubba1
Date: September 20, 2009 at 02:15:06 Pacific
Reply:

If ! is your field separator, then the 7th field is 16042296. How do you get a date out of that? The 5th field is a date "20090805235959" and 60th field is a date "20090805172515". A !D! would appear to be the 32d field.

This question requires much more information. However, modifying your input file using today's date (20 Sep 2009) I created 3 entries:

3555005!K!00630000078!C!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!3555005!D!16042296!DUMMY!20090805235959!0!47001231000000!0!336344324!1!1!POST!USAGE!336344324!0!3555005!C!336344324!1!!!EUR!1!1!!I!3555005!S!00630000078!20090805172515!LF010300!
3555005!K!00630000078!C!20090920235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!3555005!X!16042296!DUMMY!20090805235959!0!47001231000000!0!336344324!1!1!POST!USAGE!336344324!0!3555005!C!336344324!1!!!EUR!1!1!!I!3555005!S!00630000078!20090805172515!LF010300!
3555005!K!00630000078!C!20090920235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!3555005!K!204042880166840!I!20090805235959!47001231000000!16042296!336344324!A!1!ENG!0!00630000078!NO!00630000078!3555005!D!16042296!DUMMY!20090805235959!0!47001231000000!0!336344324!1!1!POST!USAGE!336344324!0!3555005!C!336344324!1!!!EUR!1!1!!I!3555005!S!00630000078!20090805172515!LF010300!

I then wrote this rough script:

#!/bin/ksh

function processLine {

print $line | while IFS='!' read f1 f2 f3 f4 f5 f6 f7 theRest

do
if [[ $f5 -lt $Today ]]
then
WriteIt=1
else
WriteIt=0
fi
done

}


typeset -L8 f5

Today=$(date +%Y%m%d)

exec 3< junk099.txt

while read -u3 line
do
if [[ $line == *"!D!"* ]]
then
processLine
else
WriteIt=1
fi

if [[ $WriteIt -eq 0 ]]
then
print $line
fi
done

Running this script against the modified input file on 20 Sep 2009, it output the line that contained no !D! and the line that had a !D! but an equal date. I'll let you figure out the rest.



0
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: delete a line ina file using a serach pattern

Delete first line of a file www.computing.net/answers/unix/delete-first-line-of-a-file/7594.html

Split a Text file using blank line www.computing.net/answers/unix/split-a-text-file-using-blank-line/7501.html

Delete all lines containing pattern www.computing.net/answers/unix/delete-all-lines-containing-pattern/4499.html