display the particular item name and item price from input file through command prompt?

StackOverflow https://stackoverflow.com/questions/17289943

  •  01-06-2022
  •  | 
  •  

Question

Chenna Black |1|76.00|Chenna
Chenna White big|1|102.00|Chenna
Chenna White small|1|82.00|Chenna
Dhal-Gram|1|78.00|Dhal
Dhal-Moong|1|76.00|Dhal
Dhal-Orid|1|72.00|Dhal
Dhal-Toor|1|68.00|Dhal
Dhal-Green gram|1|88.00|Dhal
Rajma-Black|1|72.00|Rajma
Rajma-Pink|1|80.00|Rajma
Rice-Basmanthi|1|80.00|Rice
Rice-Boiled|1|42.00|Rice
Rice-Idly|1|33.00|Rice
Rice-IGBasamanthi|1|99.00|Rice
Rice-Karanatka Ponni|1|38.00|Rice
Rice-Nirapara Silky|1|235.00|Rice
Rice-Raw|1|40.00|Rice
Rice-RoastedGram|1|100.00|Rice

its my input file. suppose if i check rice item from input file. it will display the rice item and rice item price also.And if i write non -available item from the file like " sugar " item . it will display the "item is not found".

Était-ce utile?

La solution

you can also do it in perl:

perl -F"|" -ane 'BEGIN{$f=0}if(/Rice/){print;$f=1}END{print "not found\n" unless($f)}' your_file

Autres conseils

awk -v item=Rice -F "|" "$0 ~ item {i++; total = total + $3}; END { 
if (i>0) print \"item name\",item,\"NO OF ITEMS\",i,\"total amount \",total; else print \"item not found\"}" datafile

output answer:

item name Rice NO OF ITEMS 8 total amount 667

In my program searching for rice. After that searched for pattern matching form input file. Next I used for counting number of rice items, total is used for total amount's of all rice. Initial item is variable, rice is value to assigned to variable item. Finally it displays the item, count of items, total amount of item.

awk -v item=Rice '$0 ~ item { i++; print }
                  END       { if (i == 0) print item " not found" }' data

If you want selected fields, you'll have to identify what each field contains. The 1 is presumably the price? (OK: no, it is fairly unlikely to be the price, but you should not expect us to divine the meaning of the columns in your data; — you should tell us the meanings.) It is not clear whether the item is in field 1 or field 4 or both.

That is not case-insensitive — if you want case-insensitivity, read the manual for your version of awk; it may or may not be possible.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top