Using linux “cut”

The following code takes a file that has 16 columns and outputs a file with 5 of those columns.  Some notes:

  • Don’t use PATH as a variable.  The program won’t work, because PATH is a system variable!
  • Note the C++ style syntax of the loop.  Versions of bash greater than 3.0 allow you to use curly brackets, like: for i in {1..50}.  But when you want to use variables inside the range, you have to do something else, such as my example.  Others are discussed here.
  • The star of this script is the ‘cut’ command.  d tells what delimiter you’d like.  f tells what fields you want to cut.
  • Then there are some simple commands around cut.  ‘cat’ displays the contents of the file.  Then, the | operator pipes the output of cat into the next command, which is cut.  Finally, you then use the > operator to direct the output of this command into a new file.
  • Save this file on the cluster or a Linux system as myFileNameHere.sh.  Then, to run the code, simply type “sh myFileNameHere.sh”

#!/bin/bash

# Cut out only the objective function values from the CBorg output files.

MYPATH=./output/
INPUT_NAME_BASE=CBorg_LRGV_
OUTPUT_NAME_ADDENDUM=_ObjOnly
EXTENSION=.out
START_COLUMN=9
FINISH_COLUMN=13
NUM_SEEDS=50

echo "Beginning..."
for ((I=1; I<=$NUM_SEEDS; I++));
do
 echo "Processing $I"
 cat ${MYPATH}${INPUT_NAME_BASE}${I}${EXTENSION} | cut -d ' ' -f ${START_COLUMN}-${FINISH_COLUMN} > ${MYPATH}${INPUT_NAME_BASE}${I}${OUTPUT_NAME_ADDENDUM}${EXTENSION}
done
echo "Totally done."

Advertisements

5 thoughts on “Using linux “cut”

  1. Pingback: Using linux “split” « Pat Reed Group Research Tips Blog

  2. Pingback: Useful Linux commands to handle text files and speed up work | Water Programming: A Collaborative Research Blog

  3. Pingback: Water Programming Blog Guide (Part I) – Water Programming: A Collaborative Research Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s