Tag Info

Hot answers tagged

4

If I understand correctly, you're looking for perl -lane 'push @{$k{$F[0]}},$F[1]; END{$"=", ";print "$_ @{$k{$_}}" for sort keys(%k)}' file There's no reason to explicitly split the line, the -a flag already does it for you. It will split each input line into the @F array. So, here we create the %k hash whose keys are the 1st fields and ...


3

Perl: perl -ne ' BEGIN {$width = 10} while (length) { ($word = substr($_,0,$width)) =~ s/^\s+|\s+$//g; # trim whitespace $word ||= "-"; print $word, " "; substr($_,0,$width) = ""; } print "\n"; ' file


3

Another perl: $ perl -anle ' push @{$h{$F[0]}}, $F[1]; END { for (sort {$a <=> $b} keys %h) { print "$_ ", join(", ", @{$h{$_}}); } } ' file 1 foo, foobar 2 bar, barbar 3 apple, mango 4 banana 5 orange Explanation We create a hash $h, which has keys are value $F[0]. Each hash's value is a array reference. ...


3

The details of this will depend on how variable your input is. If we can assume that JOURNEY is unchanging and that the numbers you want added to it will never be more or less than two characters (01-99), this will work: perl -pe 's/^.(\d+) ## ignore the first character and capture ## as many digits as possible after it. ...


3

Assuming your shell is bash, this can be a one-liner: perl -i.bak -pe ' /\\begin\{verbatim\}/../\\end\{verbatim\}/ or s/->/\$\\to\$/g ' {Cardiology,Pathophysiology,"Patology and Biopsy",Physiology,Propedeutics,Radiology,Rheumatology,Surgery}/*.tex Note that {...} is a regex quantifier, so the braces need to be escaped. I'd write your code as: ...


3

Why don't you directly give / as parameter to df -k? df -k / | tail -n +2 This will give the list without the header.


3

If you don't need to keep the whitespace exactly the same, this works: $ perl -lane 'do{$_=sprintf("%.8f",0) for @F[3..6]} if $F[6]>1; print "@F"' file 0.04718683 0.24081557 0.02831210 0.00000000 0.00000000 0.00000000 0.00000000 0.05662420 0.24081557 0.02831210 -0.00006975 0.00001697 -0.00144172 0.00144350 0.06606157 0.24081557 0.02831210 0.00039672 ...


3

Try this: pcregrep -M '\\begin{question}(.|\n)*?\\end{question}' Explanation: pcregrep: grep with Perl-compatible regular expressions -M: Allow patterns to match more than one line (.|\n)*?: any normal character . or new line \n matched zero or more times ., in non-greedy mode ?. Result: \begin{question} {You get a patient. What do you notice ...


2

Try this (assuming GNU grep): for pdf in *pdf; do mv "$pdf" "$(grep -aE 'VAX[0-9]*' "$pdf" | sed 's/.*\(VAX[0-9]*\).*/\1/)" done Note that this assumes only one instance of VAXNNNNNNN. If you can have more, use the first match only: for pdf in *pdf; do mv "$pdf" "$(grep -m 1 -aE 'VAX[0-9]*' "$pdf" | sed 's/.*\(VAX[0-9]*\).*/\1/)" done


2

I was going to extend my original code but it is getting to a point where it's much easier to just reimplement everything in Perl directly: #!/usr/bin/env perl ## This is the path to the target directories my $path="/Users/Masi/Dropbox/"; ## The target directories my @directories=("Cardiology","Rheumatology","Surgery"); ## Iterate over the directories ...


2

Using sed: sed -i.bak -e '/^[0-9]\{1,9\}$/d' file Using perl: perl -i.bak -nle 'print unless /^[0-9]{1,9}$/' file


2

Assuming you want to remove any cases of : followed by numbers from the 4th column (you say the 6th in your question but it's the 4th in your example), you can use one of these: awk awk -F';' -v OFS=";" '{sub(/\.[0-9]*$/,"",$6);}1;' file.csv > new.csv Perl (this changes the actual file and keeps the original as file.csv.bak) perl -i.bak -F';' -ane ...


2

Here I do it with sed: sed '/,/!b s/\( *[^ ]*\)\(.*\)/\2,\n\1/;:t s/\([^,]*,\)\(\n.*\)/\2\1/;tt s/\n\(.*\),/\1/' <<\DATA LABEL1 .BYTE 01, 02, 03, 04, 05 .BYTE 01, 02, 03 LABEL1 .BYTE 01, 02, 03, 04, 05 .BYTE 01, 02, 03 DATA OUTPUT LABEL1 .BYTE 05, 04, 03, 02, 01 ...


2

Here's how I would do it: perl -MTie::File -e' tie @lines,"Tie::File","your_file"; for(@lines){ next unless /,/; # Skip lines with no commas $csv = /(\s*[^,\s]+,.*)/; $new_csv = join ",",reverse split /,/,$csv; s/\Q$csv/$new_csv/; }' Disclaimer!! This will modify your file in situ. Use a dummy copy of the file ...


2

sed '/^#/N;s/\(.*\)\n\([^#].*\)/\2 \1/;P;D' That will handle your simple example in the question - any commented line that is followed by a line that is not a comment and consists of at least one character will be appended to the line that follows it. So, running your example through it and the output is: sp_after_tparen_close = ignore # ...


1

That worked for me: sed 's/^ //g' input | tr -s "[:blank:]" "," | \ awk -F, '!$1{printf "- "} $1{printf "%s ", $1} \ !$2{printf "- "} $2{printf "%s ", $2} \ !$3{print "-"} $3{print $3}' Explanation: sed 's/^ //g' input removes a leading space if present tr -s "[:blank:]" ",": replaces all spaces with comma awk ...: uses the delimiter , and prints ...


1

The following sed plus gawk (GNU awk) solution performs the requested task: sed -e '/ .BYTE/{s/\$/0x/g;s/,//g}' INPUTFILE.txt | gawk --non-decimal-data '/.BYTE.*0x/{ printf ".BYTE $%x, $%x\n", ($5+$4*4+$3*16+$2*64 ) , ($9+$8*4+$7*16+$6*64) };!/.BYTE.*0x/{print}' The sed call replaces $01 with 0x01 and delete commas in the .BYTE lines which ...


1

As per my comment, it is easiest to develop something like this in a short script rather than fiddling with command-one-liners. Also, you can keep it. #!/usr/bin/perl use strict; use warnings FATAL => qw(all); my $buffer = ""; my $linesBuffered = 0; while (<STDIN>) { # Check if this line is a just a comment. if ($_ =~ /^\s*#/) { # Assume ...


1

I believe that this does what you want: awk '/^[[:space:]]*[^#]/ && last ~ /^#/ {printf "%s %s",$0,last; last="";next} {print last; last=$0} END{print last}' sample.cfg As an example, suppose that we have this input file: # # Some comments # # Add or remove between the parens in the function type: 'void (*x)(...)' sp_after_tparen_close = ...


1

File revbytes2.awk: #!/usr/bin/awk -f BEGIN { FS=",? +" } NF>2 && match($0,"^ +\.BYTE ") { printf substr($0,1,RSTART+RLENGTH-1) for(i=NF;i>3;i--) printf $i", " print $3 next } 1 FS=",? +" makes awk recognise the space after .BYTE and the , plus space sequence between the bytes as field separator. For ...


1

With your input, you can use perl: $ perl -MText::Tabs -anle ' BEGIN {$tabstop = 4}; print and next if /^\S/; @nums = grep { $_ =~ /\d+/ } @F; map { s/\D//g } @nums; map { $_ = (pop @nums) . (@nums==0 ? "" : ",") if $_ =~ /\d+/ } @F; print expand "\t@F"; ' file LABEL1 .BYTE 05, 04, 03, 02, 01 .BYTE 03, 02, 01 I ...


1

You could try the below Perl one-liner. perl -00pe 's/(\\"(?:(?!\\"|\|).)*)\n((?:(?!\\"|\|).)*\\")/\1\2/g' file Example: $ cat file test \" data get this line\" in above line test \" data keep| this line\" here $ perl -00pe 's/(\\"(?:(?!\\"|\|).)*)\n((?:(?!\\"|\|).)*\\")/\1\2/g' file test \" dataget this line\" in above line test \" data keep| this ...


1

This is easy enough to set up using the fnotify script. Set it to autoload and then customise it to print to a file of your choosing: sub filewrite { my ($text) = @_; # FIXME: there is probably a better way to get the irssi-dir... open(FILE,">>$ENV{HOME}/path/to/your/fnotified"); print FILE $text . "\n"; close (FILE); } Then, ...


1

With awk, and guessing what you wish file ul.awk (edited) /JOURNEY/ { jn=substr($1,2,1) ; x=substr($1,10,1) ; y=substr($1,13) ; printf "JOURNEY_%02d%s; %02d TO %02d\n",jn,substr(" ",1,31),x,y ; next ; } {print ;} and then run awk -f ul.awk u JOURNEY_01 ; 00 TO 01 .BYTE ...


1

The problem was from Urxvt.* . I changed it to the URxvt.* and It works now. I also put matcher after default. (but I down't think it had any thing to do with the problem.)


1

In double quotes, you need to backslash backslashes, i.e. double the backslash before the dot. system("grep '^.*\\.[a-zA-Z0-9][a-zA-Z0-9]*\$' file.txt > file2.txt"); # ^ # | # Here.


1

Do you need it to be a pure regex solution, or just perlish? perl -lne 'print if(/^\\begin{question}/ .. /^\\end{question}/)' file


1

For automatically converting CSV files to XLS/XLSX ones you can also use ssconvert (which comes with Gnumeric) or unoconv (which comes with LibreOffice). SSConvert Example $ echo -e 'surname,name,age\nCarlo,Smith,23\nJohn,Doe,46\nJane,Doe,69\nSarah,Meyer,23\n' \ > example.csv $ unix2dos example.csv $ ssconvert example.csv example.xlsx $ ssconvert ...


1

If you don't want to say ' /$' you can use the sets name for space too, i.e. [[:space:]]. Example: $ df -k | grep '[[:space:]]/$' /dev/mapper/fedora_greeneggs-root 51475068 24631544 24205700 51% / If you do testing though, you'll notice that inclusion of the space is unnecessary since / is the only entry in the output that is terminated by the EOL ...


1

Try: df -k | grep '/$' or you can use awk: df -k | awk '$NF == "/"'



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