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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
10-01-2012, 10:33 PM
|
#1
|
LQ Newbie
Registered: Oct 2012
Posts: 4
Rep: 
|
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.
|
|
|
10-02-2012, 02:37 AM
|
#2
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,038
|
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).
|
|
|
10-03-2012, 02:05 AM
|
#3
|
LQ Newbie
Registered: Oct 2012
Posts: 4
Original Poster
Rep: 
|
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
|
|
|
10-03-2012, 02:16 AM
|
#4
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,527
|
probably I misunderstood: grep generates a file named output. If you want to have an extension, just append it: grep blabla >> output.csv
|
|
|
10-03-2012, 12:15 PM
|
#5
|
LQ Newbie
Registered: Oct 2012
Posts: 4
Original Poster
Rep: 
|
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.
|
|
|
10-03-2012, 12:47 PM
|
#6
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
Why not just use
grep "X3=C"
?
|
|
|
10-03-2012, 12:54 PM
|
#7
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,797
|
Quote:
Originally Posted by suicidaleggroll
Why not just use
grep "X3=C"
?
|
Because there are double quotes in the input file 
|
|
|
10-03-2012, 12:58 PM
|
#8
|
LQ Guru
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573
|
Quote:
Originally Posted by Wim Sturkenboom
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.
|
|
|
10-03-2012, 02:42 PM
|
#9
|
LQ Newbie
Registered: Oct 2012
Posts: 4
Original Poster
Rep: 
|
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
|
|
|
10-03-2012, 10:05 PM
|
#10
|
Member
Registered: Apr 2012
Distribution: RedHat
Posts: 42
Rep:
|
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"
|
|
|
10-04-2012, 12:30 AM
|
#11
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,527
|
even, you can try: grep "X3=.C.", it should work also
|
|
|
All times are GMT -5. The time now is 04:45 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|