Deleting a line from a flatfile using Shell Script

Hi All,
Can Anyone please tell me,how can I delete a line from a file.
I am reading the file line by line using whil loop and validating each line..Suppose in the middle i found a particular line is invalid,i need to delete that particular line.
Can anyone please help.
Thanks in advance,
Dinesh

you may redirect the output to a new file, while writing all the lines in the loop except for the ones you dont want...else
try sed, grep, awk anything.

Thanks for ur reply..
Can you please help me in writing the command using sed/awk. I am a beginner in UNIX.

search for the awk or sed manual...
atleast give some try...

I have tried with sed -i and sed -d ..but it is not working

show your code.
sample input,
validation pattern, and
expected output

input file
First Name - position from 1-5
Last Name- position 6-10
Age-Position 11-12
Say above is the format of input file.
Example:
DINESKUMAR21
SACHISHAH 23
23
GURUNTARIM22

In the above example,in the 3 rd record Name is missing. Now while reading the file line by line,I want to delete the 3 rd line during validation.

---------- Post updated at 08:47 AM ---------- Previous update was at 08:46 AM ----------

for the third line blankspaces will be present before 23

and what have you tried?

---------- Post updated at 07:37 PM ---------- Previous update was at 07:23 PM ----------

grep '^[A-Z]' inputfile > newfile
awk '/^[A-Z]/ {print}' inputfile > newfile
perl -pi -e 's/^.*\n$// if (unpack "A5") eq ""' filename

tyler_durden

Hi,
You can use the below command to delete the invalid line.

cat data | awk '\{ if\(substr\($1,1,5\)!="" && substr\($1,6,10\)!="" && substr\($1,11,12\)!=""\) print $1\}' > new\_filtered\_file_name.

Where the data is the input file.

Thanks,
chidhu

Really ?

$
$ cat data
DINESKUMAR21
SACHISHAH 23
          23
GURUNTARIM22
$
$ cat data | awk '{ if(substr($1,1,5)!="" && substr($1,6,10)!="" && substr($1,11,12)!="") print $1}'
DINESKUMAR21
GURUNTARIM22
$
$

tyler_durden

Thanks for your replies ..i ll try it and update

---------- Post updated 06-26-09 at 03:20 AM ---------- Previous update was 06-25-09 at 03:54 AM ----------

I donot want to redirect to any new file. I want to delete the line from the file itself.
I have tried sed -i '3d' to delete the 3rd line. But my shell is not recognizing sed -i.

I have tried sed '3d' <filename> ..it is displaying the file without the 3rd line. But the 3rd line is not permanently deleted. Can anyone please help

With a little trick you can redirect the output of sed into the same file

sed '3d' file | tee file

Thank you..this is working..

Hi,

Instead of counting the line number which has to be deleted and coming out of the file and then implementing it, you may try an easy way out.
This is an interactive way of deleting a line while reading the file:

  1. Enter the file using say vi xx.txt
  2. Esc and then press down arrow to reach the line to be deleted
  3. Esc click DD (twice)
  4. Line is removed

Regards,
Sumedha