shell script to find and replace value from csv file
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
shell script to find and replace value from csv file
Could some one please help me with requirement below ?
I am looking for a shell script to search a cell value in csv file and replace corresponding cell in same column.
Below is my csv file (report.csv)
#,Device,Status
1,Device1,Used
2,Device2,Free
3,Device3,Free
The search variable will take as input while executing the script.
eg: Enter the device name
user enters "Device2"
Next step : script should search for Device2 in report.csv. If it find Device2, check value in status column corresponding to it and if the value is Free replace it as Used else exit the script saying Device2 already in use
Could some one please help me with requirement below ? I am looking for a shell script to search a cell value in csv file and replace corresponding cell in same column. Below is my csv file (report.csv)
#,Device,Status
1,Device1,Used
2,Device2,Free
3,Device3,Free
The search variable will take as input while executing the script.
eg: Enter the device name user enters "Device2"
Next step : script should search for Device2 in report.csv. If it find Device2, check value in status column corresponding to it and if the value is Free replace it as Used else exit the script saying Device2 already in use
Ok...so now that we know your 'requirement', can you show us what YOU have done/written/tried of your own, and tell us where you're stuck?? We are happy to help you if you're stuck, but we will not write your scripts for you. There are many bash scripting tutorials you can find with a brief Internet search, many of which have examples to help you get started.
Please read the LQ Rules and the "Question Guidelines" link in my posting signature.
I'd start with awk or perl. If you have a really strict formatting with only commas between the fields and nowhere else, then use awk. If it is more complex, with commas sometimes in quoted fields, then use perl with a CPAN module.
Show us your code so far and where you've gotten stuck and we'll be able to help you move forward.
Ok...so now that we know your 'requirement', can you show us what YOU have done/written/tried of your own, and tell us where you're stuck?? We are happy to help you if you're stuck, but we will not write your scripts for you. There are many bash scripting tutorials you can find with a brief Internet search, many of which have examples to help you get started.
Please read the LQ Rules and the "Question Guidelines" link in my posting signature.
Her the code I have tried
#!/bin/bash
dvc=""
FILE_HOME="/tmp/test/"
This code is fine when the "if statement" is true. But when it is false, it is not calling function "input_value" from else section. It just print "In use" along with other entries in csv file to new file.
Is this the right approach I am doing ? I would be happy to see if there a different/better approach to achieve the requirement.
This code is fine when the "if statement" is true. But when it is false, it is not calling function "input_value" from else section. It just print "In use" along with other entries in csv file to new file.
Is this the right approach I am doing ? I would be happy to see if there a different/better approach to achieve the requirement.
Thanks,
Sajesh
Doesn't the semi-colon after "In use" terminate the else clause? Shouldn't there be brackets?: (untested)
Code:
awk -F, -v d="$dvc" '$2==d{if($3=="Free")$3="Used"; else {print "In use"; input_value;}...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.