Search a file column wise and delete it

Scottn, m really sorry but i have not got my answer yet.
my concern is how to delete the row !!!
i have a file which has a column that is unique
i am intending to serach it and if it is there to remove the row.

the file looks like

 
ROLLNO,NAME ,SUB1,SUB2,SUB3,TOTAL,PERCENTAGE,RESULT
15 ,rig ,34 ,56 ,87 ,177 ,59 % ,PASS
23 ,wel ,45 ,76 ,56 ,177 ,59 % ,PASS
24 ,rew ,23 ,45 ,67 ,135 ,45 % ,THIRD

if i need to remove the row with a roll num
i have tried with

 
cut -f1 -d "," studentrec.txt | sed '/"$rollno"/d'
even with 
cut -f1 -d "," studentrec.txt | sed '/"$rollno"/{d;}'

but still unable to get the required result

plz help

Try,

$ awk -F ',' -v rnum=15 '$1 == rnum { next }1' studentrec.txt
$ rnum=23;sed  '/^'$rnum'/d' studentrec.txt

gotam,

If you want to delete always the row/rows containing "rollno" try with:

grep -v -i rollno inputfile

regards

Hi,

Try this,

grep -v "^$roll_no" stundent.txt

i tried all of ur suggestions

this is not helping me to permanently delete the intended row.
rather.. at the time i am pursuing the command it helps me to hide the intended rows and output the rest
but againn when i am doing all rows are coming intact.

Plz help me to delete the rows permanently from the file !!!

Hi,

write the output of the command to temp file and rename (mv) the temp file with stundent.txt.

grep -v "^$roll_number" stundent.txt > temp ; mv temp stundent.txt

Hi.

If your sed supports the -i option:

sed -i "/^$rollnum[ ,]/d" file1

Otherwise you can use ed:

ed file1 << !
/^$rollnum[ ,]/d
w
q
!

Otherwise, create a temporary file, as shown in previous suggestions.

Thanks..
i got it working..

sed -i is not working .. not sure why!!
but the one suggested by praveen serves the purpose though it requires a temp file.

thank you all !!

Not all SEDs have the -i option (in fact very few of them do!)

The pravin27 solution will fail for three-digit roll numbers where the first two numbers are not unique:

i.e

23
230

You should modify it slightly.

yes thats true !!
i was happy assuming that it got resolved
you made me to fry my brain again .
thanks for pointing that out !! i'll work on it and post the finding :slight_smile: