LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 11-21-2007, 01:13 AM   #1
morphix
Member
 
Registered: Nov 2007
Location: Newcastle, Australia
Distribution: Ubuntu, Debian, Arch
Posts: 47

Rep: Reputation: 15
Question script to parse variables to curl script and execute


Hi Guys,

I currently have a situation which up until now has been a complete manual copy, paste & URL editing procedure.

I have a text file which contains information in basic form:

111111,21-11-2007,Some Text Seperate By Spaces,No
222222,21-11-2007,More Text Seperate By Spaces,Yes

above is the example of what is in the text file.

What i am trying to do is make a script which reads the text file and puts the above into variables such as 22222 = $NUM, 21-11-2007 = $DATE, Text Spaces = $REASON, Yes = $ENABLED

For the ",Some Text Seperate By Spaces," i need all the spaces to get turned into "+" characters before being put as a variable

After this i need the example below to be run

curl <URL>/index.php?number=$NUM&date=$DATE&reason=$REASON&enabled=$ENABLED

if anyone knows how i can do this i would appreciate it as ive been messing with bash, grep, sed, curl & awk.


Cheers,

Morphix
 
Old 11-21-2007, 01:22 AM   #2
Four
Member
 
Registered: Aug 2005
Posts: 298

Rep: Reputation: 30
Perl script should do the trick
Code:
# read a line (assuming file is opened using open())

$read = <file>;

# get relevent data
$read =~ /(.*?),(.*?),(.*?),(.*)/;

$num = $1;
$data = $2;
$reason = $3;
$reason =~ s/ /+/g;
$enable =$4

open TOCURL, "| curl";
# its case sensitive so the below and above need to match case
print TOCURL "<URL>/index.php?number=$NUM&date=$DATE&reason=$REASON&enabled=$ENABLED
";

close TOCURL;
Four

Last edited by Four; 11-21-2007 at 01:25 AM.
 
Old 11-21-2007, 05:06 PM   #3
morphix
Member
 
Registered: Nov 2007
Location: Newcastle, Australia
Distribution: Ubuntu, Debian, Arch
Posts: 47

Original Poster
Rep: Reputation: 15
@Four: so i assume this reads line by line from the text file and does the same curl command per line?

Just i am at work atm and unable to test it.


EDIT:

Just tested it, it just errors out

$ perl test.pl
syntax error at test.pl line 16, near "$4

open "
Execution of test.pl aborted due to compilation errors.

Last edited by morphix; 11-21-2007 at 05:41 PM.
 
Old 11-21-2007, 06:08 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Maybe this is better. Substitute a valid url & curl options.

Code:
#! /usr/bin/perl -w

use strict;
use POSIX ":sys_wait_h";

    while(<>){
        my( $num, $date, $reason, $enabled ) = split /,/, $_;
        $reason =~ s/ /+/g;
        my $kid = fork();
        if( $kid ){
            waitpid( $kid, WNOHANG );
        }
        else{
            exec( "curl", "--help", "http://someurl.net/index.php?number=$num&date=$date&reason=$reason&enabled=$enabled\n" );
        }
    }
    exit( 0 );
Run the script with the data file as a commandline arg.
--- rod.
 
Old 11-21-2007, 06:27 PM   #5
morphix
Member
 
Registered: Nov 2007
Location: Newcastle, Australia
Distribution: Ubuntu, Debian, Arch
Posts: 47

Original Poster
Rep: Reputation: 15
@theNbomr: can't seem to get that to work either :/
 
Old 11-21-2007, 06:39 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
How about telling exactly what you did and what error you got?
 
Old 11-21-2007, 06:50 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Yah. What chrism01 said. Emphasis on exactly. With error messages and other relevant output pasted.
Did you replace the URL and curl options appropriately, as I mentioned?
--- rod.
 
Old 11-21-2007, 10:04 PM   #8
morphix
Member
 
Registered: Nov 2007
Location: Newcastle, Australia
Distribution: Ubuntu, Debian, Arch
Posts: 47

Original Poster
Rep: Reputation: 15
nevermind, i got it to work, was just me not fully understanding hah.

EDIT:

is there anyway i can have this script perform an if statement on the last variable?

eg. if $enabled = Yes then change text to "Yes+this+is+enabled" else "This+is+disabled"

cheers theNbomr

Last edited by morphix; 11-21-2007 at 11:15 PM.
 
Old 11-22-2007, 02:43 AM   #9
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by morphix View Post
is there anyway i can have this script perform an if statement on the last variable?

eg. if $enabled = Yes then change text to "Yes+this+is+enabled" else "This+is+disabled"
Code:
$enabled = ($enabled eq "Yes" ? "Yes+this+is+enabled" : "This+is+disabled")
 
Old 11-23-2007, 10:41 AM   #10
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 231Reputation: 231Reputation: 231
Quote:
Originally Posted by morphix View Post
. . . if anyone knows how i can do this i would appreciate it as ive been messing with bash, grep, sed, curl & awk.
Assuming you are still interested, in a short solution, here's the awk:
Code:
awk -F, '{gsub(" ","+",$3);system("curl $URL/index.php?number="$1"\\&date="$2"\\&reason="$3"\\&enabled="$4"")}' $FILE
This assumes that FILE & URL have been set to your desired values. FILE seems to have to be exported, because (AFAIK) the awk system call executes in a sub-shell.
The final double double quote is an artifact of the piece-wise construction of the code & may be unnecessary.


As for your 2nd Q, I am not clear if angrybanana's
code represents the logic you want, or if you want $3/$reason changed regardless of its value. I.E., if $4/$enabled does = "yes" but $3/$reason does NOT = "Yes+this+is+enabled" do you want it to be changed to "This+is+disabled" anyway?

BTW, I find the GNU all-on-1-page version of the The GNU Awk User's Guide aka "Gawk: Effective AWK Programming" to be the most helpful.
 
Old 11-23-2007, 01:33 PM   #11
Four
Member
 
Registered: Aug 2005
Posts: 298

Rep: Reputation: 30
Quote:
Originally Posted by morphix View Post
@Four: so i assume this reads line by line from the text file and does the same curl command per line?

Just i am at work atm and unable to test it.


EDIT:

Just tested it, it just errors out

$ perl test.pl
syntax error at test.pl line 16, near "$4

open "
Execution of test.pl aborted due to compilation errors.
Oops I forgot a semicolon

Heres a more complete version simply run by

perl perlcode.pl file.txt_or_some_file

Code:
# this code is not tested I'm at school right now using windows machine with no perl
# read a line (assuming file is opened using open())
$file = $ARGV[0];
if(!$file){
   print "Please enter a file name\n";
   $file = <>;
}
if(!(-e $file)){
   print "file doesn't exit\n";
}
open (file, "$file") or die ("couldn't open $file\n");
while(<file>){
   $read = $_;

   # get relevent data
   $read =~ /(.*?),(.*?),(.*?),(.*)/;

   $num = $1;
   $data = $2;
   $reason = $3;
   $reason =~ s/ /+/g;
   $enable = $4;

   if($enable eq "yes"){
      # put code to do something here if you want
   }
   open (TOCURL, "| curl") or die("Couldn't open pipe to curl\n");
   # its case sensitive so the below and above need to match case
   print TOCURL "<URL>/index.php?number=$num&date=$date&reason=$reason&enabled=$enable
";

   close TOCURL;
}
close file;

Last edited by Four; 11-23-2007 at 01:34 PM.
 
Old 11-24-2007, 08:38 PM   #12
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
You need to chomp() the input filename to remove the trailing '\n'.
Also, for cmds like 'open'/'close', perl stores the error code/msg in '$!'. You should add this to the exit msg so you know WHY it failed.
 
Old 11-25-2007, 05:47 PM   #13
morphix
Member
 
Registered: Nov 2007
Location: Newcastle, Australia
Distribution: Ubuntu, Debian, Arch
Posts: 47

Original Poster
Rep: Reputation: 15
theNbomr scripts works upto a point.

when i enter the CURL URL and run the script it gives me back the following errors.

$ perl script.pl script_txt.txt
Bareword found where operator expected at script.pl line 14, near ""curl -s -S -b cookie.txt -d "number"
(Missing operator before number?)
Operator or semicolon missing before &radius at script.pl line 14.
Ambiguous use of & resolved as operator & at script.pl line 14.
Operator or semicolon missing before &email at script.pl line 14.
Ambiguous use of & resolved as operator & at script.pl line 14.
Operator or semicolon missing before &ftp at script.pl line 14.
Ambiguous use of & resolved as operator & at script.pl line 14.
Operator or semicolon missing before &cancel_reason at script.pl line 14.
Ambiguous use of & resolved as operator & at script.pl line 14.
Operator or semicolon missing before &do_cancel_service at script.pl line 14.
Ambiguous use of & resolved as operator & at script.pl line 14.
String found where operator expected at script.pl line 14, near "1" http://url/subdir/page.php""
(Missing operator before " http://url/subdir/page.php"?)
Global symbol "$reason" requires explicit package name at script.pl line 8.
syntax error at script.pl line 14, near ""curl -s -S -b cookie.txt -d "number"
Execution of script.pl aborted due to compilation errors.

------

Also with $enabled checking if = yes or no, all i want it to do is. if $enabled = Yes then change $enabled to "ETC Applies" if $enabled = No then change $enabled to "No ETC"

Last edited by morphix; 11-25-2007 at 05:49 PM.
 
Old 11-25-2007, 05:54 PM   #14
morphix
Member
 
Registered: Nov 2007
Location: Newcastle, Australia
Distribution: Ubuntu, Debian, Arch
Posts: 47

Original Poster
Rep: Reputation: 15
Four: your updated script when running just creates 4-5 empty lines (i am testing output via echo command instead of curl to test).
 
Old 11-25-2007, 08:14 PM   #15
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by morphix View Post
theNbomr scripts works upto a point.

when i enter the CURL URL and run the script it gives me back the following errors.

$ perl script.pl script_txt.txt
Bareword found where operator expected at script.pl line 14, near ""curl -s -S -b cookie.txt -d "number"
Please post the line in question with your edits. I see nothing that prevents it from compiling, and it compiled fine in the form that I posted.
--- rod.
 
  


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
How to execute a ssh script on Linux server from Windows through a bat script? wanna13e Programming 13 10-23-2009 03:41 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 12:33 PM
Help w/ script to read file and parse variables cslink23 Linux - General 18 11-26-2006 03:22 AM
cURL script question verbatim Programming 8 05-18-2005 05:50 PM
Passing variables from AWK script to my shell script BigLarry Programming 1 06-12-2004 05:32 AM


All times are GMT -5. The time now is 07:18 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration