...
Code Block |
---|
|
cat -n haiku.txt | \
while IFS= read line; do
echo "Line is: '$line'"
done
|
- The IFS= clears all of read's default Input Field Separator, which is normally whitespace (one or more space characters or tabs).
- This is needed so that read will set the line variable to exactly the contents of the input line, and not specially process any whitespace in it.
- The lines of ~/haiku.txt are piped into the while loop
...
Code Block |
---|
|
tail -n +2 ~/data/sampleinfo.txt | \
while IFS= read line; do
jobName=$( echo "$line" | cut -f 1 )
sampleName=$( echo "$line" | cut -f 3 )
if [ "$jobName" == "" ]; then
sampleName="Undetermined"; jobName="none"
fi
echo "job $jobName - sample $sampleName"
done | more |
...
- The double quotes around the text that "$line" are important to preserve special characters inside the original line (here Tab characters).
- Without the double quotes, the line's fields would be separated by spaces, and the cut field delimiter would need to be changed.
- Some lines have an empty Job name field; we replace Job and Sample names in this case.
...
Sometimes you want to take a file path like ~/my_file.something.txt and extract some or all of the parts before the suffix, for example, to end up with the text my_file here. To do this, first strip off any directories using the basename function. Then use the odd-looking syntax:
- $ ${<variable-name>%%.<suffix-to-remove>}
- $ ${<variable-name>##<prefix-to-remove>}
Code Block |
---|
|
pathname=~/my_file.something.txt; echo $pathname
filename=`basename $pathname`; echo $filename
# isolate the filename prefix by stripping the ".something.txt" suffix
prefix=${filename%%.something.txt}
echo $prefix
# isolate the filename suffix by stripping the "my_file.something." prefix
suffix=${filename##my_file.something.}
echo $suffix |
Exercise 3-12
Use the suffix-removal syntax above to strip the .bed suffix off files in ~/data/bedfiles.
Expand |
---|
|
Code Block |
---|
| cd ~/data/bedfiles
for bedf in *.bed; do
echo "BED file is $bedf"
pfx=${bedf%%.bed}
echo " .. prefix is $pfx (after the .bed suffix is stripped)"
done |
|
A few odds and ends
Input from a sub-shell
When parentheses ( ) enclose an expression, it directs that expression be evaluated in a sub-shell of the calling parent shell. Recall also that the less-than sign < redirects standard input. We can use these two pieces of syntax instead of a file in some command contexts.
...