Perl one liner to wrap comment lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl one liner to wrap comment lines
# 1  
Old 12-07-2014
Perl one liner to wrap comment lines

Greetings fellow scripters.

I find myself editing multiple files, sometimes with the same bits of information. My bash script, a changelog, and a plist file (OS X). Once I realized this, I thought why not script part of this process (and so it begins). In any case, I've solved several of the tasks I've set forth for myself. My new update script will update version/revision numbers in all files, and add text to the top of my change log using a combination of bash, python, and now perl (I think I'm nerding out a bit). I know this is all overkill, I just wanted to see if I could do it.

So, with the command
Code:
./update.sh 1.8.8.8 "some text for a change log header" /some.plist

Code:
pVersion="$1"
pRevision=$(echo "$1" | awk -F. '{print $4}')
vHeader=$(echo $pVersion | sed -e 's/\.//g' -e 's/^/v/')
pChangeLog=$2

export PVERSION=$pVersion
export PREVISION=$pRevision
export PLISTPATH="$3"

python - <<END
import os
import plistlib

pVersion=os.environ['PVERSION']
pRevision=os.environ['PREVISION']
plistPath=os.environ['PLISTPATH']

Plist=plistlib.readPlist(plistPath)

Plist['ProductVersion'] = pVersion
Plist['Revision'] = pRevision


plistlib.writePlist(Plist, plistPath)

END

perl -spi -e "s/version=\"*.*.*.*\"/version=\"$pVersion\"/g" somescript.sh

What I'm stuck with is the next bit.

Code:
perl -spi -e "s/change\ log/change\ log\n#\t$pVersion\n#\t-$pChangeLog/g"

Well, maybe not stuck. It works fine. But, if I could somehow make this perl one-liner also wrap text at 80 columns and add a "#\t\t" at the start of each line (and make me a sandwich).... Is that just asking too much? Maybe I should cut down on the energy drinks.

---------- Post updated at 23:21 ---------- Previous update was at 23:20 ----------

These are the things I do when I'm bored.
# 2  
Old 12-07-2014
It's doable, but then the one-liner would no longer be a one-liner, imho.

You might find following alternative viable/useful
Code:
#!/bin/bash

pVersion="$1"
pChangeLog="$2"


perl -spi -e "s/version=\"*.*.*.*\"/version=\"$pVersion\"/g" somescript.sh

if [ ${#pChangeLog} -gt 71 ] # 80 - 8 spaces from tab - 1 hypen
then    # wrapping necessary

MLINE=$(
    # first line
    line="${pChangeLog:0:71}"
    printf "#\t-%s\n" "$line"
    pChangeLog="${pChangeLog:71}"

    # all the rest
    while [ ${#pChangeLog} -gt 0 ];
    do
        line="${pChangeLog:0:64}" # 80 - 16 spaces from two tabs
        printf "#\t\t%s\n" "$line"
        pChangeLog="${pChangeLog:64}"
    done
)

    perl -spi -e "s/change\ log/change\ log\n#\t$pVersion\n$MLINE/g" somescript.sh

else    # fits in one line

    perl -spi -e "s/change\ log/change\ log\n#\t$pVersion\n#\t-$pChangeLog/g" somescript.sh

fi

Demo:
Code:
$ cat somescript.sh
#!/bin/coolcli

# version="w.x.y.z"
# change log

# <------ program code below this line  ------>
$
$ ./update.sh 0.0.0.1 "first version"
$ ./update.sh 0.0.0.2 "added function x"
$ ./update.sh 0.0.1.0 "some text"
$ ./update.sh 0.0.1.1 "some more text"
$ ./update.sh 0.1.0.0 "A changelog is a log or record of changes made to a proje
ct, such as a website or software project, usually including such records as bug
 fixes, new features, etc. Source: wikipedia"
$ 
$ cat somescript.sh
#!/bin/coolcli

# version="0.1.0.0"
# change log
#       0.1.0.0
#       -A changelog is a log or record of changes made to a project, such as a 
#               website or software project, usually including such records as b
#               ug fixes, new features, etc. Source: wikipedia
#       0.0.1.1
#       -some more text
#       0.0.1.0
#       -some text
#       0.0.0.2
#       -added function x
#       0.0.0.1
#       -first version

# <------ program code below this line  ------>
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PERL one liner

hi, I am using PERL one liner for oracle database connection as : $PERL -e "use DBI; DBI->connect(qw(DBI:Oracle:SID user passwd));" is there a way to append select statement to this connection ? i.e. DB connection and select stmt in one line ? how to do sysdba connection using one lines... (1 Reply)
Discussion started by: talashil
1 Replies

2. UNIX for Dummies Questions & Answers

Bash does not wrap long lines correctly

Ksh is my default shell, but I want use the bash shell since its convenient to me. When I type a long command line in a terminal, it does not wrap to the next line when I reach the end of the line and it wraps onto the same line, overwriting my prompt and the rest of what I typed. $... (5 Replies)
Discussion started by: senthil.ak
5 Replies

3. Shell Programming and Scripting

Comment ( -- ) lines from 10 to 25 for every .sql file

Platform : RHEL 5.4 I have several .sql files in a directory. I want to comment lines 10 to 25 for all .sql files. How can I do this ? The symbol for comment in SQL is -- eg: -- select salary from emp where empname = 'URS' ; (3 Replies)
Discussion started by: omega3
3 Replies

4. Shell Programming and Scripting

Comment lines in FSTAB using perl

Hi All, I need to comment specific Two Lines in fstab & want to do using & also want to ensure that is done corretly. I am trying the below method. But its giving Search pattern not terminated. ################ b36376 67 % cat linux-fstab_testing | perl -i -wnl -e '/^('\Q... (1 Reply)
Discussion started by: ajaincv
1 Replies

5. Shell Programming and Scripting

How can i comment out a section between two particular lines

I want to find out which files under /etc have the the following section: and then i would like to comment out the above section in all the files. Please help. (3 Replies)
Discussion started by: proactiveaditya
3 Replies

6. Shell Programming and Scripting

Search & Replace regex Perl one liner to AWK one liner

Thanks for giving your time and effort to answer questions and helping newbies like me understand awk. I have a huge file, millions of lines, so perl takes quite a bit of time, I'd like to convert these perl one liners to awk. Basically I'd like all lines with ISA sandwiched between... (9 Replies)
Discussion started by: verge
9 Replies

7. Shell Programming and Scripting

Wrap lines with awk to create SQL script

Greetings! Some of my files list hardware errors (we test electronic components), some have none. If the file name has no errors, I still want to display a message like "No error", else I display the error from the file itself. I came up with this (with help) for myfile in `find . -name... (2 Replies)
Discussion started by: alan
2 Replies

8. UNIX for Dummies Questions & Answers

comment lines

Is there a command to put a comment (#) in a whole part of a shell script? (5 Replies)
Discussion started by: vero_81
5 Replies

9. Shell Programming and Scripting

Perl One Liner

Hi , Can anybody explain how this perl one liner works.. It is to test whether the number is prime or not perl -le 'print "PRIME" if (1 x shift) !~ /^(11+)\1+$/' 19 Thanks in advance Shihab (2 Replies)
Discussion started by: shihabvk
2 Replies

10. Shell Programming and Scripting

how to comment multiple lines in unix

hi all, please help me how to comment multiple lines in unix script. thanks in advance --bali (3 Replies)
Discussion started by: balireddy_77
3 Replies
Login or Register to Ask a Question