LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-01-2012, 10:33 PM   #1
rks10a
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Rep: Reputation: Disabled
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.
 
Old 10-02-2012, 02:37 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,518

Rep: Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896
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).
 
Old 10-03-2012, 02:05 AM   #3
rks10a
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-03-2012, 02:16 AM   #4
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,771

Rep: Reputation: 1272Reputation: 1272Reputation: 1272Reputation: 1272Reputation: 1272Reputation: 1272Reputation: 1272Reputation: 1272Reputation: 1272
probably I misunderstood: grep generates a file named output. If you want to have an extension, just append it: grep blabla >> output.csv
 
Old 10-03-2012, 12:15 PM   #5
rks10a
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
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.
 
Old 10-03-2012, 12:47 PM   #6
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,845

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Why not just use

grep "X3=C"

?
 
Old 10-03-2012, 12:54 PM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by suicidaleggroll View Post
Why not just use

grep "X3=C"

?
Because there are double quotes in the input file
 
Old 10-03-2012, 12:58 PM   #8
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,845

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by Wim Sturkenboom View Post
Because there are double quotes in the input file
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.

Last edited by suicidaleggroll; 10-03-2012 at 01:01 PM.
 
Old 10-03-2012, 02:42 PM   #9
rks10a
LQ Newbie
 
Registered: Oct 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-03-2012, 10:05 PM   #10
ip_address
Member
 
Registered: Apr 2012
Distribution: RedHat
Posts: 42

Rep: Reputation: 2
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"
 
Old 10-04-2012, 12:30 AM   #11
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,771

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 shell script help with a CSV file. lothario Linux - Software 2 08-31-2012 11:40 AM
Shell Script for CSV file comparision aravind_balan Programming 1 02-17-2009 03:33 AM
Calculate average from csv file in shell script khairilthegreat Linux - Newbie 5 11-21-2007 12:57 PM
Filtering a CSV file from web log with shell script? Micro420 Programming 8 08-22-2007 03:13 AM
Shell script to read from csv file hendemeg Programming 1 05-11-2004 08:23 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration