Tag Info

New answers tagged

2

You can use the split tool: split -l 1000 words.txt words- will split your words.txt file into files with no more than 1000 lines each named words-aa words-ab words-ac ... words-ba words-bb ... If you omit the prefix (words- in the above example), split uses x as the default prefix. For using the generated files with parallel you can make use of a ...


0

With bash: #!/bin/bash while read LINE do if [[ "$LINE" =~ '<' ]]; then echo -e "${LINE/>*</>\\n<}"; fi done < file.html


2

Using an actual html parser isn't that hard: perl -MHTML::Parser -E ' $handler = sub {say "<".shift.">"}; HTML::Parser->new(start_h => [$handler,"tag"], end_h => [$handler,"tag"]) ->parse_file(shift @ARGV) ' file.html <html> <head> <title> </title> </head> <body> </body> ...


3

A quick hack with perl: perl -wlne 'print for(/<.*?>/g)' file.html But for a serious solution you should use a tool that really understands html/xml.


0

I use the simplest possible approach. Mainly, if the command has no parameters, I usually use aliases. If the command has parameters I use a function. e.g. making and cd'ing to a directory: md () { [ $# = 1 ] && mkdir -p "$@" && cd "$@" || echo "Error - no directory passed!"; } I also use a function if the command uses an installed ...


1

Though you did start the program in the background by stating &, the output of the background process is still directed to the stdout of your current terminal. That means your prompt is overwritten by something like a echo '\r\n' (overwrites the current line in case your next prompt is completely gone) or echo "Someotherstuff" (prints Someotherstuff ...


4

You're backgrounding the application, and the application is generating output. Your prompt is still there, it just has extra stuff being shown. For example: $ ( sleep 1 && echo hello ) & [1] 24764 $ █ And then after a 1 second delay, I get: $ ( sleep 1 && echo hello ) & [1] 24764 $ hello █ The echo is just writing output to ...


1

One thing to bear in mind: in for i in *.{txt,py,c}; do ... The *.{txt,py,c} is first expanded to *.txt *.py *.c. While *.txt is sorted, *.{txt,py,c} is not. It also means that if there's not .py file, you will loop over the literal string *.py. If you want a sorted global list, you should use an alternation globbing operator instead of brace expansion. ...


2

Use zsh instead: setopt extendedglob for f ($arg/**/*.(#i)(txt|h|py)(N.)) { ... } (the (N.) is to not return an error if there's no matching file and select regular files only (the equivalent of find's -type f))


4

In bash you can set nocaseglob: shopt -s nocaseglob for file in "$arg"/**/*.{txt,h,py} do .... done shopt -u nocaseglob noclaseglob is fine to use in any bash since 2.01, however ** requires bash or later (an it follows symlinks up to bash 4.3). Note the correction to quote $arg since there will be problems if this contains spaces or glob characters. ...


8

Very simple: for i in *; do echo "<$i>" done This uses bash's file globbing. A sort is not necessary as bash already sorts pathname expansions. From man bash: Pathname Expansion After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then ...


1

If none contain newlines, then this will get you a sorted list: _state=$(set +o) set -f IFS=' ' ; for f in $(set +f; printf %s\\n * |sort) ; do echo "$f" ; done eval "$_state" That is, it will get you a sorted list of all of the files in the current directory - here represented by *.


0

Have you looked at the WMI Client package? Atomic looks to have a build for RedHat. # wget -q -O - http://www.atomicorp.com/installers/atomic | sh # yum install wmi # wmic -U username%password //servername "Select * from Win32_Process" Ref: http://www.krenger.ch/blog/wmi-commands-from-linux/


1

You can do this with awk. For example: arguments=`awk '{a = $1 " " a} END {print a}' arguments.txt` Edit after reading your comment: arguments=`awk '{i = 0; while(i<=NF){i++; a = a " "$i}} END {print a}'


2

Assuming each line of arguments.txt represents a separate argument, with bash 4 you can read arguments.txt into an array using mapfile (each line from the file goes in as an array element, in sequence) and then pass the array to the command mapfile -t <arguments.txt source test.sh "${MAPFILE[@]}" The advantage is that splitting on spaces embedded ...


0

Here is a Bash Scripting Tutorial which is good if you are a beginner. If you are new to the Linux command line here is a Linux tutorial which is also good for the beginner.


6

You should use -v option of awk: awk -F',' -v accNum="$1" '$1 == accNum {print $3,$2}' With $1 in double quote, the shell will handle all its special character ( if has ) for you.


2

Your script already practically does the job without any awk at all: while IFS=, read -r num last first do [ $((num==accountNum)) -eq 1 ] && printf '%s.%s\n' "$first" "$last" done < Accounts I'm not suggesting that this is a better or more efficient solution than using awk alone, but if you want the while...read loop, this ...


2

You have: accountNum=$1 awk -F',' '{ if($1==accountNum) { print $3.$2 } }' Accounts How does the shell variable accountNum get into awk? It doesn't: you need to provide its value to awk explicitly. You can use: awk -F',' '{ if($1=='"$accountNum"') { print $3.$2 } }' Accounts which leaves single quotes and has the shell substitute the value of its ...


0

Awk isn't going to make your life easier. You can use the suffix removal syntax: ${x%.*} is the value of the variable x with the shortest suffix of the form .* removed. for x in *.txt.*.*; do mv "$x" "/other/directory/${x%.*}" done


-1

This should do the job: rename -vn 's/.[0-9,:,_]+$//' /path/to/* Call the command without the -n switch when you see that the file get renamed correctly in the output. rename will print the filenames (-v). With this command everything after the last point (.) will be deleted out of the filename from every (*) file in the folder /path/to/. Output in my ...


0

#!/bin/bash if [ -f $1 ] ; then sed 's/^M$//' $1 > $1.txt total=0 amount=0 echo "#Name Surname City Amount" while read -r LINE || [[ -n $LINE ]]; do firstName=$( echo $LINE | cut -d " " -f1) lastName=$( echo $LINE | cut -d " " -f2) city=$( echo $LINE | cut -d " " -f3) amount=$( echo $LINE | cut -d ...


1

If you see : integer expression expected, it's a sign that what's before the : ends with a carriage return character. A carriage return causes your terminal to overwrite the current line with the subsequent text, so if a field contains something like 1234␍ where ␍ is a carriage return, the shell displays the error message 1234␍: integer expression expected, ...


4

You can actually do this with xclip. The secret is the -t option, which refers you to the ICCCM Section 2.6.2. Which I confess I don't blame you for failing to read... Anyway, an example, I have just right-clicked your avatar in Chromium and selected "Copy Image": See what targets are available: $ xclip -selection clipboard -t TARGETS -o TARGETS ...


0

Ok, as I needed this again, I put together a Perl script that does it; it turned out to be somewhat complicated, so I released it here: http://sourceforge.net/p/sdaaubckp/code/HEAD/tree/single-scripts/revrs-tree.pl What it does, is that it parses the output from tree - but unlike the OP example, it also needs the --dirsfirst option. To avoid remembering ...


1

It seems applying a command line argument to a bsub file is a very complicated process. I tried the HEREDOC method stated by mikeserv, but bsub acted as if the script filename was a command. So the easiest way to get around this problem is just to not use input redirection at all. Since my question specifically involved bsub for Platform LSF, the following ...


3

How about assigning it to a variable? #!/bin/sh user="$(zenity --entry --title="Username" --text="Username")" pass="$(zenity --password --title="Password" --text="Password")" xfreerdp /cert-ignore /f /v:farm.company.com /d:company.com /g:rds.company.com /u:${user} /gd:company.com /gu:${user} /gp:${pass} /p:${pass} Just a heads up, this is not a good ...


0

You need either a here document or a here string. bsub <<HEREDOC $(script.bsub 1) HEREDOC That's what a here document looks like. The shell will read in the expansion of everything between the line in which <<HEREDOC occurs until it finds a line on which nothing but HEREDOC occurs and pass the output to the invoked command on its file ...


2

First a note on style. Using a for loop to iterate over lines of output is rarely a good idea. The for loop will split its input on whitespace so if you have more than a single word per line, it will break each into a different variable.You should use while instead since this deals with whitespace more gracefully. Also, it is generally preferred to use ...


2

With bash: [ $UID -ne 0 ] && echo "Must be root to run script" && exit 1


0

In awk: parse each input line to find the current index after the TEST01N prefix. If the current output line number (n) is less than the index, print missing lines before copying the input line. Finally add more lines to reach the desired count. { i = match($1,/[0-9]+$/); prefix = substr($1,1,i-1); current = substr($1,i) + 0; while (++n < ...


3

[ $UID -ne 1000 ] && exit # exit if UID ist not equal 1000


3

You could do this: if [ "$(id -u)" = "1000" ]; then do stuff for user 1000 else do stuff for other users fi However, it would probably be more effective to chown the script to that user and then chmod 700, since it appears from your example that you are attempting to stop execution of the script for other users. This would also give you more ...


0

bash solution: #!/bin/bash FILE="$1" for i in $(seq 1 18) do i_format=$(printf "%02d" $i) T="TEST01N"${i_format} l=$(awk '$1 ~ /'$T'/{print $0}' $FILE) if [[ -z $l ]] then echo -e $T" empty" else echo $l fi done save to file (e.g. s.bash) and run: bash s.bash "/tmp/file" > "/tmp/file2" (/tmp/file is you ...


11

Use the convert command from ImageMagick: convert x.png -resize 25% x.jpg will create a 25%-scaled JPEG version of x.png. To do all the PNG files at once, use a for loop: for filename in *.png ; do convert "$filename" -resize 25% "${filename%.png}.jpg" ; done We look at every file matching *.png, and for each one run the convert command above. ...


1

I was actually able to figure this out, and I figure I'd add it here for the next googler who bangs their head against the same wall. I had a grep alias and GREP_OPTIONS set. This caused color highlighting to remain on in the script, even when piping to another command. That usually doesn't play nicely with sed. Here's my .alias and options: alias ...


1

Here is a perl solution: $ perl -anle 'BEGIN {$pattern = "TEST01N"} $curr = $F[0] =~ s/$pattern//r; if ($prev and ++$prev != $curr) { printf "%s%02d empty\n", $pattern, $prev; redo; } $prev = $curr; print; ' file TEST01N01 40 2002 2697 2523 TEST01N02 40 4 4 5 TEST01N03 empty TEST01N04 40 4 4 5 TEST01N05 40 4 4 5 ...


1

The solution for me was: crontab for a "non-root user" with the line: @reboot /usr/local/share/myserver/cn4server.sh restart It works fine, although it's a workaround.


0

If an "all defaults" key is acceptable, this works: $ ssh-keygen < /dev/zero > /dev/null 2>&1 You don't need the zero > /dev/null 2>&1 bit if you don't care about the complaints ssh-keygen emits when you feed input from /dev/zero like that. This will not overwrite an existing key, and it will only generate the default key type.


1

With GNU sed anhd grep, you can try: sed -n "/$(date +%y%m%d)/,\$p" file | grep -c partitioned /pattern/,$ matched first pattern to the end of file. With your input: $ sed -n "/$(date +%y%m%d)/,\$p" 1.txt | grep -c partitioned 1 Without matching date: $ sed -n "/$(date +%y%m%d)/,\$p" 1.txt | grep -c partitioned 0


1

You could try chaining two greps together: grep `date +"%y%m%d"` mysql-error.log | grep -c partitioned Or even simpler: grep -c "`date +%y%m%d`.*partitioned" mysql-error.log EDIT As the date and the word 'partitioned' are on different lines, then: d=$(date +"%y%m%d"); awk '/'$d'/,EOF {print $0}' mysql-error.log | grep -c partitioned Or: awk ...


0

This will work (do not type any extra spaces): command &>output.file


2

How about: date -d "yesterday" "+%y_%m_%d-" 14_07_08 Or for 4 digit year: date -d "yesterday" "+%Y_%m_%d-" 2014_07_08 To assign it to a variable, use: d=$(date -d "yesterday" "+%y_%m_%d-") Then: echo "$d" 14_07_08 Or: echo "Yesterday was $d" Yesterday was 14_07_08


4

You can authorize as many public keys as you like on the server side. Furthermore, you can restrict a key to a specific command on the server side. So generate an SSH key pair on the client, and don't put a password on the private key. Append the public key to the list of authorized keys, and add a command restriction. ssh-keygen -t rsa -f ...


0

You can either use ssh-keygen procedure as suggested in comment and answered here. For a longer version you can use even this link Linux Cookbook. It can open a security hole if you use shared account on your machine (everybody from that account can access to all the machine for which you did the ssh-keygen authentication). write a little script that ...


1

Following an article from here: You should setup SSH connection without Password Using ssh-keygen then it will allow you to use scp without prompting password or any other remote task: 3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id You can login to a remote Linux server without entering password in 3 simple steps ...


0

You could write your own in Lisp http://www.gigamonkeys.com/book/practical-parsing-binary-files.html


3

I would recommend creating a private/public key pair on the client machine, and copying the public key to the remote machine. You can generate such a keypair with ssh-keygen and copy it to the remote machine using ssh-copy-id. The logs are probably readable by all user accounts on the server (at least they are on my machine). You should therefore not use ...


3

You've injected the characters Ctrl+A, H and Return into the application (bash) running in the Screen window. The string that is passed to stuff is not parsed for Screen escapes. screen -S test -X log on seems to work, or screen -S test -X log to toggle as you want.


1

There is no such tool that I've ever seen. You could write a script in a variety of scripting languages, including Bash, to parse the output from tree and reconstruct a corresponding directory on disk that matched it. Looking through the text file would require a while or for loop and making use of mkdir or mkdiir -p to create a directory or a nested ...



Top 50 recent answers are included