Tag Info

New answers tagged

0

Assuming that an extension starts with a letter after the period, the following command prints .tar.gz: echo gbamidi-v1.0.tar.gz | awk \ 'BEGIN { FS = "\." } \ { \ extension = ""; \ i = NF; \ while ((i > 1) && (substr($i, 1, 1) ~ /[A-Za-z]/)) { \ extension = "." $i extension; \ i-- \ ...


0

This may not be valid in all cases, but .gz is the extension. foo.tar.gz first has to be extracted to a foo.tar, then unarchived. The fact you can do that in one command is just convenience. You need to get the extension? It's .gz. If you need something else then you're going to need to target specific things using regex, awk, cut or the like.


2

As a non-regex alternative, use pup: pup 'a[href] attr{href}' < yourfile.html Will find all a elements that have a href attribute, then display the value of the href attribute. To install pup, you need Go (a programming language): sudo apt-get install golang sudo go get github.com/ericchiang/pup The advantage of this solution is that it doesn't ...


0

wget -qO- google.com | tr \" \\n | grep https\*:// ...would probably do pretty well. As written, it prints: http://schema.org/WebPage http://www.google.com/imghp?hl=en&tab=wi http://maps.google.com/maps?hl=en&tab=wl https://play.google.com/?hl=en&tab=w8 http://www.youtube.com/?tab=w1 http://news.google.com/nwshp?hl=en&tab=wn ...


5

As I said in my comment, it's generally not a good idea to parse HTML with Regular Expressions, but you can sometimes get away with it if the HTML you're parsing is well-behaved. In order to only get URLs that are in the href attribute of <a> elements, I find it easiest to do it in multiple stages. From your comments, it looks like you only want the ...


2

Not sure if you are limited on tools: But regex might not be the best way to go as mentioned, but here is an example that I put together: cat urls.html | grep -Eo "(http|https)://[a-zA-Z0-9./?=_-]*" | sort | uniq grep -E : is the same as egrep grep -o : only outputs what has been grepped (http|https) : is an either / or a-z : is all lower case A-Z : is ...


4

If your grep supports Perl regexes: grep -Po '(?<=href=")[^"]*(?=")' (?<=href=") and (?=") are lookaround expressions for the href attribute. This needs the -P option. -o prints the matching text. For example: $ curl -sL https://www.google.com | grep -Po '(?<=href=")[^"]*(?=")' /search? https://www.google.co.in/imghp?hl=en&tab=wi ...


1

sed -ne '/^[aA]$/!i\' -e failed -e 's//worked/p;q' </dev/tty


6

Just use the standard sh (POSIX and Bourne) syntax: case $answer in a|A) echo OK;; *) echo >&2 KO;; esac Or: case $answer in [aA]) echo OK;; *) echo >&2 KO;; esac With bash, ksh or zsh (the 3 shells that support that non-standard [[...]] syntax), you can declare a lower case variable: typeset -l test printf 'Enter test: ' ...


2

There are several ways to do this. If you're using a recent version of bash it's quite easy: you can convert the case of test, or you can use a regex to match both upper & lower case a. First the regex way: read -p "enter test: " test;[[ $test =~ ^[Aa]$ ]] && echo yes || echo no Now the case shifter: read -p "enter test: " test;[[ ${test^^} ...


5

There are several useful ways to achieve this (in bash): two checks echo -n "Enter test: " read test if [[ $test == "a" || $test == "A" ]]; then echo "worked" else echo "failed" fi make the input lower case echo -n "Enter test: " read test test="${test,,}" if [[ $test == "a" ]]; then echo "worked" else echo "failed" fi regex for both ...


1

perl is good for this: $ str='I see that you#146;re eligible to get ticket for show on your device#44;' $ perl -pe 's/#(\d+);/chr($1)/ge' <<<"$str" I see that you’re eligible to get ticket for show on your device, I had to set my terminal's encoding to WINDOWS-1252 to get that output. Decimal 146 is not a valid ISO-8859-1 character. To treat ...


4

Note that you don't have to read the file beforehand, sed has the r command that can read a file: $ printf -v var "%s\n" "s1random stuff" "s2 more random stuff" "s1 final random stuff" $ echo "$var" s1random stuff s2 more random stuff s1 final random stuff $ sed '/^s2/r file.txt' <<< "$var" s1random stuff s2 more random stuff line 1 line 2 s1 ...


1

You need to replace the newlines in the variable with \newline. And the text to be inserted needs to be preceded by \newline. var=$(<file.txt) # Put backslash before newlines in $var var=${var// /\\ } printf "s1random stuff\ns2 more random stuff\ns1 final random stuff\n" | sed "/^s2/a \ $var"


0

Are you trying to do something like this?: LINE1=`cat test.file | sed '1!d'` LINE2=`cat test.file | sed '2!d'` LINE3=`cat test.file | sed '3!d'` LINE4=`cat test.file | sed '4!d'` echo $LINE1 echo $LINE2


0

You can simply use [ "$character" ] to test if string is nonzero, what is equivalent of longer form [ -n "$character" ]. Alternatively [ -z $character ] to test if string is zero. However many times in interactive scripts what is really needed is to set some default value in the variable which user can override. If this is your intention then you can avoid ...


1

Using awk: awk 'FNR == 1 || /potato|apple/' Using sed: sed -n '1p; /potato\|apple/p' In both cases, line number 1 and any lines matching potato|apple are printed.


0

Here's the txt table again in plain text: A B C D E 21 63 apple yellow 5 23 69 lemon green 6 45 135 orange yellow 7 67 201 mango green 4 54 162 potato maroon 5 Though I could think of a solution with low complexity, I think it is a bit hackish :-) Supposing the table txt ...


2

To edit in place, I'd use perl: perl -i.bak -pe 's/(?<=mu_nano=)([\d.]+)/ sprintf "%.1f", $1+1 /e' variables.f90


2

This looks like fortran code, and it is not the greatest idea ever to parse higher language source code, but anyway... with awk: awk -F'[=.]' '/nano/{$2++; print $1"="$2"."$3; next}1' This assumes that variable is always given with a dot. It takes only integer part of it (between = and .), increase by 1, and prints everything back.


1

With GNU awk: awk '{while(length%4)$0=0$0;$1=$1}1' FPAT=.... Input 100001010000000000100000011000000011000000000111010000000000 1000000000000000100 Output 1000 0101 0000 0000 0010 0000 0110 0000 0011 0000 0000 0111 0100 0000 0000 0100 0000 0000 0000 0100


0

Let bc and sed do the splitting into nibbles for you, and use bc and printf to do the conversion to 0-padded 4-digit binary: i=598980975283696640 echo "obase=16;$i" | bc | sed 's/./&\n/g' | \ xargs -I {} sh -c 'echo "ibase=16;obase=2;{}" | bc' | \ xargs printf "%04d " echo 1000 0101 0000 0000 0010 0000 0110 0000 0011 0000 0000 0111 0100 0000 0000


0

echo 'obase=2;262148' | bc | perl -E 'say readline =~ s/((....)*)\n/ $1/r =~ s/(\d{4})/$1 /gr'


0

You can apply (GNU) cut to the output of bc using a custom output delimiter: $ echo 'obase=2;5989809752836966' | BC_LINE_LENGTH=0 bc | rev \ | cut --output-delimiter=' ' \ -c$(echo -n 1-4; for i in $(seq 5 4 100); do echo -n ,$i-$((i+3)); done ) \ | rev 1 0101 0100 0111 1011 0011 0100 0010 1001 0111 0000 1011 0110 0110 The construction of the ...


6

echo 'obase=2;598123123980975281233696640' | BC_LINE_LENGTH=0 bc | rev | fold -w4 | paste -sd ' ' - | rev Or: echo 'obase=2;598123123980975281233696640' | BC_LINE_LENGTH=0 bc | sed ':1 s/\(.*[01]\)\([01]\{4\}\)/\1 \2/;t1' BC_LINE_LENGTH=0 is to stop bc from wrapping numbers at 70 columns. That's GNU specific though. rev is not a standard command, ...


0

As alternative you could use the python interactive shell for it. For example: $ python >>> s = bin(598980975283696640)[2:]; s = '0'*(4-len(s)%4)*(len(s)%4>0)+s >>> [s[i:i+4] for i in range(0, len(s), 4)] ['1000', '0101', '0000', '0000', '0010', '0000', '0110', '0000', '0011', '0000', '0000', '0111', '0100', '0000', '0000'] ...


0

My preferred way would be to pad it to multiples of four: echo 'obase=2;262148'| bc | perl -pe 'chomp; $_ = 0 x ((-length) % 4) . $_;s/([01]{4})/ $1/g;s/$/\n/'


2

I would use this simple function: nibbles () { echo "obase=2; $1" | bc | rev | while read -n4 a; do echo -n "$a ";done | rev ; echo; } $ nibbles 598980975283696640 1000 0101 0000 0000 0010 0000 0110 0000 0011 0000 0000 0111 0100 0000 0000



Top 50 recent answers are included