LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   In Shell script, csv file is not generating (http://www.linuxquestions.org/questions/programming-9/in-shell-script-csv-file-is-not-generating-4175429992/)

rks10a 10-01-2012 10:33 PM

In Shell script, csv file is not generating
 
Hi Friends,
I need your help.
I'm working on script, in which I'm trying to filter the values & displaying the output on screen as well as sending the output in a csv file.

Suppose I have values like:-
Input file has following file

X1="A" X2="B" X3="C" X4="D"
X1="A1" X2="B2" X3="C2" X4="D2"
X1="A1" X2="B3" X3="C" X4="C"
X1="A" X2="B" X3="C" X4="C4"
...............................
...............................

So, my goal is to filter all the values for X3="C" & send o/p to .csv file.

1) I used print "%s%s%s%s\n" "X1" "X2" "X3" "X4" > output

then
grep -w "C" {Input_file}.csv >> output

It was displaying o/p, also it is generating csv file.

problem with this script is, it filter not only X3=C, but it also filter for X4=C i.e., wherever it gets C in each line.

To resolve this issue, I filter with X3="C"

grep -w "X3=\"C\"" {Input_file}.csv >> output

O/p display in webpage is perfect. But, it doesn't generate csv file.

Please help me resolve it.Thanks.

grail 10-02-2012 02:37 AM

I think you will need to structure your question better if you want help.

Firstly, in what shell is the following working for you:
Code:

print "%s%s%s%s\n" "X1" "X2" "X3" "X4" > output
Also, if just printing the XN field names, why would you need such an elaborate print statement?

Second, how does the bracing structure you have used in the below work??
Code:

grep -w "C" {Input_file}.csv >> output
Lastly, I fail to see how the above is displaying to stdout (on screen) as well as into the output file?
Again this would have to be redirection in a shell I am not familiar with and that works in a way no other shell does (that i know of).

rks10a 10-03-2012 02:05 AM

1) Forget about print command, actually, its printf......I used only for print the heading.

2) For curly bracket, I'm using as variable with $ & I'm taking out from temp file, which is generated from database.
Please find corrected command

grep -w "C" ${Input_file}.csv >> output

pan64 10-03-2012 02:16 AM

probably I misunderstood: grep generates a file named output. If you want to have an extension, just append it: grep blabla >> output.csv

rks10a 10-03-2012 12:15 PM

My mistake again, actually, I'm using output file, which is also storing it in temp folder. So, it is storing as ${output}.csv, where output file name randomly generate.

So, suppose, it is like this

grep -w "C" ${Input_file}.csv >> ${output}.csv

which is working fine. But when I use


grep -w "X3=\"C\"" ${Input_file}.csv >> ${output}.csv

It doesn't stored the output in the output file.

suicidaleggroll 10-03-2012 12:47 PM

Why not just use

grep "X3=C"

?

Wim Sturkenboom 10-03-2012 12:54 PM

Quote:

Originally Posted by suicidaleggroll (Post 4796238)
Why not just use

grep "X3=C"

?

Because there are double quotes in the input file :confused:

suicidaleggroll 10-03-2012 12:58 PM

Quote:

Originally Posted by Wim Sturkenboom (Post 4796244)
Because there are double quotes in the input file :confused:

So then

grep 'X3="C"'

His should work too though. I think the problem lies elsewhere in the script. It would help if the OP just copied and pasted his ACTUAL script into the post, instead of the pseudocode he's typing out which appear to be full of typos.

rks10a 10-03-2012 02:42 PM

I'm using "X3=\"C\"", because C is also variable. actually, I'm using "X3=\"$value\""

So, if I forget about all the variables, in that case this sentance is correct..

grep -w "X3=C" ${Input_file}.csv >> ${output}.csv

ip_address 10-03-2012 10:05 PM

Can you try this ..

Let's say the name of the input file is rks10a.csv and the name of the desired output file is rks10a_output.csv

# Input file
Code:

$ cat rks10a.csv

X1="A" X2="B" X3="C" X4="D"
X1="A1" X2="B2" X3="C2" X4="D2"
X1="A1" X2="B3" X3="C" X4="C"
X1="A" X2="B" X3="C" X4="C4"

you can use a bash script, get inputs from STDIN and generate output files

# Bash script
Code:

$ cat rks10a.sh

#!/bin/bash

# usage: ./rks10a.sh rks10a rks10a_output

var='C'
Input_file="$1".csv
Output_file="$2".csv
echo "X1 X2 X3 X4" >> "$Output_file"
grep -w "X3=\"$var\"" "$Input_file" >> "$Output_file"

# Usage
Code:

$ ./rks10a.sh rks10a rks10a_output
# Output
Code:

$ cat rks10a_output.csv
X1 X2 X3 X4
X1="A" X2="B" X3="C" X4="D"
X1="A1" X2="B3" X3="C" X4="C"
X1="A" X2="B" X3="C" X4="C4"


pan64 10-04-2012 12:30 AM

even, you can try: grep "X3=.C.", it should work also


All times are GMT -5. The time now is 06:36 AM.