Edit: I should have pointed out originally, as I have now received feedback on this. This is NOT the best or optimal way of performing this task. I was trying to illustrate as many shell scripting principles as possible in terms of ‘if’, ‘for’, counters, etc, and how such a one liner has been put together. Perhaps I should have thought of a better way of illustrating such principles, but nevertheless, here it is!
Here’s a quick one liner, can’t think why anyone would ever have any use for it, but maybe the principle itself could be of use to someone! This will take a file containing listings of 16 digit numbers, i.e. 1234123412341234 and replace it with XXXXXXXXXXXX1234
Duly spaced and indented:
P=”"
ctr=0;
for I in `echo $I|grep -o .`; do
let ctr=$ctr+1;
if [ $ctr -gt 12 ]; then
P=${P}${I};
else
P=${P}”X”;
fi;
done;
echo $P|tr -d ‘\n’;
echo -ne “\n”;
done
Would love anyone to comment with variations.
Tags: bash, echo, grep, script, tr
On the command line we have a number of powerful tools available to us. I’m going to cover some text sorting methods here.
I have a file called ‘testfile’ within this file is the following:
test:~# cat testfile
line1
line3
abcdefg
test
line9
this is a test
test file
test
(more…)
Tags: awk, cat, grep, sed, sort, text sort, uniq
We have three relevant streams when dealing with passing data around on the command line. STDIN (0), STDOUT (1) and STDERR (2)
echo “hello” will return “hello” to STDOUT
echo “hello” | sed s/llo/y/g
Returns: ‘hey’
echo “hello” will print “hello” to STDOUT which we pipe to sed’s STDIN. The shell will fork both processes, echo and sed, and create a pipe between one’s STDOUT to the other’s STDIN. A ‘broken pipe’ will occur when one terminates unexpectedly.
strace echo “hello” will print the system calls that the command makes. Lets say I just want to print out open() calls.
strace echo “hello” | grep open does not work. It seems that the grep is ignored.
This is because strace sends it’s output to STDERR and not STDOUT. In this case we must redirect STDERR to STDOUT so grep can pick it up on it’s STDIN.
strace echo “hello” 2>&1 | grep open will work successfully.
What if we want to redirect STDOUT and STDERR to a file? We simply redirect STDOUT to a file and then redirect STDERR to STDOUT.
strace echo “hello” >/tmp/strace.output 2>&1
A nonstandard method of achieving the same by redirecting everything in one go is strace echo “hello” &>/tmp/strace.output however this is not guaranteed to work across all implementations.
* Post edited thanks to observations from Adam Bolte (16/11/09)
Tags: echo, grep, pipe, redirect, sed, stderr, stdin, stdout
I was asked today how to sort a string alphabetically with BASH
Using perl, you can easily enough use
print (join “”, sort split //,$_)
With bash however, the best option is:
echo “teststring” | grep -o . | sort -n |tr -d ‘\n’; echo
Which returns: eginrssttt
A good way of enumerating each character from a string in general is:
for (( i = 0; i < ${#str[@]}; i++ )); do echo “${str[$i]}”; done
Tags: bash, do, done, echo, for, grep, perl, print, shell, shell scripting, sort, string, tr