Tag Info

Hot answers tagged

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.


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


6

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


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


4

With GNU sed: sed -i '1s/^/insertedtext/' file This replaces the beginning of the first line with the inserted text. -i replaces the text in file rather than sending the modified text to the standard output. (Thanks to glenn jackman for suggesting dropping the capture I had previously.)


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


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.


4

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


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


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

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


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


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]*\)\(.*\) ...


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


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


2

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


2

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


2

GNU sed will do. sed -i '1s/\(.*\)/insertedtext\1/' However, note that appending at the beginning of the file requires rewriting of its whole content. In case of small files it is not a problem, but if the file in question has several dozens of gigabytes, then it might become a tricky task to do it efficiently. In such cases you usually want to make some ...


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

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;


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

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 } '


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


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.


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


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



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