New answers tagged

1

POSIX-compliant sed solution which produces 0019093203 from the sample data (assumes Invoice information follows "ODSTATION BUSY"): sed '/.*ODSTATION BUSY.*Invoice #: */!d; s///; s/\\n.*//' file A tweaked version of don_crissti's approach to handle either case: sed -n '/ODSTATION BUSY/s/.*Invoice #: \([0-9]\{1,\}\)\\n.*/\1/p' file


1

perl -e 'while(<>) {if ($_ =~ qr/ODSTATION BUSY/) { print "$1\n" if $_ =~ /Invoice #:\s+(\d+)/ } }' <yourfile_goes_here> Oh, Bash? grep 'ODSTATION BUSY' filename |egrep -o 'Invoice[^0-9]+[0-9]+'|egrep -o '[[:digit:]]+' Or: awk 'BEGIN{$0 ~ /ODSTATION BUSY/}; gsub(/^.*Invoice #: /,""){print $1}' filename |sed 's/\\n.*$//g'


0

if there are too many lines, like in the order of thousands, I prefer to use a temporary file rather than extracting them in the memory and causing system load, but this is a preference of mine. grep "ODSTATION BUSY" logfile > workfile cat workfile | while read line do invoffset=$(echo ${line} | grep -b -o "Invoice #:"|cut -d: -f1) #locates string ...


0

One way of doing this would be: $ grep "ODSTATION BUSY" <filename> | sed 's/^.*Invoice #://;s/\n.*$/' That's pretty ugly, I'm sure there are other, possibly better, ways of pulling this information out using something like awk.


0

As noted in comments, creating certificates (signed host keys) would help you to solve this issue (see manual page for ssh-keygen, chapter "Certificates" describing this procedure). Basically you would create a CA on server, sign the host keys and store the CA public key in your known_hosts such as: @cert-authority * ssh-rsa AAAAB5W... The other ...


2

With the GNU implementation of grep, you have the -h/--no-filename option for that. $ grep -rc PATTERN . ./b:1 ./a:0 ./1/2/c:2 $ grep -rhc PATTERN . 1 0 2 The portable/standard equivalent would be: $ find . -type f -exec grep -c PATTERN {} \; 0 2 1 but would mean running one grep invocation per file. If instead, you want the total number of ...


0

here you are: for ((i=2; i < 254; i++)); do ip address add xxx.yyy.zzz.$i dev eth0; done Adjust the values to your needs Edit: more than the 'C'-class net: for ((j=3; j < 5; j++)); do for ((i=2; i < 254; i++)); do ip address add xxx.yyy.$j.$i dev eth0; done; done Edit: not class dependent implementation: #!/bin/bash DEV=eth0 ...


2

The set of options could be visualized by using printf: $ printf '<%s> ' "-e ssh -axhPv" arg2 arg3 ; echo <-e ssh -axhPv> <arg2> <arg3> If quotes are removed, this is what you get: $ printf '<%s> ' -e ssh -axhPv arg2 arg3 ; echo <-e> <ssh> <-axhPv> <arg2> <arg3> That is: ...


1

Enumerating the files Parsing the output of find is fragile. Better make find invoke the transformation program. To generate the output file name, a simple parameter expansion is enough to change the suffix _times.csv into _subtracted.csv (for example). find logs_swapoff -name '*_times.csv' -exec sh -c ' <"$1" awk "$0" ...


3

To sort from highest to lowest: grep -rc 'Author' $1 | sort -r -t ':' -k2,2n -r sorts in reverse order, that is, from highest to lowest. -t introduces the separator. -k introduces which fields to sort by. The fields are separated by the separator defined by -t. This syntax means to sort by all of the fields between 2 and 2 (so just the second field). The ...


3

grep -rc 'Author' $1 | sort -t : -k 2,2n is good if you want the number of lines containing the keyword, regardless how many times it repeats on any given line. If you want the actual word count, you should use this echo $1:$(grep -o 'Author' $1|wc -l) | sort -t : -k 2,2n -o option for grep is most probably available on the gnu version of grep. If you ...


1

When you quoted the rsync_options to the rsync command, you passed all of those options as one argument to rsync, instead of as separate options. Thus, rsync was trying to run ssh with the "-axhPv" flags. Demonstration time: function showme () { echo First: $1 echo Second: $2 echo Third: $3 } $ showme "-e ssh -axhPv" two three First: -e ssh -axhPv ...


0

I think the argument for -e needs to be in quotes itself. rsync -axhPv -e 'ssh -p 2222' user@host:/source_dir/ /dest_dir/ So you might need to do something along the lines of: rsync_options="-e 'ssh' -axhPv" You may or may not need to escape the single quotes inside the doubles.


2

In while [ false ] the false is neither a command nor a boolean value. The while expects a command but [ ... ] with no operators just checks for any non-empty string. Thus [ false ] is true. It is in this sense the same as [ faaaalseeee ]. What you meant is: while true; do ... while false; do ...


4

In bash, use FUNCNAME array: tt() { printf '%s\n' "$FUNCNAME" } With some ksh implementations: tt() { printf '%s\n' "$0"; } In ksh93: tt() { printf '%s\n' "${.sh.fun}"; } From ksh93d and above, you can also use $0 inside function to get the function name, but you must define function using function name { ...; } form. In zsh, you can use ...


0

function tt { echo ${FUNCNAME}; } does it on my CentOS 6 box.


2

In bash, you can use ${FUNCNAME[0]}.


0

You can declare an array in here #!/bin/bash string=('fast' 'apache') ps -ef > test.txt for i in "$string[@]" do grep "$i" test.txt done Or you can do it directly in the ps line to save only those processes #!/bin/bash string=('fast' 'apache') for i in "$string[@]" do ps -ef | grep "$i" > ps_output_of_$i.txt done Give a try


6

If you're looking for portability, you can bet that /bin/sh will be on most every system, though different platforms will implement it differently (e.g., on Ubuntu it links to dash and on Fedora it links to bash). Something will be there though. If you use that and write your scripts in POSIX compliant ways you'll have your best shot at being portable.


0

The most common is /bin/sh. It's lighter than bash, that's why it's implemented on most embedded Linux-based systems.


0

first of all the first shebang line is not what you want for your script. expect as a shell has a limited use and this is not one of them first line should be something like #!/bin/bash in your case then ps -ef > test.txt grep -e fast -e apache test.txt will print you all the lines containing either of these words. or you can skip the writing ...


2

From logrotate(8): It allows automatic rotation, compression, removal, and mailing of log files. [...] The lines between postrotate and endscript (both of which must appear on lines by themselves) are executed after the log file is rotated. In other words: postrotate happens after rotation, but before compression. Your log's gzip archive doesn't ...


0

Command you have used --delete-during will delete the files in receiving side /mnt/usb0/backup/partition2 However check your --exclude-from file /etc/rsync-exclude.txt has the name test.txt in it. You can also use below options, --delete-before receiver deletes before xfer, not during --delete-during receiver deletes during ...


0

In the script you present: $1 contains the name of an option, and an $array contains the value of the options. To get the result in the variable $ans expanded as you request, there are a couple of options: Eval An eval: expand $1 to Sigil for example, and assign $Sigil to ans: eval ans\=\"\$\{"$1"\}\" However, that assumes that $1 is a one word label of ...


0

Here's a quick and dirty solution: sed -n 's/^.*href="\([^"]*\)".*$/\1/p; s/^.*\(£[0-9.]*\).*$/\1/p' | awk 'NR % 2{ printf "%s, ", $0; next} {print}' It simply extracts what looks like hrefs and what looks like prices, hoping you'll get an alternating sequence of urls and prices. Then joins consecutive lines to get the format you want.


0

The best way to get at data from eBay is through their API. This being said, sometimes all you have is HTML, so I'll cover that in my answer. Don't even try to extract information from HTML with tools like sed and grep. It's hard to do when it works at all, and extremely brittle. This way lies madness. If you have to parse HTML, use a tool for parsing ...


1

First time sudo is invoked password is prompted for. Then, depending on configuration, if invoked within N minutes (default 5 minutes IIRC), one do not need to enter password again. You could do something like: sudo echo >/dev/null || exit 1 or perhaps something like: sudo -p "Become Super: " printf "" || exit 1 at start of script. If you want to ...


0

I have another perhaps simpler approach to cleaning up old print jobs. Instead of finding a way to convert the abbreviated time stamps on the print jobs, it's easier to use the find command to accomplish the task of deleting old print jobs fitting a certain criteria. For example, if you want to find print jobs older than 4 days: find /var/spool/cups -name ...


1

put the following lines into a script file, such ad mydownload.sh magnet=$(lynx -dump -hiddenlinks=listonly lynx_bookmarks.html | grep "magnet" | sed 's/&.*//' | cut -c 7-) curl http://my_ip:my_port_number/startdownload/${magnet} save and exit chmod 755 mydownload.sh ./mydownload.sh


1

So what you need is to capture the output of one command, and use it in another. The bash $(..) syntax will let you execute a command and capture the output, then you can use that output in another command. One way is to capture the output in a variable, such as: shortcut=$(lynx -dump -hiddenlinks=listonly lynx_bookmarks.html | grep magnet | sed ...


3

You can force interactive session using -tt switch in your script: ssh -tt user@hostB ./scripts/test.ksh Otherwise the TTY on the hostB will not get allocated and you will get such errors.


0

bash-4.1$ echo $( echo hi ) hi bash-4.1$ So, curl $( ... ) Would pass the output of whatever ... is to curl by running whatever those commands are as a subshell. $() can also be written with backticks, though those are header to read, and do not nest. grep | sed can probably be replaced with a single call to awk.


2

You are missing one pipe | character. Try: sort myfile |uniq -u|tee newfile.txt If this is not working, please provide the error message you are getting. By the way, this command uniq -u eliminates all lines which have duplicates. If this is your intention, that is fine. But if you want to see one of the duplicate lines, you need to drop -u for the uniq ...


1

Use an associative array: #!/bin/bash declare -A url url=( [url1]="http://www.google.com" [url2]="http://www.yahoo.com" [url3]="http://www.bing.com" ) if [[ -z $1 ]] ; then echo "This script needs at least one parameter!" exit 1 elif [[ -z ${url[$1]} ]] ; then echo 'Unknown option' exit 1 fi echo "Let's search ${url[$1]}." ...


0

arr=($url1 $url2 $url3) for i in ${arr[@]}; do if [[ $i == $1 ]] ;then ans=$i break fi done


6

I'm not aware of any awk implementation that has support for that. You could write an extension for gawk for that, but here, I'd rather switch to another language. perl makes it easy to convert awk scripts with its a2p script. For instance, if you have an awk script like: {count[$0]++} END { for (i in count) printf "%5d %s\n", count[i], i } a2p on it ...


1

Running across a network connection, sometimes (in particular when permissions change, e.g., using sudo), the terminal size information is not propagated to the remote machine. When that happens, of course, some programs become confused about when to format their output, wrapping lines, etc., You can usually use the resize program to ask the terminal its ...


1

It looks like you could use FIFOs for this: mkfifo tasks Producer: for i in {1..10}; do echo "task$i"; done > tasks Consumer: while read task; do echo "received $task"; done <tasks With this, you don't have to take care of synchronization or deletion, and you don't waste any CPU time on polling -- if there's no data or if the producer hasn't ...


0

This is not something you can do in tcsh. Your options are: Use an alias, as already mentioned by terdon: alias jgrep grep --include="*.java" Set up a completion pattern. For example: complete grep 'c/--/(complete)/' 'n/--complete/(*.java *.rb *.py)/' This will allow you to type grep --<Tab><Tab>j<Tab>. Use a variable: set java = ...


0

If you're open to using a different syntax (which is also shorter and easier to type in this case), just create an alias for it. Add this to your ~/.cshrc: alias jgrep grep --include="*.java" Then, you can run something like: jgrep foo * And it will run grep --include="*java" foo *


1

So, running this script inside the directory that has folder1, folder2, folder3, etc etc, it will look inside each folder and rename each of the files files present to <directory_<filename>. I believe this is what you were looking for, let me know if it is not what you were looking for. #!/bin/bash for x in $(ls `pwd`); do if [ -d $x ]; ...


1

Your best best is to set up a secure shell daemon on the system (if it's not already running) and use paswordless keypair authentication. Presuming that sshd is already in place, you can log in as tiger and run the following commands: ssh-keygen ssh-copy-id jenk@localhost After that, tiger will be able to run a command as jenk with the following syntax: ...


0

When you run that command in a pipe, it spawns a new process rather than running the command in the current shell. The new shell switches to the user jenk, but when that shell exits (since it didn't have anything else to do), your original shell which spawned that shell is still logged in as you. If you run the command in a script, it will be run in a ...


1

Answer: The shell that is running the (sourced) code is not bash. if the code you present is inside a file called script.sh, then this errors will appear: $ dash ./script.sh ./script.sh: 6: [: a: unexpected operator $ zsh ./script.sh ./script.sh:6: = not found That seems to confirm that you are using a zsh prompt, and sourcing the script. To reproduce: ...


3

When sourcing the script, your current shell executes the commands. In zsh, you must use a single = in comparison. $ echo '[ a == a ]' | zsh zsh: = not found exit code: 1 $ echo '[ a = a ]' | zsh $


0

Try to put sync between your cp command and touch ready.txt.


0

Some errors found: The shebang must be #!/bin/sh. All the # are a syntax error. Lines 15-21 are exactly equal to lines 4-11 (remove them). Line 58 missing $ in Col2 var for Column_list= Line 59, the echo "$Column_list">big_data_file_$$ will erase all information previously written to big_data_file$$. Change to >>. Line 93, 103, 112, 124 the if[ ...


1

You just could use grep to do this. The second grep is to filter files with no matches. grep -rc 'Author' ${1} | grep -v ':0$'


0

Loop over the files: for file in "$1"/* ; do if [[ -f "$file ]] ; then printf %s: "$file" grep -o Author "$file" | wc -w fi done You can also use Perl: perl -lne 'BEGIN { $c = 0 } $c++ while /Author/g; if (eof) { print "$ARGV: $c"; $c=0 } ' "$1"/*


0

Depends how precisely you want the formatting - usually tab separated is enough - but I'd tackle it like this: #!/usr/bin/env perl use strict; use warnings; #set record separator to double line feed. local $/ = "\n\n"; #print header row print join "\t", "VM", "Virtual_Disk", "size", "Physical Disks", "\n"; #iterate stdin or files specified on ...



Top 50 recent answers are included