Tag Info

New answers tagged

1

From a user's perspective, a nice & simple Unix tool that does the job perfectly is qsubst. For example, % qsubst foo bar *.c *.h will replace foo with bar in all my C files. A nice feature is that qsubst will do a query-replace, i.e., it will show me each occurrence of foo and ask whether I want to replace it or not. [You can replace unconditionally ...


3

an awk answer: this will keep the order the questions the same as in the source file. $ awk '{filename = "questions" ++n[$2] ".txt"; print > filename}' questions.txt $ cat questions1.txt Q.1 2 Marks Q.2 5 Marks Q.3 4 Marks Q.4 3 Marks Q.5 6 Marks $ cat questions2.txt Q.6 4 Marks Q.7 3 Marks Q.8 2 Marks Q.9 6 Marks Q.10 5 Marks


6

sort -n -k2 -k1.3 file | awk '{$2!=a?x=1:x++} {print > "file"x; a=$2}' First , we need to sort the file correctly. -n sorts the file numerically, -k2 sorts according to the second field (the marks 2-6), -k1.3 then sorts within this order the first field starting from the 3rd character numerically (irgnoring the leading Q.). Now awk splits the output ...


0

perl -0777 -pe '1 while s/^(.)(.*)\n\1.*/$1$2/gm' file.in >file.out That slurps the entire file, and loops until no more matches can be found.


0

Maybe this is what you need: #!usr/bin/perl use strict; use warnings; my@list=qw(2 7 8 9 11 15 34 91 91 92 94); my%hash; print "Input:\n@list\n"; foreach(@list) { #count occurences for each element ...


4

Using GNU uniq with the -w option1: -w, --check-chars=N compare no more than N characters in lines you could omit consecutive duplicate lines, comparing just the first character: uniq -w1 infile >outfile 1. This won't work properly for multi-byte characters. See Stéphane's note below


1

perl -C -ne '$c = substr($_,0,1); print unless $c eq $l; $l = $c;' < file.in > file.out


0

#!/usr/bin/perl use warnings; use strict; my $first_line = <>; my $second_line = <>; if (substr($first_line, 0, 1) eq substr($second_line, 0, 1)) { print $first_line; } else { # You didn't say what to do if the character are different. }


1

sed '$n;h;N;/^\(.\).*\n\1/g;/\n/P;//!G;D' <in >out There's a sed script that will do it. It works like this: If current line is the last, print it to standard out and end the script. If not, save a copy of current line to hold space. Then append the Next input line to pattern space. If the first char in pattern space is identical to the first char ...


1

Use set -e You can write: #!/bin/bash set -e # function email_success {...} # function email_fail { ... } if /usr/bin/perl perlscript.pl; then email_success else email_fail exit 1 fi #More commands to be executed if its successful Explanation: Bash Reference Manual says: -e Exit immediately if a pipeline, which may consist of a single ...


2

You probably mean something like this: perl -pe 's|(?<=root:)[^:]*|crypt("my_password","\$6\$my_salt\$")|e' /etc/shadow From perldoc perlre: Substitution-specific modifiers described in "s/PATTERN/REPLACEMENT/msixpodualngcer" in perlop are: e - evaluate the right-hand side as an expression On the right side you can use $& to ...


1

Here is one way to do it with sed using the hold buffer: sed '/\[1em\]/{N;/Constant/{N;x;/^$/d;x};/Semester/{x;/^$/!{H;x};//g}}' file On each line matching [1em] it reads in the Next line and then If the pattern space matches Constant it reads in aNother line, exchanges the buffers and if the pattern space is just an empty line (that means the hold ...


1

Perl solution: perl -F, -le '$, = "\t"; print @F[1,5,6,7] if $F[5] > 4 || $. == 1' file -F, specifies the pattern to split on. -F implicitly sets -a -a turns on autosplit mode when used with a -n. An implicit split command to the @F array is done as the first thing inside the implicit while loop produced by the -n. -a implicitly sets -n -n ...


1

Read the first parameter from the command line somewhere near the top of the program (but before you start reading contents of the files into the $header): my $prefix = shift @ARGV; Use $prefix as part of the filename, for example like this: open( my $output, ">", "${prefix}_output.$last_sequence_number.out" ) or die $!; But I'm puzzled. Your ...


0

Use a zero-width positive look-ahead assertion: perl -pe 's/^(?=.)/\section{}/' perlre says: (?=pattern) A zero-width positive look-ahead assertion. For example, /\w+(?=\t)/ matches a word followed by a tab, without including the tab in $& Another solution: perl -lpe 's/^/\section{}/ if length' You should chomp \n at the end of a line before ...


1

Just don't use set -e and add an exit to your if fail branch. If you want that behavior for the rest of the script add the set -e after the email call.


2

Let's do this using a simple example, consider for a file, we will replace each digit of a line with the string HELLO, if the line does not have any digit then leave it as it is : #!/usr/bin/env python2 import re with open('file.txt') as f: for line in f: if re.search(r'\d', line): print re.sub(r'\d', 'HELLO', line).rstrip('\n') ...


4

Just for fun: python -c 'import sys,fileinput,re;sys.stdout.writelines(re.sub("stuff", "changed", l, 1) for l in fileinput.input() if re.search("patternmatch", l))' file Don't do it:) Use sed/perl/awk


8

Use the modifier form of if: perl -ne 's/stuff/changed/, print if /patternmatch/' or, you can use and and or for flow control: perl -ne '/patternmatch/ and s/stuff/changed/, print'


2

It seems you need to chomp the $interface.


3

With join and a shell smart enough to know how to deal with <(...): join <(sort file1) <(sort file2) | join - <(sort file3) | join - <(sort file4) Output: Bm1_00085|Bm1_22625 0.263974289 0 4 1


1

Here is one way you could do it with awk: parse.awk # Use the first column of the first file as a key and the second column # as a value in the h hash NR==FNR { h[$1] = $2; next } # If $1 is a key in h append $2 to h[$1] $1 in h { h[$1] = h[$1] OFS $2 } # When the input has been exhausted, print h key value pairs # that contain more than one element ...


0

In perl, the tool for this job is a hash. A hash is a set of key-value pairs which makes this sort of cross referencing quite easy. Note - this will ONLY work if the first field is unique: #!/usr/bin/env perl use strict; use warnings; my %data; while (<>) { my ( $key, $value ) = split; push( @{ $data{$key} }, $value ); } foreach my $key ( ...


0

The answer is, you cannot access the image location using only Tomcat. Tomcat is secured so that you won't be able to browse anything outside what is defined in deployment descriptor. In order to be able to access the image, you can write a servlet code (in Java) to do that, or you need Apache Web Server.


1

You posted an Incomplete questions, anyway, If you are not finding the image url try to inspect with the web browser inspector Firefox use firebug addson If you are using google chrome Google Chrome Use built-in google-chrome web inspector, load your project url and do CTRL + SHIFT + I Another issue with uploading the image, may be the ...


4

You can do it like this: sed -e's/ \([^ ][^ ]\)/\n\1/g' \ -e's/\([^ ][^ ]\) /\1\n/g' \ -e's/ //g;y/\n/ / ' <<\IN I have a source text file containing text where some words are l e t t e r s p a c e d like the word "letterspaced" in this question (i.e., there is a space character between the letters of the word. IN The idea ...


3

A Perl approach that mostly works: perl -C -lpe 's/(?:^|\P{L})\K\p{L}(?:\s\p{L})+(?=\P{L}|$)/$&=~s{\s}{}rgo/goe' This assumes a version of Perl recent enough to know about the /r flag in replacements. Proof of concept: $ echo 'Do I like «ł é t t ê r s p ä c è đ» text?' | perl -C -lpe ...


0

Perl's lookahead assertions make this simple. AFAIK, sed lacks these. Given that two or more whitespaces separate the words, this eliminates single spaces but leaves unaltered sequences of two or more: perl -pe 's/\s(?!\s)//g' myfile The p switch causes Perl to read myfile and then substitute single spaces (\s) which are NOT followed by another space. ...


1

This is more a find "start/running" or something similar which is better formatted as if (system("service mysql status") =~ /start\/running/) Next thing is that you will match the return code of the system command instead of the output. Use backticks (`) instead: if (`service mysql status` =~ /start\/running/) There is no not in this condition as you ...


2

sed -e's/\(\t.*\t\).* PT~/\1/;t' \ -e's/\t[^\t]*/\tNA/2' That should work for you, though it will only stop stripping chars at the last occurrence of PT in the last field on the line. Also, depending on your sed version, you may have to use literal <tab> characters everywhere I use the \t escape. The gist is to look for two tabs on a line ...


4

This is more easily done using awk in place of sed; in case awk is an option: < input awk 'BEGIN {FS=OFS="\t"} {if ($3~/PT~/) sub(/.*PT~/, "", $3); else $3="NA"; print}' Expanded: BEGIN { FS=OFS="\t" } { if ($3 ~ /PT~/) sub(/.*PT~/, "", $3); else $3 = "NA"; print } BEGIN {FS=OFS="\t"}: sets the field separator1 and ...


2

This example assumes, all the csv contents are lying in a file named a.csv...you can change it to use stdout stream instead of file stream and out of laziness, i put longitude, latitude as sub-elements.. you can put them as attributes as well from xml.etree.ElementTree import Element, SubElement, Comment, tostring top = Element('markers') f = ...



Top 50 recent answers are included