LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 08-01-2017, 07:20 AM   #1
sajesh.pp
LQ Newbie
 
Registered: Aug 2017
Posts: 3

Rep: Reputation: Disabled
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

Thanks,
Sajesh

Last edited by sajesh.pp; 08-01-2017 at 07:29 AM.
 
Old 08-01-2017, 07:41 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,617

Rep: Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963
Quote:
Originally Posted by sajesh.pp View Post
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.
 
Old 08-01-2017, 07:49 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,294
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
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.
 
1 members found this post helpful.
Old 08-01-2017, 02:23 PM   #4
TheEzekielProject
Member
 
Registered: Dec 2016
Distribution: arch
Posts: 668

Rep: Reputation: 190Reputation: 190
I would think that sed would also make a good candidate here
 
Old 08-02-2017, 05:58 AM   #5
sajesh.pp
LQ Newbie
 
Registered: Aug 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
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/"

update_value (){
awk -F, -v d="$dvc" '$2==d{if($3=="Free")$3="Used"; else print "In use"; input_value;}1' OFS=, $FILE_HOME/file > $FILE_HOME/file1
mv $FILE_HOME/file1 $FILE_HOME/file
}
input_value (){
echo -e "Enter device\n"
read dvc
update_value
}
input_value

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
 
Old 08-02-2017, 06:31 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,294
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
It looks like you're mixing shell scripts with awk scripts. I'd stick with just awk for this.

Also, it helps readability when code is posted between [code] and [/code] tags.
 
Old 08-02-2017, 06:40 AM   #7
sajesh.pp
LQ Newbie
 
Registered: Aug 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Turbocapitalist View Post
It looks like you're mixing shell scripts with awk scripts. I'd stick with just awk for this.

Also, it helps readability when code is posted between [code] and [/code] tags.
but, how can I get user input with in awk and also exit the program when if statement is false
 
Old 08-02-2017, 07:32 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,294
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Ok. You'll need a wrapper in shell to get input from the user. In that case maybe something like this:

Code:
dvc="";
FILE_HOME=".";

echo -n "Enter device\n"
read dvc

while ! awk -v d="$dvc" \
    'BEGIN { FS="[, ]+"; }

    $2==d {
        if($3=="Free") { $3="Used"; }
        else { fail++; }
    }
    { $0=$0; print; }

    END { if(fail) { exit(1); } }' \
    $FILE_HOME/file > $FILE_HOME/file1;
do
    echo "Device in use";
    echo -n "Enter device\n";
    read dvc;
done;

cat $FILE_HOME/file1;

exit 0;
 
Old 08-02-2017, 05:28 PM   #9
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,725

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by sajesh.pp View Post
Her the code I have tried
Code:
#!/bin/bash
dvc=""
FILE_HOME="/tmp/test/"

update_value (){
        awk -F, -v d="$dvc" '$2==d{if($3=="Free")$3="Used"; else print "In use"; input_value;}1' OFS=, $FILE_HOME/file > $FILE_HOME/file1
        mv $FILE_HOME/file1 $FILE_HOME/file
}
input_value (){
        echo -e "Enter device\n"
        read dvc
        update_value
}
input_value
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;}...
 
Old 08-04-2017, 12:16 PM   #10
MPH426
LQ Newbie
 
Registered: Feb 2013
Posts: 23

Rep: Reputation: Disabled
You could try this. Note: You'll need vim-minimal installed and your input will be case sensitive

Code:
#! /bin/ksh
printf "Enter Device: "
read device
print '%s/'$device',Free/'$device',Used\nwq' | ex Report.csv
 
Old 08-05-2017, 01:07 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by scasey View Post
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;}...
As said before, you cannot run a shell function from awk.
 
Old 08-05-2017, 01:32 AM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
Quote:
Originally Posted by Turbocapitalist View Post
Ok. You'll need a wrapper in shell to get input from the user. In that case maybe something like this:

Code:
dvc="";
FILE_HOME=".";

echo -n "Enter device\n"
read dvc

while ! awk -v d="$dvc" \
    'BEGIN { FS="[, ]+"; }

    $2==d {
        if($3=="Free") { $3="Used"; }
        else { fail++; }
    }
    { $0=$0; print; }

    END { if(fail) { exit(1); } }' \
    $FILE_HOME/file > $FILE_HOME/file1;
do
    echo "Device in use";
    echo -n "Enter device\n";
    read dvc;
done;

cat $FILE_HOME/file1;

exit 0;
You can have more than one statement between while-do and until-do and if-then.
The last exit status counts.
Code:
dvc=""
FILE_HOME="."

until
    echo "Enter device"
    read dvc
    awk -v d="$dvc" '
        BEGIN { OFS=FS="," }
        $2==d {
            if ($3=="Free") { $3="Used" }
            else { fail=1 }
        }
        { print }
        END { exit (fail) }
    ' $FILE_HOME/file > $FILE_HOME/file1
do
    echo "Device in use"
done

cat $FILE_HOME/file1

exit 0

Last edited by MadeInGermany; 08-05-2017 at 01:36 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to validate CSV file using shell script Sivarajkhamithkar Linux - Newbie 9 06-17-2017 09:03 PM
[SOLVED] A challenging script - Replace field of CSV file based on another CSV file arbex5 Programming 11 06-12-2013 06:56 AM
Need shell script help with a CSV file. lothario Linux - Software 2 08-31-2012 11:40 AM
Shell script: Find "\n\t..." to replace a string in a file michael24h7d Programming 8 05-11-2007 03:07 AM
Shell script to read from csv file hendemeg Programming 1 05-11-2004 08:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 11:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration