New answers tagged

1

With excessive use of grep's -f (obtain patterns from file) option: grep 's$' input | # output: all lines ending with s sed -e 's/s$//' | # those same entries, minus the s grep -F -x -f input | # the entries whose plurals appear sed -e 's/$/s/' | # the plurals to remove grep -F -x -v -f - input


0

Using parenthesis works, but you need to escape them so the shell does not interpret them as subshell invocations. You can use backslash, ( and ) or quotes. If none of your files have names that contain newlines or other control characters, which should be the case unless someone tries to be nasty, then you can use the pipe to xargs approach, which is the ...


0

If you have no shebang line in your script, then /bin/sh ./script ./script are equivalent as #!/bin/sh is the implicit shebang line. If you do have a shebang line and it resolves to the same bash as using bash as a command resolves to, then there's no difference. You can also use: bash -c "$(cat script)" ./script and it'll work the same as the ...


1

#!/bin/bash #consider using bash for this script. for file in graham/quant/*.biz do if grep -q "max" "$file"; then maximum.sh "$file" elif grep -q "milan" "$file"; then milan.sh "$file" elif grep -q "min" "$file"; then minimum.sh "$file" else echo " keep working you are close " fi done


0

I faced the same problem and that is how I fixed it: First, try to find out why what is the error of your push. You can get the error log by adding 2>&1 at the end of your script. In your case it would be: ${GIT} push git@bitbucket.org:username/repo.git master >> ${LOG} 2>&1 Several reason can cause your push is not working from ...


1

Here's an awk approach: $ awk 'FNR==NR{a[$1]=$2; next} {print $0,a[$1]}' bandwidth.txt hits.txt onlinestudyboard.com received 186 hits from 31/May/2016 at 1201 aaspak.org received 184 hits from 31/May/2016 at 1202 211.57M khawajarubber.com received 183 hits from 31/May/2016 at 1246 aatonerpk.com ...


0

The cd should not be required. The following line should do the same. /WebSphere/was85/mycel/mynode/AppServer/java_1.7_64/bin/java -cp \ /usr/my.jar com/com.my_comapny_witt_entire_name/myMain I broke the command line into two lines by using backslash continuation. There must be no characters between the backslash and the newline for continuation ...


1

I use alias to navigate If you have few directories that are frequently accessed then simply set alias For example, alias e='cd /etc' alias h='cd /home' alias al='cd /var/log/apache2/' Then simply e will take you to etc.


0

I've finally found the problem was grep that with the -r switch ignores stdin and search in the current working directory instead. To force stdin reading, i've just added a -: filecat "$ARG" | grep $GREP_ARGS - | sed "s#^#$ARG: #"


1

I am using jump to quickly change the working directory. To add the current directory: jump -a [bookmark-name] To list all your bookmarks: jump -l e.g.: ------------------------------------------------------------------ Bookmark Path ...


2

Using a bash script: #!/bin/bash readarray -t mylist # compare each item on the list with a new list created by appending `s' # to each item of the original list for i in "${mylist[@]}"; do for j in "${mylist[@]/%/s}"; do [[ "$i" == "$j" ]] && continue 2 done echo "$i" done The list is read from stdin. Here is a test run: $ cat file1 ...


2

Using awk and reading the file twice. Save all variables in array with s on the end. Check the array on each line in the second run through and print if the line is not in array. awk 'FNR==NR{a[$0 "s"]++;next}!($0 in a)' test{,} To use a little less memory you could also do awk 'FNR==NR{!/s$/ && a[$0 "s"]++;next}!($0 in a)' test{,}


7

Use pushd and then the special names for the directories in your directory stack: ~1, ~2, etc. Example: tmp $ dirs -v 0 /tmp 1 /tmp/scripts 2 /tmp/photos 3 /tmp/music 4 /tmp/pictures tmp $ cd ~3 music $ dirs -v 0 /tmp/music 1 /tmp/scripts 2 /tmp/photos 3 /tmp/music 4 /tmp/pictures music $ cd ~2 photos $ cd ~4 pictures $ cd ~3 music $ cd ...


2

You can do this in several ways, e.g., the simplest way would be to sort the data and compare adjacent lines: sort foo |awk '{ if ( plural[$1] == "" ) print; plural[$1 "s"] = 1; }' Given input frog dogs cats catfish cat dog frogs output cat catfish dog frog Without sorting: #!/bin/sh awk 'BEGIN { count=0; } { words[count++] = $1; ...


3

You might find wdiff useful for this type of comparison; it's a front-end to diff which produces word-by-word comparisons. With your example it produces by default Insert into table1 values (101 ,"a", [-"xyz");-] {+"xyz123");+} Insert into table1 values (102 ,"b", "pqr"); It can use terminal features to make the output more legible on a terminal (wdiff ...


1

This is a simplified solution using awk, which does not preserve the order of words: { len = length($1); prefix = $1; if (substr($1, len) == "s") { prefix = substr($1, 1, len - 1); } if (prefix in data) { next; } else { print prefix; data[prefix] = 1; ...


3

I mostly use ZSH with oh-my-zsh profile. You can type into a terminal the following match: # cd /ho Then you can simply use arrows (up and down) to go through all the shell history which shows only those entries that start with the characters above. So, for instance, if you went to /home/morfik/Desktop/ and /home/morfik/something/, you can switch between ...


9

When you cd somewhere, Bash stores the old working directory an environment variable, $OLDPWD. You can switch back to that directory with cd -, which is equivalent to cd "$OLDPWD". You can bounce back and forth between directories like so: blue$ cd ~/green green$ cd - blue$ cd - green$


0

I suppose the trick is to use -3t for the first pr, and then also add -w200 (or so) to avoid that the last column gets clipped. More generally, if you have N matrices, you'd use pr -${N}t -w $((N*W)) as first pr command, where W is the character width of the one matrix (say, roughly 10 for each column plus a bit). All else should be fine.


1

By definition, diff is showing differences lines by lines (see diff manual page), it will therefore not show only the differing characters. You can reduce the amount of difference by pre-processing the files, for exemple by inserting an end-of-line character after each semi-column: sed -e 's/;/;\'$'\n/g' old.sql > old.patched Then use diff on the two ...


2

You can achieve this with a bunch of near-minimum typing aliases in your ~/.bashrc, like so: alias pd=pushd alias po=popd alias d='dirs -v' alias d0=d alias d1='pd +1' alias d2='pd +2' alias d3='pd +3' alias d4='pd +4' alias d5='pd +5' alias d6='pd +6' alias d7='pd +7' alias d8='pd +8' alias d9='pd +9' alias d10='pd +10' # feel free to add more aliases if ...


0

To redirect the output of any command, binary, or script, you can use the shell's redirection syntax (>), to send the output of anything that you run into a file: /path/to/some/script.sh > /tmp/example-output.log


1

To start with, it's better to always check the exit status of cd, so cd /WebSphere/was85/mycel/mynode/AppServer/java_1.7_64/bin && ./java -cp /usr/my.jar com/com.my_comapny_witt_entire_name/myMain cmd1 && cmd2 means execute cmd2 if cmd1 succeeds. Now I suspect your problem here is that that script is not being interpreted by that OMVS ...


1

What is the name of the script itself? Does it match the pattern Recorder Manage? If so, the pgrep is finding the script itself in addition to the process you're looking for, which will then make it effectively a crap shoot as to which will be listed first. Also, don't use kill -9 unless you absolutely have to; just use kill to send SIGTERM rather than ...


0

Just a backslash cd /WebSphere/was85/mycel/mynode/AppServer/java_1.7_64/bin ./java -cp \ /usr/my.jar com/com.my_comapny_witt_entire_name/myMain


3

I use a small script called z, which might also be of interest, even though it does not do exactly what you asked. NAME z - jump around SYNOPSIS z [-chlrtx] [regex1 regex2 ... regexn] AVAILABILITY bash, zsh DESCRIPTION Tracks your most used directories, based on 'frecency'. After a short learning phase, z will take ...


2

curl -o dl.html -s -m 10 --retry 0 "$URL" Note: --retry is not really necessary, but is here for completeness' and because the defaults might be set in an environment or rc file.


2

If you have 5 or 10 directories you use a lot, and don't necessarily care about recently used 5 or 10 directories, set up some command aliases like alias cdw="cd /var/www/html" And so when I want to go to Apache home page directory I just type cdw as I answered in Equivalent of alias for a symbolic link?.


0

rsync will only synchronize files that have been changed. You can set up an rsync command that will synchronize every hour or at a specified time interval and it will only sync the files that have been changed.


10

I suggest you install fasd. It gives you the ability to quickly jump to any directory you've already been in by typing only a small fraction of its name. Example: if you've visited /home/someName/scripts/, you could jump there just by typing z scr for example. It's way more convenient that remembering the ordering in the history stack of anything similar.


2

Unless your script is not executable (fix that with chmod +x /root/backup_system.sh) or the crond isn't running, there's nothing obviously broken in your script that will prevent it from running. All of the commands you use (date, hostname, tar, and ls) should be in /bin, which should be in the default PATH....unless you've changed it in your crontab. The ...


0

$ awk -F': ' '/position[0-9]+:|word[0-9]+:/ {w=w" "$2 }; /^[0-9. ]+$/ { w=w" "$0 }; /^[[:blank:]]*$/ || eof {if (w) {gsub(/,/,"",w);print w;w=""}}' muthu.txt [ 0.0000000 0.0000000 0.0000000 ] 0.0000000 0.32015595 0.18484212 0.00000000 0.00000000 0.36968424 0.00000000 0.00000000 0.00000000 ...


0

#! /usr/bin/perl use strict; use warnings; my %files=(); my @files=(); my $currentfile=''; my $maxcols=1; while(<>) { chomp; # a hash such as %files is inherently unordered, so store each # filename we process in @files, in the order that we see them. if ($currentfile ne $ARGV) { $currentfile = $ARGV ; push @files, $currentfile; }; ...


1

just a wild guess awk 'BEGINFILE { printf "%s",FILENAME} { printf ",%s",$0 ;} ENDFILE { printf "\n" ;}' file1.txt file2.txt file3.txt this will turn file to csv (but unquoted), file being converted to one line. replace ",%s" by "\t%s" to use tab.


4

There is also cd_func from Peter Marinov, it's basically cd with a stack with up to 10 entries: http://linuxgazette.net/109/misc/marinov/acd_func.html # do ". acd_func.sh" # acd_func 1.0.5, 10-nov-2004 # petar marinov, http:/geocities.com/h2428, this is public domain cd_func () { local x2 the_new_dir adir index local -i cnt if [[ $1 == "--" ]]; ...


14

The short answer is: you don't, but it saves about 1ms of CPU time (on modern CPUs). (Note that you should exec only at the end of a script, because nothing after the exec will get run). The longer answer is: Exec replaces the process image of the current process with the process image of the executable you exec. That means that the moment you exec, the ...


1

If you have iselect installed, you could do something like this: $ alias dirselect='cd $(iselect -a $(dirs -l -p | sort -u))' $ dirselect This will give you a full-screen ncurses-based interactive arrow-key navigable menu to select a directory to cd to. If you haven't used pushd in the current shell session, the list of directories in the menu starts ...


0

First combine all the text files: cat Article1.txt Article2.txt Article3.txt > Result.txt Then convert the text file to CSV: (echo "Col1;Col2;Col3" ; cat Result.txt) | sed 's/;/<tab>/g' > file.csv


0

With expr $j:'.*' the command expr is receiving one (1) argument. The command expr could not understand that. The command expr needs each argument clearly separated: expr "$j" ":" '.*' That will be three (3) arguments given to the command expr. The quotes " around : are not really required. And is better to use an space before the string in $j to avoid ...


1

You need to divide the value by something (even 1) in bc to get the scale to matter: $ i=5; bc -l <<< "scale=7;$i/1" 5.0000000 $ i=5; bc -l <<< "scale=7;$i/2" 2.5000000 Edit: To special case 0 (which doesn't add .0000000 by default) you could add: $ [ "$i" -eq 0 ] && i="0.0000000" But beware that that doesn't make the ...


35

bash's builtin pushd with the + and - options can rotate the directory stack. The syntax can be a little confusing, perhaps because that stack is a zero-based array. These simple wrapper functions cycle through the directory stack: # cd to next directory in stack (left rotate) ncd(){ pushd +1 > /dev/null ; } # cd to previous directory in stack ...


4

I wrote a script named xyzzy to do this: #!/bin/bash i="$1" i=$((${i//[^0-9]/})) i="$(($i-1+0))" b="$2" b=$((${b//[^0-9]/})) b="$(($b-1+0))" if [ -z "$XYZZY_INDEX" ]; then XYZZY_INDEX="$((-1))" fi if [ ! -f "/tmp/xyzzy.list" ]; then touch /tmp/xyzzy.list chmod a+rw /tmp/xyzzy.list fi readarray -t MYLIST < /tmp/xyzzy.list showHelp(){ read ...


1

[Inspired by this answer by cas.] But what if …? But what if my script sets a variable to a known value before using it?  In particular, what if it sets a variable to one of two or more possible values (but it always sets it to something known), and none of the values contain space or glob characters?  Isn’t it safe to use it without quotes in ...


0

In your case statement, replace "A" with A, and "S" with S: echo "Enter S if you want to Subtract, A if you want to Add." read request case "$request" in A) echo "$sum";; S) echo "$difference";; *) "Sorry, that is an invalid request.";; esac


1

expr is old, but it does have one limited use I can think of. Say you want to search a string. If you want to stay POSIX with grep, you need to use a pipe: if echo november | grep nov then : do something fi expr can do this without a pipe: if expr november : nov then : do something fi the only catch is expr works with anchored strings, so if you ...


2

In bash, if you want to delete these patterns, you could use extended pattern matching: # optional - save extglob state $ shopt -q extglob && extglob_disabled=false || extglob_disabled=true # enable extended pattern matching $ shopt -s extglob $ addons="WEBIF_LIVELOG WEBIF_JQUERY TOUCH WITH_SSL READ_SDT_CHARSETS WITH_DEBUG MODULE_MONITOR" $ echo ...


0

You need to use a file description different from stdin if you want to run something interactive inside the loop. #!/bin/bash while read -r file <&3; do vim "$file" done 3< <(find . -type f) or with zero-separators: #!/bin/bash while IFS= read -r -d '' file <&3; do vim "$file" done 3< <(find . -type f -print0) The ...


1

For me this works: #!/bin/sh for f in *.txt do vim "$f" done It opens a vim for each file, and opens the next after having closed the previous.


0

There are countless ways that using a regex to parse HTML can go wrong. Even for a seemingly simple task like extraction of URLs from a HTML file. So, don't use a regex. Use something like this instead: #! /bin/bash htmlfile='./file.html' URL='https://google.com' links=($(lynx -dump -listonly -nonumbers -force_html "$htmlfile" | sed -e ...


1

The expr utility parses its arguments as an expression. The operators must appear as standalone arguments. expr "$j" : '.*' Above, expr is passed 4 arguments: expr, the content of $j, : and .*. Assuming the content of $j is not ( or ! (or things like length with some implementations), expr will under that as the : pattern matching operator applied to the ...



Top 50 recent answers are included