1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Having troubles with searching in specific fashion with bash

  1. Mar 1, 2017 #1
    1. The problem statement, all variables and given/known data

    Say I have a textfile that is seperated by columns, and I want to search columns 10-15. If there are any matches, then I want to cut out specific parts of all the matches and print it to the user. How do I do this?

    2. Relevant equations

    3. The attempt at a solution
    What I tried was:

    cut -c 10-15 textfile.txt | grep -q $userInput

    But then how would I cut out the specific results from this entry?
    I want to search columns 10-15, then print out columns 20-35 of all results from that search.
  2. jcsd
  3. Mar 2, 2017 #2


    User Avatar

    Staff: Mentor

    This is a homework exercise, right? So we can't simply hand you the code.

    It needs to be made clear when solving Shell exercises whether you are expected to use only builtin features of that particular shell, or whether you are allowed to also use standard utilities such as cut, grep, sed, and even awk and perl.

    One way: loop through the file reading it into a variable one line at a time and use the utilities to operate on $line. If there's a match in that $line then cut the other columns of $line.

    Simply 'cut'ing the other columns is sufficient, by default this prints the result to standard output.
  4. Mar 2, 2017 #3


    User Avatar

    Staff: Mentor

    An alternative approach:

    Providing it can be guaranteed there will be no nasties in your data file, it may be feasible to write the script using all builtins and avoid completely the need for cut and grep, e.g., you could build your shell script around a line like this:

    read -r c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 c25 c26 c27 c28 c29 c30 c31 c32 c33 c34 c35 c36

    It's not as scary as it might seem!
  5. Mar 2, 2017 #4
    it is for a class, and unfortunately my freedom for how I solve the homework is limited.

    I have actually done what you stated before where it reads the file line by line, however I have a question.

    Say I have a condition:

    if [ substring is found in line i, col 10-15 ]
    variable += col 20-30 + \n

    How could I do this in bash programming? In java when you want to add a string you can just use the += operator
  6. Mar 2, 2017 #5


    User Avatar

    Staff: Mentor

    You would only accumulate stuff into a variable if you were sure it wasn't going to run into megabytes of data, otherwise append it to a file.

    You want to assign three things to that variable: its own current value, the result of a shell command, and a Unix end-of-line character. You don't need plus signs, just write them one after another on a line. $(cut ...) might be useful.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted