Having troubles with searching in specific fashion with bash

  • Thread starter Arnoldjavs3
  • Start date
  • Tags
    Specific
In summary, the student is trying to solve a homework problem and is having difficulty. They are asking for help from the instructor.
  • #1
Arnoldjavs3
191
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
  • #2
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.
 
  • #3
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!
 
  • #4
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
 
  • #5
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

FAQ: Having troubles with searching in specific fashion with bash

How do I search for specific files or directories using bash?

To search for specific files or directories in bash, you can use the find command. For example, to find all files with a specific extension, you can use the command find . -name "*.txt" which will search for all files ending with the .txt extension in the current directory and its subdirectories.

Can I search for a specific string within a file using bash?

Yes, you can use the grep command to search for a specific string within a file. For example, grep "apple" fruits.txt will search for the word "apple" in the file fruits.txt and return any lines that contain the word.

How can I search for a specific pattern in multiple files using bash?

To search for a pattern in multiple files, you can use the grep command with the -r flag. For example, grep -r "pattern" folder will search for the pattern in all files within the specified folder and its subdirectories.

What is the difference between single and double quotes in bash?

In bash, single quotes and double quotes are used to enclose a string. The main difference is that single quotes will treat all characters inside as literal, while double quotes will allow for variable expansion and command substitution. For example, echo 'Hello $NAME' will print Hello $NAME, while echo "Hello $NAME" will print Hello [your name] if you have a variable NAME set.

How do I search for files based on their size using bash?

You can use the find command with the -size flag to search for files based on their size. For example, find . -size +1M will return all files larger than 1 megabyte in the current directory and its subdirectories.

Similar threads

Replies
3
Views
4K
Replies
2
Views
2K
Replies
105
Views
7K
Replies
6
Views
3K
Replies
1
Views
1K
Replies
21
Views
2K
Replies
4
Views
6K
Back
Top