Tag Info

Hot answers tagged

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'


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 ...


5

The problem is with your double quotes. Here you don't need to quote those hash keys as they are simple identifiers. From perldoc perldata: In fact, a simple identifier within such curlies is forced to be a string, and likewise within a hash subscript. Neither need quoting. Our earlier example, $days{'Feb'} can be written as $days{Feb} and the quotes ...


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


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


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


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


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') ...


2

With sed: sed '/^$/{$!{N;/\n$/D;s/.//;$!h;$p;d};};//!{H;1h;$!d};$x' infile this should print the last set of non-empty lines without any leading/trailing empty lines. e.g. iostat -d 1 2 | sed '/^$/{ # if the line is empty $!{ # and if it's not the last line N # then pull in the next line ...


2

It seems you need to chomp the $interface.


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

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


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

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 ...


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 ...


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.


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 ...


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 ...


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

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 ...


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 ...



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