473,597 Members | 2,198 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP scripting... An similiar awk command

Greetings,

I have the need to take a file and remove the trailing white space as
well as any empty lines... I inherited this script:
cat $1 | awk -v quote="'" \
'
/^[ ]*$/ { next; }
/^.*[ ]*$/ {
printf ("%s\n",subs tr ($0,1,match($0, "[
]*$")-1));
next;
}
{ print $0; }
'
I prefer to do it in PHP. Is there a simpler way?

Oct 31 '06 #1
3 7263
"Anthony Smith" <mr******@hotma il.comwrote:
>
I have the need to take a file and remove the trailing white space as
well as any empty lines... I inherited this script:
>>
cat $1 | awk -v quote="'" \
'
/^[ ]*$/ { next; }
/^.*[ ]*$/ {
printf ("%s\n",subs tr ($0,1,match($0, "[
]*$")-1));
next;
}
{ print $0; }
'
I prefer to do it in PHP. Is there a simpler way?
Why would you prefer to do it in PHP? This is exactly what commands like
perl and awk and sed were designed for, not php.

There are, however, simpler ways to do it. For example, this should
replace your entire script:

sed -e 's/[ \t]*$//' -e '/^$/d' $1
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Nov 1 '06 #2
Excellent point Tim.

Well, our web development (I just started in this group) consists of
perl and python in cgi. I want to move to php. Unfortunately, the idea
was to consolidate, but instead I am adding. However when I saw you
could do scripting with PHP I thought I had an answer for Perl and
Python. I do realize that there are some things that Perl and/or Python
just do better. Perl seems to intimidate people and become hard to read
past a certain point. I do understand that part of that is the
programmer's fault. I have no problem with Perl. I just thought since
we are using PHP to do web development why not use it as our scripting
language too. Since sed is not perl, this should be ok.
Tim Roberts wrote:
"Anthony Smith" <mr******@hotma il.comwrote:

I have the need to take a file and remove the trailing white space as
well as any empty lines... I inherited this script:
>
cat $1 | awk -v quote="'" \
'
/^[ ]*$/ { next; }
/^.*[ ]*$/ {
printf ("%s\n",subs tr ($0,1,match($0, "[
]*$")-1));
next;
}
{ print $0; }
'
I prefer to do it in PHP. Is there a simpler way?

Why would you prefer to do it in PHP? This is exactly what commands like
perl and awk and sed were designed for, not php.

There are, however, simpler ways to do it. For example, this should
replace your entire script:

sed -e 's/[ \t]*$//' -e '/^$/d' $1
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Nov 1 '06 #3

Anthony Smith wrote:
Greetings,

I have the need to take a file and remove the trailing white space as
well as any empty lines... I inherited this script:
cat $1 | awk -v quote="'" \
'
/^[ ]*$/ { next; }
/^.*[ ]*$/ {
printf ("%s\n",subs tr ($0,1,match($0, "[
]*$")-1));
next;
}
{ print $0; }
'
I prefer to do it in PHP. Is there a simpler way?
$a = file($filename) ;
$a = array_filter(ar ray_map('rtrim' , $a));

Nov 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

41
2776
by: Richard James | last post by:
Are we looking at the scripting world through Python colored glasses? Has Python development been sleeping while the world of scripting languages has passed us Pythonista's by? On Saturday Slashdot ran this article on the "best" scripting languages. http://developers.slashdot.org/developers/04/06/12/2125229.shtml?tid=126&tid=156 "Folks at the Scriptometer conducted a practical survey of which scripting language is the best. While...
1
2019
by: Gonzalo | last post by:
Hi everyone! I want to develop a C++ application, which must be scriptable (I'm considering to use Python or Lua). The end users should develop and run their scripts in an IDE, and the scripting language must be extended with specific functions related to my application. The IDE should have debugging capabilities (step by step execution, breakpoints, watching variables, etc. ) What would be the best option in order to develop this using...
14
2797
by: Michele Simionato | last post by:
I would like to know what is available for scripting browsers from Python. For instance, webbrowser.open let me to perform GET requests, but I would like to do POST requests too. I don't want to use urllib to emulate a browser, I am interested in checking that browser X really works as intended with my application. Any suggestion? Michele Simionato
5
2256
by: Javaman59 | last post by:
Hi all, I'm a Unix guy from way back, who's enjoying learning C# and Windows programming. Overall, it's not such a difficult transition (as long as one keeps an open mind :) ), but there is one thing from Unix I miss the most - the command line. I've install a Unix like programming environment, call MSys, which let's me do command line programming the way I want to, with the bash shell, and this is running smoothly (vim editor, bash,...
3
2161
by: Chris Lieb | last post by:
I am new to XML Schema and am running into a bit of a snag. I have defined an XML-based scripting language for an updater program that I am working on. I would like to make a schema for this language since malformed XML documents break the updater. (I did not have time to add good error-handling code, so run-time errors can abound if the document is malformed.) A sample doc might look something like this: <manifest>
10
2263
by: A.M | last post by:
Hi, I am having difficulty with shell scripting in Python. I use the following command to run a DOS command and put the return value in a Python variable:
0
1240
by: Phas0r | last post by:
I have made a embedded scripting language for C++ based on XML. But the most important part is missing, calling C++ functions from within the script. I have a struct which stores all c++ commands. struct _Commands { const xmlChar* Name; const xmlChar* Args; void *Command; } Commands; But when i wan't to execute Command it says i can't use it as a function. I'm trying to execute it this way
12
2939
by: Tony Belding | last post by:
I'm interested in using an off-the-shelf interpreted language as a user-accessible scripting language for a MUCK. I'm just not sure if I can find one that does everything I need. The MUCK must be able to call the interpreter and execute scripts with it, but the interpreter must also be able to call functions in the MUCK code. And then there's the security issue that really worries me. . . I have to be able to limit what the interpreter...
2
2940
by: Sm00th | last post by:
Hi all, Need help again in batch scripting. I need to create a command line script to launch an .exe file, but this .exe launches a window instead of a command prompt, and in this window you have to login and click some buttons before it actually do its stuff. How can I pass keystroke parameters just by using the command line? Here's what it's supposed to do: 1) Run winprogram.exe 2) Enter password 3) Tab 4) Enter 5) delay 5 seconds
0
7962
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8267
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8380
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8024
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5423
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3921
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1229
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.