LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-05-2013, 11:54 AM   #16
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241

You get a syntax error. "line 1: syntax error near unexpected token `('".

You can't redirect input from a process unless you use a pipe. The error is the "<(awk ..." sequence.
 
Old 01-07-2013, 01:29 PM   #17
atjurhs
Member
 
Registered: Aug 2012
Posts: 168

Original Poster
Rep: Reputation: Disabled
shivaa,

typing
Code:
sh (cat outfile.txt <(awk 'NF>=2 {print $0}' infile.txt))> infile.txt
I get
Code:
bash: syntax error near unexpected token `cat'
typing
Code:
sh ./(cat outfile.txt <(awk 'NF>=2 {print $0}' infile.txt))> infile.txt
I get
Code:
bash: syntax error near unexpected token `('
typing
Code:
sh ./(cat outfile.txt <(awk 'NF>=2 {print $0}' infile.txt))> infile.txt
I get
Code:
bash: syntax error near unexpected token `cat'
so I don't know how to execute it, sorry

Tabitha
 
Old 01-07-2013, 01:47 PM   #18
atjurhs
Member
 
Registered: Aug 2012
Posts: 168

Original Poster
Rep: Reputation: Disabled
theNbomr, I got your Perl line to run IF I treat it as a line command input. How can I write it to a file and after chmod a+x perl_script.pl be able to execute it as a script?

right now the terminal gives me back:

Code:
 Bareword found where operator expected at perl_script.pl line 1, near " 'while(<>){ @z=split; print "$z[0] $z[1] $z[2] ",$z[3]*-1,"$z[4]\n";}' inputfile.dat" (missing operator before test?)
syntax error at perl_script.pl line 1, "-i -e"
Execution of perl_script.pl aborted due to compliation errors.
Tabitha
 
Old 01-07-2013, 02:57 PM   #19
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
This should do it. You might want to use more descriptive variable names now that it isn't a shell one-liner.

Code:
#! /usr/bin/perl -w
#
#  LQatjurhs.pl
#
#  Usage: LQatjuhrs.pl -i file.dat
#

use strict;

    while(<>){ 
        my @z=split; 
        print "$z[0], $z[1], $z[2], ",$z[47]*-1,"$z[4]\n";
    } 
    exit 0;
--- rod.

Last edited by theNbomr; 01-07-2013 at 03:00 PM.
 
Old 01-07-2013, 03:21 PM   #20
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 2,329
Blog Entries: 1

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
Quote:
Originally Posted by ntubski View Post
The rm is not necessary.
Actually depending on the system it could be necessary. The mv command has parameters to not overwrite existing files (-n noclobber). And there may be an alias that invokes that parameter. And this is something that can vary between systems and between different users on a system. Plus all the usual quirks if you're thing is not the only thing playing with the file at the time of execution. A little less quirky these days with faster computers, networks, and I/O devices, but still something that may need to be taken into account. Especially on larger systems or systems that may scale to many times their current usage.
 
Old 01-07-2013, 03:55 PM   #21
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,967

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Quote:
Originally Posted by Shadow_7 View Post
Actually depending on the system it could be necessary. The mv command has parameters to not overwrite existing files (-n noclobber). And there may be an alias that invokes that parameter. And this is something that can vary between systems and between different users on a system.
And that is precisely why aliases are expanded only in interactive shells, not in scripts.
 
Old 01-07-2013, 04:14 PM   #22
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
Quote:
Originally Posted by Shadow_7 View Post
Plus all the usual quirks if you're thing is not the only thing playing with the file at the time of execution.
I'm don't see how doing a rm helps with that. The rename system call is (has always been?) atomic (although that does require the temp file to live in the same filesystem as the target file, which might not be the case if you put it under /tmp).
 
Old 01-07-2013, 08:34 PM   #23
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 2,329
Blog Entries: 1

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
rename and move(mv) are not the same thing. And I was pointing out that for things owned by a system of users the temp file method was not optimal (or even possible). But completely functional for scripts that only get run by one user once in it's own location with unique filenames. And you can prevent expansion of aliases on an interactive shell by prefixing the command with a "\".
 
Old 01-07-2013, 08:34 PM   #24
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
iirc, 'mv' is atomic, but some rename cmds actually call a perl script in the background.
I forget which distros, but I always use mv rather than rename for portability and following the principle of 'least surprise'.
 
Old 01-07-2013, 10:14 PM   #25
atjurhs
Member
 
Registered: Aug 2012
Posts: 168

Original Poster
Rep: Reputation: Disabled
computer geeks are the best and atomic MUAH

Tabby
 
Old 01-07-2013, 10:34 PM   #26
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,013

Rep: Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225Reputation: 1225
Quote:
Originally Posted by chrism01 View Post
iirc, 'mv' is atomic, but some rename cmds actually call a perl script in the background.
Quote:
Originally Posted by Shadow_7 View Post
rename and move(mv) are not the same thing.
I guess I wasn't clear enough. I did NOT mean the rename(1) command, but rather the rename(2) system call. If the arguments to mv(1) are both in the same filesystem, then mv(1) will use the rename(2) system call.

Quote:
Originally Posted by Shadow_7
And I was pointing out that for things owned by a system of users the temp file method was not optimal (or even possible). But completely functional for scripts that only get run by one user once in it's own location with unique filenames.
Hmm, maybe I'm missing something obvious, but I think it's actually quite a good method (provided you create a unique filename with mktemp(1)). This is the same method used by sed -i actually:
Code:
~/tmp$ strace -e trace=file sed -i s/foo/bar/ file.dat 2>&1 | tail -3
open("file.dat", O_RDONLY)              = 3
open("./sednhVC9e", O_RDWR|O_CREAT|O_EXCL, 0600) = 4
rename("./sednhVC9e", "file.dat")       = 0
Interestingly, perl -i uses a different method:
Code:
~/tmp$ strace -e trace=file perl -i -pe s/foo/bar/ file.dat 2>&1 | tail -3
open("file.dat", O_RDONLY)              = 3
unlink("file.dat")                      = 0
open("file.dat", O_WRONLY|O_CREAT|O_EXCL, 0600) = 4
It seems like a problem with perl's approach is that a crash between the unlink and 2nd open call will result in loss of all the data.
 
Old 01-07-2013, 11:16 PM   #27
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,967

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Quote:
Originally Posted by ntubski View Post
Interestingly, perl -i uses a different method:
Code:
~/tmp$ strace -e trace=file perl -i -pe s/foo/bar/ file.dat 2>&1 | tail -3
open("file.dat", O_RDONLY)              = 3
unlink("file.dat")                      = 0
open("file.dat", O_WRONLY|O_CREAT|O_EXCL, 0600) = 4
It seems like a problem with perl's approach is that a crash between the unlink and 2nd open call will result in loss of all the data.
That's actually rather disturbing, since the window of vulnerability extends well beyond the unlink() call and subsequent open(), until perl has finished writing the output and performed a close(). Since a perl script could potentially be long-running, that might be a significant time. I presume that somewhere buried in the mass of perl documentation there is a warning about use of "-i".
 
Old 01-08-2013, 10:27 AM   #28
atjurhs
Member
 
Registered: Aug 2012
Posts: 168

Original Poster
Rep: Reputation: Disabled
theNbomr,

it perfoms the operation and writes it to the screen. I can get it to write it to a new file by using

Code:
perl perl_script.pl -i test.dat > out.dat
but it gives me the error message: Can't open -i: No such file or directory at perl_script.pl line 6

what I was really hoping it would do is to write an output file with the same name as the input file so I wouldn't have to use the mv command, but it looks like no matter what scripting language I use that is'nt an easy thing to do.

Last edited by atjurhs; 01-08-2013 at 10:40 AM.
 
Old 01-08-2013, 10:43 AM   #29
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Put the option in front of the first file.

Program files (the perl script) and parameters for the script follow the options and any parameters the option might have.
 
Old 01-08-2013, 11:03 AM   #30
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
You can invoke the '-i' option in at least two ways:
Code:
perl -i perlscript.pl datafile.dat
or, modifying the script itself...
Code:
#! /usr/bin/perl -w -i
# note added option  ^^
#
#....... script here ......
My Usage comment in the earlier post seems not to work, although I am sure I have used that format before so perhaps Perl's behavior has changed.

--- 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 do search & replace on a text file--need to extract URLs from a sitemap file Mountain Linux - General 4 08-07-2015 11:52 AM
[SOLVED] replace matched pattern from 1st file into 2nd file using awk sopier Programming 6 12-13-2011 10:58 AM
[SOLVED] Root unable to chmod, chown or operate on specific file chochem Linux - Software 2 09-06-2009 03:19 AM
replace line in CSV file and rename file connected to that name wademac Linux - Newbie 3 07-15-2009 02:09 PM
How to operate socket with file interface wangjinyi Programming 4 11-28-2005 12:31 AM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration