Posts mit dem Label bash werden angezeigt. Alle Posts anzeigen
Posts mit dem Label bash werden angezeigt. Alle Posts anzeigen

Freitag, 10. Februar 2012

Bash if statement

The Bash if statement is a bit funny. Say for every files-i.txt, there should be a files-i.dat.
This example can be used to filter out files that dont have the corresponding .dat file present.


for i in files-*txt
do
if [ ! -e ${i/txt/dat} ]
then
echo $i
fi
done


Note the spaces after and before the opening and closing hard bracket. Also note that "then" is on its own line.

Donnerstag, 7. Juli 2011

Shell 007: Zero Padding

Zero padding is useful when naming files and everything should be nicely aligned when listed.
This can be done by using

for i in 000{1..9) 10
do
echo $i
done

Montag, 30. Mai 2011

Shell 006: Replacing String with incrementing index

Lets say we have some text as below.

header
foo
foo
foo
header
bar
bar
bar
header
baz
baz
baz

where we want the "header" be replaced by "model {1,2,3}".
I was pointed out to a couple of solutions over at LinuxQuestions.org

An awk solution.

$ awk 'BEGIN { cntr = 0 } /header/ { cntr++ ; print "model", cntr } !/header/ { print $0 }' infile

where 'infile' contains the data.

A bash solution.

#!/bin/bash

filename=data

index=1

for entry in `grep -n model $filename`
do
line=`echo $entry | awk -F":" '{print$1}'`

sed -e "$line s/model/model $index/" -i $filename

index=$(($index + 1))
done


An even shorter awk solution.

awk '$0 = /header/?"model "++c:$0' file


A one line bash solution

unset count; while read line; do [[ $line =~ header ]] && line="model $((++count))"; echo "$line" >> oufile; done < infile


Great to have the support from the online community.

Montag, 2. Mai 2011

Shell 006: Nested Calls

In Bash a command can be nested by the following syntax:

cd $(dirname $(which prog))

The syntax for command substitution that uses back ticks does not permit nesting. Therefore the $(command) expansion is required.

Mittwoch, 13. April 2011

Shell 005: String processing

A directory contains the following

~/shell $ ls -l
00_01_boxpdb.py
00_01_realign.py
00_02_callPDB2PQR.py
00_03_getPointCharges.py
00_04_getCharge.py
01_01_connect.py


How can the leading terms be removed?


~/shell $ for i in 0*
> do
> mv $i ${i:6}
> done


The syntax is ${i:FROM:BY}. The indexing starts from 0. Also

# Is it possible to index from the right end of the string?

echo ${stringZ:-4} # abcABC123ABCabc
# Defaults to full string, as in ${parameter:-default}.
# However . . .

echo ${stringZ:(-4)} # Cabc
echo ${stringZ: -4} # Cabc
# Now, it works.
# Parentheses or added space "escape" the position parameter.

Shell-004: Selective Listing

Given the case that a number of files are in a directory, file-test.py and at the same time there is a directory called file-test in the same directory. How can only the directories be listed? This is useful when the content of the directories needs to be processed somehow.

ls -l|grep ^d|awk '{print $8}'

Grep matches the beginning of the line output by 'ls' with 'd', which indicates a directory. " 'awk {print $8}' " then prints the 8th column of the output (so the directory name is available again).

Sonntag, 10. April 2011

Shell 005: Redirect Part of a File

Suppose a file contains data starting from a given keyword (the content above the keyword is considered not relevant). If only the content from after the keyword is to be redirected to a new file (in a batch), this can be done using sed.

for i in file-*.log
do
sed -n '0,/DONE/!p' $i > ${i/file/new}
done

"!" tells it to ignore everything from the start of the file "0" to the first line containing the keyword "/DONE/", then p makes it print the rest, with a shell redirection of the output to a new file.
Regards to David the H. at LinuxQuestions for pointing this out to me.

Freitag, 8. April 2011

Vim 002: Submitting Vim Command from the shell

A Vim command can be submitted to Vim directly from the shell (without entering Vim) by calling:
vi "+:%s/+0/+1/g" "+wq" $i

Notice the '+' operator within the command string. Also notice the 'wq' command to save and "exit" Vim once it finished its job. This is an excellent way of processing a batch of files.

Shell 003: Tail

All but the top 40 lines of a file can be catted to a new file by calling:



count=`cat file.dat|wc -l`
tail -n -$((count-40)) file.dat > newfile.dat


Notice the backtics to evaluate the cat and wc calls in the first line. Also notice the '-' sign in front of the numeric argument to the tail command.

Montag, 4. April 2011

Shell 004: Arithmetic Extension

Sometimes it is useful to have an arithmetic extension available in the BaSH. In its most primitive form this could look something like

mzh @ ~/shell $ for i in {1,2,3}
> do
> tmp=$((i*i))
> echo $tmp
> done
1
4
9

I found it here.

Dienstag, 15. März 2011

Shell 002: allsh

Good to know. A shell command can be issued for all running shells at once.
http://72.14.189.113/howto/shell/allsh/