Tag Info

New answers tagged

1

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


0

One of the ways in which you can solve this is: Open the input file Store the first line of the input file (the header) For every line in the input file after the header: Read the first two columns If we haven't opened an output file for the fields you want to match on yet, open a new output file and store its file handle in a hash. Write the header line ...


0

#!/bin/sh ascend() { echo $( i=0 while test $# -gt 0; do i=$(($1 > i ? $1 : i + 1)) echo $i shift done ) } ascend 2 7 8 9 11 15 34 91 91 92 94 ascend 1 1 1 1 1 1 1 1 1 1 Output: 2 7 8 9 11 15 34 91 92 93 94 1 2 3 4 5 ...


2

This is a bit too complex to be legible as a one liner so here's a commented gawk script: #!/usr/bin/gawk -f ## Save the data in array data: data[M][INS]=dinucleotide NR==FNR{ data[$2][$1]=$3; next } ## Save the groups in array groups: groups[GRN][INS] { groups[$1][$2]++ } ## Now that everything is stored in memory, analyze END{ ## Get ...


0

awk ' { if($1$2==cust){ if(startdate<$3){ custline=$0 startdate=$3 enddate=$4 next } if(startdate==$3 && enddate<$4){ custline=$0 startdate=$3 enddate=$4 next } }else{ if(custline!=""){ print custline } custline=$0 cust=$1$2 startdate=$3 ...


0

This is fairly simple with AWK: File1 into File2 before pattern = "Pointer" First load the contents of File1 into a variable f1="$(<File1)" then do the insertion awk -vf1="$f1" '/Pointer/{print f1;print;next}1' file2 (Or, if you want to insert File1 after "Pointer") awk -vf1="$f1" '/Pointer/{print;print f1;next}1' file2


2

Here's your perl one-liner: it works with multiple file arguments perl -i -pe '/^$ARGV,/ or print "$ARGV,"' file1 file2 ... $ARGV is the magic variable that holds the filename of the current file. See http://perldoc.perl.org/perlvar.html#Variables-related-to-filehandles The field separator (comma) is hardcoded. You can decide if that's a problem. Small ...


1

While you can actually do this with Perl, the syntax is not the simplest (or at least, it isn't with the best I can come up with). It will probably be both simpler and faster to use other tools. For example, sed gawk (relatively recent versions) for f in file*csv; do awk -i inplace -F, '{ if($1==FILENAME){print} else{print ...


1

Can't manage a one liner, but here's a perl script. Put it in a file and make it executable. Then give it the *.csv filenames as args. It creates *.new files. If you are confident it works, uncomment the rename command at the end. #!/usr/bin/perl use strict; foreach my $file(@ARGV){ open(F,$file) or die "$file:$!"; $_ = <F>; next if ...


0

OK, the problem with a 'perl one liner' as you note: perl -p -i -e 's/^/Welcome to Hell,/' file*.csv This applies a transform to the file right enough, but perl 'handles' opening the file(s) and streaming them through STDIN automagically. Which means you don't know your file name when you're doing it. The in place edit option (-i) is a convenience but ...


2

Before told about perl speed try to speed up your own script for f in *file*.csv; do sed -i "/^$f,/! s/^/$f,/" "$f" done


3

Using awk and an associative array keyed on the first two columns: awk -F : '{ sum[$1 FS $2] += $3; }; END { OFS=FS; for (key in sum) print key, sum[key]; }' file


2

I'm going to assume you want the second line merged as well. You can use join: join -t : file1 file2 This parses file1 and file2 using : as separator, and merges lines whose first field matches. By default lines which don't match are ignored and don't appear in the output. The input files need to be sorted on the join field; if they're not, you can ...


2

use Net::OpenSSH and let it do the quoting for you: use Net::OpenSSH; my $ssh = Net::OpenSSH->new('id@host'); my $output = $ssh->capture("/usr/message/send", -pin => $pager_num, -message => $message); $ssh->error and die "ssh failed: " . $ssh->error;


6

There are two basic approaches one can use when dealing with fields: i) use a tool that understands fields; ii) use a regular expression. Of the two, the former is usually both more robust and simpler. Many of the commonly available tools on *nix are either explicitly designed to deal with fields or have nifty tricks to facilitate it. 1. Use a tool that ...


0

Perl module names correspond to a .pm file path out of a necessity -- not any specific rule of the languague, but because that's how perl finds them. This assumption can be used with apt-file search to determine which distro package, if any, contains that file: apt-file search "/Future/Utils.pm" There are a lot of modules with no package, however, and ...


2

If you want strict and warnings, the answer is 'don't do a one liner'. Almost invariably one liners suffer from perl's propensity for write-only code. Expand it out into a multi-liner instead. If you really want - you can embed it using a HEREDOC perl <<ENDPERL use strict; use warnings; print "This is a test\n"; ENDPERL Or alternatively: ...


1

On my Debian system running your command gave me this result apt-get install libfuture-utils-perl Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package libfuture-utils-perl However, I found the library was available as a package by searching: apt-cache search libfuture libfuture-perl - module ...


1

I would first extract candidates, then check for the maximum 777 later: egrep '^[0-7]?[0-9][0-9]?)' file | sed 's/^\([0-7]\?[0-9][0-9]\?\))/\1 )/' | awk '($1 < 778) {print $0}' | sed 's/^\([0-7]\?[0-9][0-9]\?\) ).*/\1,/' | tr --delete \\n | sed 's/,$/\n/' It works for my test files. (EDIT1: The last sed now gives the number only EDIT 2: removed ...


2

perl -ne 'print unless /hernia|ignshello|...|n/' file Your RE as provided will match any line of three of more characters (the ...) or any line containing the letter n. I assume these aren't intentional, but may be why you consider it "unstable". Since you haven't got any variables present in this one-liner, the use warnings and use strict would be of ...


1

As some clarification iterations showed, only integer numbers and with values not larger than 777 shall be collected into a comma separated list. Here we go: awk -F ')' '$1~/^[0-9]+$/ && $1<=777 {print $1}' <datafile | paste -sd, Note: To match an integer range you can in awk also write: awk -F ')' '$1>=1 && $1<=777 {print ...


4

sed '/./s/^/\\section{}/' Would prepend \section{} to every line that contain at least one valid character. sed '/^$/!s/^/\\section{}/' Would prepend \section{} to every non-empty line (that is lines that contain at least one byte). sed 's/./\\section{}&/' Would insert \section{} before the first valid character in every line (that has such a ...


2

You could do it in two statements with sed : the first one to ensure no empty lines are matched, the second to append at the beginning : sed '/^$/n;s/^/\\section{}/' Or you could do it in one statement by capturing what you match and adding it to your replacement : sed 's/^\(.\)/\\section{}\1/' Here, the parentheses \( and \) define a capturing group ...


1

Use -n instead of -p: perl -ne 'print unless /[0-5],[0-5]?|[0-5].,[0-5]?/' file -n processes the input line by line, but doesn't automatically print each line. I'm not sure what your goal is, so you might need to tweak the regexes, but the general approach should work.


1

sed -n 'p; /Theoretical/q' This will print until the regular expression matches for the first time then quit.


1

Just use a hash: #!/usr/bin/perl -w use strict; open(my $fh, "<", "/var/ldt/ldt.conf") || die "Can't open file: $!\n"; my %vars; while(<$fh>){ ## remove trailing newlines chomp; ## Split the line on = my @F=split(/=/,$_,2); ## remove quotes $F[1]=~s/^['"]//; $F[1]=~s/['"]$//; ## Save the values in the hash ...


0

Here is a one liner I wrote the other day for very similiar purpose. Assuming you understand chown/chmod ,So can adapt for your purposes. In this case, some user accounts are nested under the userX account. The problem was files uploaded by those users were still owned by otheruser:otheruser, So userX could not affect them, This script runs every minute to ...


2

You can use find for that: find / -type d -group web -exec chmod g+rx {} + This is slightly inefficient as it will also set group to rw for those that already have those set. You can also have find check on some of the permission bits with -perm /mode and negate that match.


3

There are two issues I see. The first is floating point math. Floating point math always has some error, although in this case It does not look to be significant. The other is that you are not doing error checking on your chdirs. I think Lambda_0.2/Production_MD is missing, which will confuse everything from there as you see. This may be an easier solution: ...


9

With GNU sed​: sed 's/;/|/2g' Which globally replaces ; with | starting from the 2nd occurrence. While sed 's/;/|/2 and s/;/|/g are POSIX, the combination is not and the behaviour varies across implementations. With the GNU implementation of sed however, the behaviour is clearly documented.


2

I didn't notice an awk version, so here it is: awk -F';' '{for(i=2;i<=NF;i++)gsub(" ; "," | ");print}' testfile1.txt Output example: $ cat testfile1.txt Question ipsun; option 1 ; option 2 ; option 3 ; option 4 ; ... ; option n Question ipsun; option 1 ; option 2 ; option ...


4

Simply sed -e 's/;/|/g' -e 's/|/;/' data.csv gets you: Question ipsun; option 1 | option 2 | option 3 | option 4 | ... | option n Which looks like what you wanted.


7

sed 'y/|;/\n|/;s/|/;/;y/\n/|/' <<\IN Question ipsun; option 1 ; option 2 ; option 3 ; option 4 ; ... ; option n IN Note that this does not use a regexp to handle the majority of the replacements, but rather uses a more basic (and far more performant) translation function to do so - and does so in a POSIX portable fashion. This should work on any ...


3

The example in your question looks like the ls -l output, not the lsattr output. In the ls -l output, the first field is the mode, that is the type (regular, directory, symlink...) and permissions. The S bit at that position means setuid but without execute permission for the user. Here given that none of user/group/other have execute permissions, that ...


3

find -perm Is what you want - it'll allow you to specify an octal mode for 'find' to ... well, find. You can find which perm to look for with 'stat' which will give you what it currently is. So e.g. find . -perm 4750 I don't recognise your bit flags well enough to tell you the octal mode of them, so you'll have to look for yourself. Edit: As ...


2

You made a slight mistake: ~$ perl -00 -pe 's/;/\0/; s/;/\n/g; s/\0/;/' file Question; option 1 option 2 option 3 option 4 ... option n what you have done: s/;//g; # remove all other semicolons s/\0/;/ # restore the first semicolon what you stated you want: s/;/\n/g; # change all other semicolons s/\0/;/ # restore the first ...


2

One way with awk: awk ' !NF { c=0 ; print ; next } { sub(/file.*txt/,"file"++c".txt") sub(/>file </,">file "c"<") print } '


3

sed 's|\([^.e]*\).txt">.... |&\1|' <in >out And... you simplified it again. And... complicated it again. So the above will just copy numbers that are already in the string - it doesn't fix any broken ones. This will: { sed '/./!G;s/\n/::::::&::::/ s/\(.*[^0-9]\)[0-9][^.]*/\1 /' | nl -d::| sed 's/ *\([0-9]*\)\(.*\) ...


1

You can try this with awk: awk -F"," 'NR == 3, NR == 8 { for (i = 1; i <= NF; i++) { if ($i == 0) { cnt++; } if ($i >= 2.452555e-05 && $i <= 0.0032784) { cnt1++; } } } END { print cnt, cnt1; }' file


1

Your starting point is good; now you have to iterate over the fields, where a precondition is to define an appropriate field separator. To count the zeroes: awk ' BEGIN { FS="[, ]+" } NR==3, NR==8 { for (i=1; i<=NF; i++) if ($i==0) c++ } END { print c } ' For checking a range change the if condition accordingly, something like: if ($i >= ... ...


0

sed -n '/^[0-9]/!H;//x;$x;s/\n\([^A]\)/ \1/gp' <infile >outfile Seems to do the trick: /^[0-9]/!H If a line does !not begin with a digit, then append it to Hold space following a \newline. //x;$x If it does begin with a digit, and/or if this is the $last line, exchange pattern and hold spaces. s/\n\([^A]\)/ \1/gp If 1 or more sequences of a ...


2

Here is an awk solution that solves the issue by appropriate definitions of what's the field and record separators for input and output; thus the effective command ($1=$1 FS) is quite simple: awk ' BEGIN { RS="" ; FS="\n" ; OFS="" ; ORS="\n\n" } $1=$1 FS ' Explanation: RS="" - will handle blocks of empty line separated data as one record FS="\n" - ...


2

I would use perl or awk to read the data a paragraph at a time, and remove all but the first newline: perl -00 -pe '$\="\n\n"; s/\n/\0/; s/\n//g; s/\0/\n/' file Commented perl -00 -pe ' # each record is separated by blank lines (-00) # read the file a record at a time and auto-print (-p) $\="\n\n"; # auto-append 2 newlines to ...


2

You can use: sed '/[0-9]\./{n;:l;N;/\n$/!s/\n/ /;t l}' file This will output: 4. Alendronic acid A. Antiosteoporotic agent. B. Inhibit osteoclast formation and function by inhibiting FPPS enzyme, so increase bone mass. C. Osteoporosis in combination with vitamin D. 5. Aminophylline A. Methylxanthine. Less potent and shorter-acting bronchodilator than ...


2

In perl perl -pi -e 's/^(.*)/insertedtext$1/ if $.==1' myfile.txt


2

Your Package Manager is now a defunct PerlScript. I Quote: What, you're afraid to run it? Don't worry. It's harmless. In fact it doesn't do anything anymore. It used to do something (5 points if you can tell me what), but then bitrot set in. There's a moral to this story (worth 10 points). Taken from: ToastBall.net Please note that I did not ...


2

Another variation - not more or less correct, just a matter of taste: awk 'BEGIN{printf "insertedtext"};{print $0}' file1.txt > file2.txt


0

<<IN head -n-1 >textfile insertedtext$( cat <textfile printf \\n.) IN In those shells which handle heredocuments with tempfiles (to include bash and zsh but not dash, busybox, or yash) the above command will safely overwrite the whole of textfile with itself and include the prepended string. The trailing \n. is added to preserve any trailing ...


4

POSIX one: $ { printf %s insertedtext; cat <./input_file; } >/tmp/output_file $ mv -- /tmp/output_file ./input_file


6

If portability across unices is a concern, use ed: ed file <<END 1s/^/insertedtext/ w q END



Top 50 recent answers are included