Having troubles with searching in specific fashion with bash

  • Thread starter Thread starter Arnoldjavs3
  • Start date Start date
  • Tags Tags
    Specific
Click For Summary

Discussion Overview

The discussion revolves around a homework problem involving searching specific columns in a text file using bash scripting. Participants explore methods to search columns 10-15 for matches and subsequently extract and print columns 20-35 of those matches. The conversation includes various approaches and considerations regarding the use of built-in shell features versus external utilities.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant describes their initial attempt using 'cut' and 'grep' to search and filter data from the text file.
  • Another participant suggests that it is important to clarify whether only built-in shell features can be used or if standard utilities are allowed.
  • A different approach is proposed, emphasizing the use of built-in features to read the file line by line and extract the necessary columns without relying on external commands.
  • One participant expresses concern about the limitations imposed by the homework guidelines, indicating they have previously implemented a line-by-line reading method.
  • Another participant advises on managing variable accumulation in bash, suggesting that appending to a file may be more efficient for large data sets.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to solve the problem, as multiple methods and considerations are presented. There is also a recognition of the constraints imposed by the homework assignment.

Contextual Notes

Participants discuss the implications of using built-in features versus external utilities, as well as the potential issues related to handling large amounts of data in variables.

Arnoldjavs3
Messages
191
Reaction score
3

Homework Statement



Say I have a textfile that is separated 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?

Homework Equations

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.
 
Physics news on Phys.org
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.
 
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[/color]

It's not as scary as it might seem!
 
NascentOxygen said:
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!

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 ]
then
variable += col 20-30 + \nHow could I do this in bash programming? In java when you want to add a string you can just use the += operator
 
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.
 
  • Like
Likes   Reactions: Arnoldjavs3

Similar threads

  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 105 ·
4
Replies
105
Views
8K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 16 ·
Replies
16
Views
6K