Hot answers tagged

5

The POSIX module includes the strftime function, which allows via strftime(3) conversion specifier characters the desired templating of time: % perl -MPOSIX=strftime -E 'say strftime "[%F %T]", localtime(time)' [2011-02-17 10:55:37] % So in your case make it perl -MPOSIX=strftime -e ... and then printf( ($line=~/^\[\s*(\d+)\.\d+\](.+)/) ? ( "[%s]%s\n", ...


4

Using awk and reading the file twice. Save all variables in array with s on the end. Check the array on each line in the second run through and print if the line is not in array. awk 'FNR==NR{a[$0 "s"]++;next}!($0 in a)' file.txt file.txt To use a little less memory you could also do awk 'FNR==NR{!/s$/ && a[$0 "s"]++;next}!($0 in a)' file.txt ...


3

Your perl script has open CSV2, "<csv2" or die; ... open CSV1, "<csv1" or die; Where are those files located? cron's current directory is the home directory of the user. If the files are in the "newitems" directory, you have to cd there first. Make sure you're not making any other assumptions about the environment in your programs. I find this is ...


3

You can do this in several ways, e.g., the simplest way would be to sort the data and compare adjacent lines: sort foo |awk '{ if ( plural[$1] == "" ) print; plural[$1 "s"] = 1; }' Given input frog dogs cats catfish cat dog frogs output cat catfish dog frog Without sorting: #!/bin/sh awk 'BEGIN { count=0; } { words[count++] = $1; ...


3

Using a bash script: #!/bin/bash readarray -t mylist # compare each item on the list with a new list created by appending `s' # to each item of the original list for i in "${mylist[@]}"; do for j in "${mylist[@]/%/s}"; do [[ "$i" == "$j" ]] && continue 2 done echo "$i" done The list is read from stdin. Here is a test run: $ cat file1 ...


2

In an attempt to give this Q a proper answer, based - on - the - comments (heeding Sobrique's note that parsing XML should really be done with an XML parser): perl -CSD -lne 'print for /\w{63,}/g' input-file-here


2

That's a job for pax. pax is a standard POSIX command; some Linux distributions omit it from the default installation, so you may need to install the package explicitly. You don't get the full power of Perl, just basic sed regex replacement, but that's good enough for your use case. pax -rw -pe -s'|/pars/|/|' -s'|/fts/|/|' -s'|innobase/include|include|' … ...


1

You can run this code, Sub SaveSheets() Dim strPath As String Dim ws As Worksheet Application.ScreenUpdating = False strPath = ActiveWorkbook.Path & "\" For Each ws In ThisWorkbook.Sheets ws.Copy 'Use this line if you want to break any links: BreakLinks Workbooks(Workbooks.Count) Workbooks(Workbooks....


1

Unix philosophy: one tool does one thing (very well), then combine tools. I suggest that you use the tools you know. The tools that have all the features and options you need and know/master already. So, use cp, rsync, scp or whatever, then use your favorite rename command.


1

Assuming you just want the shell variables $repo $arch and $linesToWrite expanded, put them in double quotes and the rest in single quotes. Quoted strings next to each other make a single string ("a"'b' is ab). (I don't know what you are doing with the backslashes in the substitute command, so I left them, but you probably don't want them). perl -0pE 'use ...


1

$ sed -e '2,$ s/^host_name/\n&/' ravi.txt | perl -n -e 'if (! m/^$/) { chomp; $line .= $_ . ", " }; if (m/^$/ || eof) { $line =~ s/ +/ /g; # merge multiple spaces into one space $line =~ s/, $//; # strip the trailing comma ...


1

I am sure you can do it much easily in awk, but awk doesn't like me much so here is my take on it using everything but the kitchen sink. Assuming that the data is in a file called file1 export output=; while read line; do if [[ "$line" =~ "host_name" ]]; then export output="${output}\n"; fi; export output="${output}, $line"; done < file1 && echo -...


1

#! /usr/bin/perl use strict; my %words = (); my $index = 1; # keep track of the order that words were read in while (<>) { chomp; $words{$_} = $index++ } # sort %words hash by value to print words in the same order # that they were seen. foreach (sort { $words{$a} <=> $words{$b} } keys %words) { my $word = $_; $word =~ s/s$//; ...


1

With excessive use of grep's -f (obtain patterns from file) option: grep 's$' input | # output: all lines ending with s sed -e 's/s$//' | # those same entries, minus the s grep -F -x -f input | # the entries whose plurals appear sed -e 's/$/s/' | # the plurals to remove grep -F -x -v -f - input


1

This is a simplified solution using awk, which does not preserve the order of words: { len = length($1); prefix = $1; if (substr($1, len) == "s") { prefix = substr($1, 1, len - 1); } if (prefix in data) { next; } else { print prefix; data[prefix] = 1; ...



Only top voted, non community-wiki answers of a minimum length are eligible