Hi i need guidence to write code for file processing.

i have one file which having ~ as delimeter and each line is keyword is 1st column.

  1. First i have to find the lines which matches with ^6999
  2. Then go to the $23 filed counted using delimeter ~ and compare it with value 0
  3. if compare is true then replace the $24 field with G
  4. this to write in same file.

i tried with awk but unable to see any option of writing what to write in python or awk whichever is best for the large file.

cat sampletext.dat |grep "^6999"|awk 'BEGIN{FS=OFS="~"}$23 -gt "0"{$24="G"}1' file.txt

6999~486854~30000000~500000000 ~0~~40844506~0~~0~3830~0~0~ EVELYN          ~242741644~Worldconference~Conference No.~20190104~20190104~7~11108~63.650000~63.65~0~0~~5~2~1~Seconds~11108~11160
  • please provide some sample lines that reflect all possible ($1==6999 && $23>0 and $1==6999 && $23==0 and $1!=6999 ...) .
    – pLumo
    Jun 25, 2019 at 8:10

1 Answer 1


You don't need grep, you can filter the lines with awk:

As you don't provide a sample, I cannot test, but something like this should work:

awk -F'~' '$1==6999 && $23>0 {$24="G"} {OFS=FS; print}' file.txt

To write in the same file:

For GNU awk, simply add following option:

gawk -i inplace ...

Otherwise, you need a temporary file:

awk ... > file.txt.tmp && mv file.txt.tmp file.txt
  • Hi Plumo, Thanks for the suggestion . need one more help Actual FIle Below:- 6999~486854~30000000~500000000 ~0~~40844506~0~~0~3830~0~0~ EVELYN ~242741644~Worldconference~Conference No.~20190104~20190104~7~186~63.650000~63.65~0~0~~5~0~1~Seconds~0~0~0~0.00~ 6999~486854~30000000~500000000 ~0~~40844506~0~~0~3830~0~0~ EVELYN ~~Worldconference~Chairman:~20190104~20190104~7~11108~63.650000~63.65~0~0~~4~2~1~Seconds~11108~11160~0~0.00~ there multiple lines matching with ^6999 ,so i need awk command to update one by one by checking the condition for each line. Please suggest
    – Anand
    Jun 26, 2019 at 11:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .