Having troubles with searching in specific fashion with bash

  • Thread starter Thread starter Arnoldjavs3
  • Start date Start date
  • Tags Tags
    Specific
Click For Summary
The discussion focuses on searching specific columns in a text file using Bash, particularly columns 10-15, and printing results from columns 20-35 based on matches. Users suggest using utilities like `cut` and `grep`, or looping through the file line by line to check for matches and extract desired columns. There is a debate on whether to use built-in shell features or standard utilities, emphasizing that homework constraints limit the approach. Concerns about handling large data sets are raised, advising against accumulating results in a variable if data size is uncertain. The conversation highlights the importance of understanding Bash scripting techniques for effective text processing.
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 Arnoldjavs3

Similar threads

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