New answers tagged

0

In an SQL single-quoted string literal, you can include a single quote by doubling it. In bash, you can use the parameter expansion feature ${VARIABLE//PATTERN/REPLACEMENT} to get the value of VARIABLE with all occurrences of PATTERN replaced by REPLACEMENT. hive -e "INSERT INTO SCHEDULED_MIGRATION_LOGS VALUES ('${OUTPUT//\'/\'\'}')" Beware that if OUTPUT ...


6

grep -f <(sed 's/.*/\^&\\>/' town-list.txt) ma-towns.txt Explanation: grep -f file reads file for a list of patterns to match against. We are searching in the ma-towns.txt list, using patterns from town-list.txt. Each separate line is treated as a new pattern, i.e. a new search term. However, that's not quite enough, so I've included a sed to ...


5

This will read the lines of file2 and parse file1 with grep using the lines : while read line; do grep "${line}" file1 done < file2


2

In Bash, the line A=${B:-C} will assign the value of the variable B to the variable A if B is set and not null. Otherwise the variable A will get the value C, i.e. the string containing the single character C. What you might want to do: B=$( some command ) A=${B:-C} After this, $A will either be the output from some command, if it's not null, or the ...


4

Nested substitution is not available in any modern Bourne-like shells except zsh: $ print -rl -- ${$(echo):-C} C $ print -rl -- ${$(echo 1):-C} 1 In other shells: A=$(nc -l 443) A=${A:-C}


4

Assuming you meant: find . -type f -name "img*_01.png" -print0 | xargs -r0I{} python script.py -f {} That can't be done with xargs, as in xargs has no rightTrim() operator. You can either do away with xargs and do something like (bash, zsh syntax): find . -type f -name "img*_01.png" -print0 | while IFS= read -rd '' file; do python script.py -f "${...


0

Bash executes the code in the variable PROMPT_COMMAND before printing the prompt. One of the things you can do that is to calculate components of the prompt which are then assembled via the PS1 setting. PROMPT_COMMAND_set_status () { if ((last_status == 0)); then PS1_status= else PS1_status="[$last_status] " fi } PS1=PS1='${debian_chroot:+($...


4

You can create conditions and expressions inside $() like this: echo "$( var=2; echo $var)" in your example you can change PS1 variable like this: PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w $( status=$?; (( status != 0 )) && echo "[$status] " )\$ ' looks like: jackman@b7q9bw1:~ $ echo hello hello jackman@b7q9bw1:~ $ sh -c 'exit 42' ...


7

To select only the SSE flags, try: awk '/SSE/' ORS=' ' RS=' ' The key thing here is setting the record separators on input and output to a space. That way, each option is accepted or rejected separately. For example: $ SUNCC_CXXFLAGS="-D__SSE2__ -D__SSE3__ -D__SSSE3__ -D__SSE4_1__ -D__SSE4_2__ -D__AES__ -D__PCLMUL__ ..." $ newFLAGS="$(echo "$...


0

BTW, if you have two version strings (e.g. in $x and $y) you can use printf and GNU sort to find which is newer. $ x=4.1.1 $ y=4.2.2 $ printf "%s\n" "$x" "$y" | sort -V -r 4.2.2 4.1.1 $ if [ $(printf "%s\n" "$x" "$y" | sort -V -r | head -1) = "$x" ] ; then if [ "$x" = "$y" ] ; then echo "$x is equal to $y" else echo "$x is newer than $y" fi ...


2

The test command, also named [, has separate operators for string comparisons and integer comparisons: INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2 vs STRING1 = STRING2 the strings are equal and STRING1 != STRING2 the strings are not equal Since your data is not strictly an integer, your test needs to use the string ...


3

You could simplify the whole thing if you use the // format for gsub: $ echo "ABC(T)" | awk '{gsub(/ABC\(T\)/,"ABC/G")}; print $0' ABC/G Then, you could simplify further by using print with no arguments (which is the same as print $0) or the 1 shorthand for printing (the default awk action for expressions that evaluate to true, such as 1; is to print the ...


1

You were very close. You just needed two \. That is because one of set is for the text itself passing through BASH, and the other would be the actual \ which is being parsed by awk. This seems to work for me: echo 'ABC(T)' | awk ' {gsub("ABC\\(T\\)","ABC/G")}; Print $0' "$FILENAME" And gives: ABC/G



Top 50 recent answers are included