Tag Info

Hot answers tagged

23

That's for compatibility with the Bourne shell. The Bourne shell was an old shell that was first released with Unix version 7 in 1979 and was still common until the mid 90s as /bin/sh on most commercial Unices. It is the ancestor of most Bourne-like shells like ksh, bash or zsh. It had a few awkward features many of which have been fixed in ksh and the ...


14

In Larry Wall's original Perl v1.0 posting to the comp.sources.misc newsgroup on December 18, 1987, he said: If you have a problem that would ordinarily use sed or awk or sh, but it exceeds their capabilities or must run a little faster, and you don't want to write the silly thing in C, then perl may be for you. In a much later ...


14

I wonder if this is turning into a golf match: sed 'p;p;p' awk '1;1;1;1' perl -lpE 'say;say;say' # if Paul McCartney and Michael Jackson were hackers... Explanation: sed's p command is to print the current line. The default behaviour is to print the current line just before moving to the next line (that's why sed has -n to allow you to turn it off). ...


13

The answer is/isn't sexy, depending on your point of view. Perl is very useful. Lots of the system utilities are written in or depend on perl. Most systems won't operate properly if Perl is uninstalled. A few years ago FreeBSD went through a lot of effort to remove Perl as a dependency for the base system. It wasn't an easy task.


13

How about cut -d. -f1 numbers.txt | sort | uniq -c Using your example data, $ cut -d. -f1 numbers.txt | sort | uniq -c 3 24 4 25 1 26 1 29 3 30


13

sed creates a temporary file, writes the output into that file, and then renames the temporary file over the top of the original. You can watch what happens using strace: $ strace -e trace=file sed -i -e '' a execve("/usr/bin/sed", ["sed", "-i", "-e", "", "a"], [/* 34 vars */]) = 0 <...trimmed...> open("a", O_RDONLY) = 3 ...


12

Try this: lslpp -l | grep perl perl -v


12

This seems to work, but I've not given deep thought to it: sed -e '/^[[:space:]]*#/d'


12

With core module Scalar::Util, you can do: $ perl -MScalar::Util=looks_like_number -anle ' $count += grep { looks_like_number($_) } @F; END { print $count } ' file 33 More about looks_like_number can see in perldoc perlapi.


10

You can use grep for that grep -vh '^[[:space:]]*#' filename Since, as I presume, you are stripping comments from some file, you might also consider removing empty lines, which expands the above to: grep -vh '^[[:space:]]*\(#\|$\)' filename


10

find(1) is powerful enough to do what you need. Simply collect all of the conforming names into an expression using parentheses, then negate it to show non-conforming file names. For example, to show all files not named *.txt, *.bz2, or *.zip: $ find . \! \( -name \*.txt -o -name \*.bz2 -o -name \*.zip \) You can use -not instead of \! with GNU and BSD ...


9

You can't. The Perl script runs in a process which is a child of your shell session. This child process can change its own working directory all it likes, but it cannot change it's parent's working directory. When the Perl script exits, control is returned to the parent process (the shell session), which will have remained in the same working directory ...


9

Case 1: awk '!NF {if (++n <= 2) print; next}; {n=0;print}' Case 2: awk '!NF {s = s $0 "\n"; n++; next} {if (n>1) printf "%s", s; n=0; s=""; print} END {if (n>1) printf "%s", s}'


9

# starts a comment in the shell. Add quotes: $message =~ s/\#/\\\#/g; $execute=`ssh -q id@host /usr/message/send -pin $pager_num -message "'$message'"`;


9

Use system LIST, which doesn't invoke a shell. i.e. system('/bin/bash', '-c', $your_command); instead of system("/bin/bash -c '$your_command'");


9

UPDATE: Actually, a much easier way is to set the record separator in gawk: $ gawk 'BEGIN{RS="\"\n"; FS=","}{print $4}' myFile.csv "col4 "4th column "4th column2 However, this will remove the trailing " from the end of each column. To fix that you can print it yourself: $ gawk 'BEGIN{RS="\"\n"; FS=","}{print $4"\""}' myFile.csv "col4" "4th column" "4th ...


9

1. Replacing one string with another in all files in the current directory: These are for cases where you know that the directory contains only files and that you want to process all files. If that is not the case, use the approaches in 2. Non recursive, files in this directory only: sed -i 's/foo/bar/' * perl -i -pe 's/foo/bar/' * Recursive, files in ...


8

If you just want to write a single y to the stdin of the process, you can do this: (echo y | nohup myprocess) & If you want to keep writing y for every prompt that comes up, the coreutil yes exists for exactly this purpose -- it will keep writing whatever you tell it to to stdout. Its default is to output "y", so you can just: (yes | nohup myprocess) ...


8

sed has a function for that, and can do the modification inline: sed -i -e '/Pointer/r file1' file2 But this puts your Pointer line above the file1. To put it below, delay line output: sed -n -i -e '/Pointer/r file1' -e 1x -e '2,${x;p}' -e '${x;p}' file2


8

In order to use join, you need to make sure that FILE1 and FILE2 are sorted on the join fields. The following command should do the trick: join -v1 <(sort file1.txt) <(sort file2.txt)


8

awk -v 'OFS=\t' 'NF == 2 { print $1, "none", $2; next } 1' input.txt > output.txt Adjust depending on the characteristics of your input file. I assume every line with only 2 fields should have a "none" inserted. Otherwise, all other lines are just passed through unchanged (the purpose of the 1 at the end).


8

awk -F , -v OFS='\t' 'NR == 1 || $6 > 4 {print $1, $6, $7, $8}' input.txt


8

To skip every other > ... block: awk '/^>/ { p = !p } p' input.txt p is a print flag - p means print when p is true, as the default action is print when none is provided. In awk, variables start out empty, which evaluates to false in Boolean contexts. Every time a > ... line is reached, toggle the p flag. To print every nth block delimited by ...


8

Perl: perl -ne 'for$i(0..3){print}' file and I have to add this one posted as a comment by @derobert because it is just cool: perl -ne 'print "$_" x4' awk and variants: awk '{for(i=0;i<4;i++)print}' file bash while read line; do for i in {1..4}; do echo "$line"; done; done < file


8

perl -pe 's|(?<=0x)[0-9a-f]{1,8}|`./convAddrs $&`|gei' perl -pe: like sed: process the input one line at a time in $_, evaluate the perl [e]xpression passed to -e for each line and [p]rint the modified $_ for each. s|X|Y|gei: substitute Y for X in $_ ([g]lobally, case [i]nsensitively, and treating Y as a perl [e]xpression instead of a basic ...


8

Use the -n parameter to run rename in a dry-run mode that is very helpful when you want to test your pattern: martin@martin ~ % rename -n s/list/list.disable/g /etc/apt/sources.list.d/*.list | sed 's/renamed as/\n =>/g' [...] /etc/apt/sources.list.d/spotify.list => /etc/apt/sources.list.disable.d/spotify.list.disable ...


8

With awk (mawk): $ awk -F . '{COUNTS[$1]++} END{for(ct in COUNTS) {printf("%d %d time(s)\n", ct, COUNTS[ct])}}' test.txt 30 3 time(s) 24 3 time(s) 25 4 time(s) 26 1 time(s) 29 1 time(s) The -F sets the field separator (FS) to ., other than that we go through all lines with the {COUNTS[$1]++}, using $1 as the part before the decimal separator (.) and ...


8

A straight Perl solution: $ perl -lne ' if(/^>/) {printf "%s ", $_;next} if(/^$/) {printf "\n";next} printf "%s", $_; ' file >Country1 Australia >Country5 Switzerland >Country2 Netherlands or a shorter way: $ perl -ane 'BEGIN{$/="";};print "$F[0] ",@F[1..$#F],"\n"' file >Country1 Australia >Country5 Switzerland >Country2 ...


7

I feel a bit stupid saying it, but did you try installing perl-doc? # apt-get install perl-doc


7

A process in S state is usually in a blocking system call, such as reading or writing to a file or the network, or waiting for another called program to finish. You can use strace -p pid to find out which system call is currently happening, it'll produce output like write(1, "foobar"..., 4096 which means that the process is trying to write 4096 bytes ...



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