grep output show result with file name but not full path [duplicate] - bash

This question already has answers here:
How can I use grep to show just filenames on Linux? [closed]
(3 answers)
Closed 5 months ago.
I use grep (egrep) to search text a log. I have multiple txt files to search in the same folder. By default, grep output file name (full path), with math result.
The directory path is long so it looks ugly.
However, if I use -h option, it only give me match result but not file name, which is not good either, because sometimes I need to know which file it is from.
for example, in folder bashsearch I have file1, file2, file3...
The default output is like this:
/mnt/c/Users/chili/Desktop/.../bashsearch/file1:match result 1
/mnt/c/Users/chili/Desktop/.../bashsearch/file2:match result 2
this looks ugly.
If I use -h, the output is like this:
match result 1
match result 2
this is not good either.
What I want is like this:
file1:match result 1
file2:match result 2
How to achieve this please? Any help is highly appreciated.

You can reformat output by using pipe to another command. For example
| grep -o "[^/]*:.*"
will remove the path part.

Related

From all the files that their name is composed of 4 letters, which contain the string “user” in their content?

I have to answer this question as an exercise.
Sample input: No sample, just trying to select and filter files using Unix shell according to some conditions
Sample output: a list of files that their name is composed of 4 letters and which contain the string “user” in their content.
I tried to use the basename ~/ command to get the file name of some files, then tried to combine it with wc by doing, for example, basename /etc/ |wc -c.
Finally, I tried grep user file_test.txt on an arbitrary file to see if it contains the word "user".
I am trying to combine all the required commands to answer the question.
I am supposed to use substitutions which I am not used to.
Could someone please help me?

Replace a whole line using sed [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 4 years ago.
I am very new to this all and have used this website to help me find the answers i'm looking for.
I want to replace a line in multiple files across multiple directories. However I have struggled to do this.
I have created multiple directories 'path_{0..30}', each directory has the same 'input' file, and another file 'opt_path_rx_00i.xyz' where i corresponds to the directory that the file is in (i = {0..30}).
I need to be able to change one of the lines (line 7) in the input file, so that it changes with the directory that the input file is in (path_{0..30}). The line is:
pathfile opt_path_rx_00i.xyz
Where i corresponds to the directory that the file is in (i={0..30})
However, i'm struggling to do this using sed. I manage to change the line for each input file in the respective directories, but i'm unable to ensure that the number i changes with the directory. Instead, the input file in each directory just changes line 7 to:
pathfile opt_path_rx_00i.xyz
where i, in this case, is the letter i, and not the numbers {0..30}.
I'll show what i've done below in order to make more sense.
for i in {0..30}
do
sed -i '7s/.*/pathfile-opt_path_rx_00$i.xyz/' path_$i/input
done
What I want to happen is, for example in directory path_3, line 7 in the input file will be:
pathfile opt_path_rx_003.xyz
Any help would be much appreciated
can you try with double quotes
for i in {0..30}; do
sed -i "7s/.*/pathfile-opt_path_rx_00$i.xyz/" "path_$i/input"
done

Put pipe value into variable [duplicate]

This question already has an answer here:
How to redirect grep output to a variable?
(1 answer)
Closed 8 years ago.
I'm new in bash scripting world...
I'm trying to get value from pipe action to a var.
Something like:
result = $(ls /usr/bin | dmenu)
the idea is put files list into a standar menu (dmenu) so, when user select
a choice, i want to know wich one is selected and work with this option
to for example, execute a file.
the $result is not getting any value.
Thanks for your help
Remove whitespaces before and after =:
result=$(ls /usr/bin | dmenu)

Using bash to list files with a certain combination of characters

So I have a directory with ~50 files, and each contain different things. I often find myself not remembering which files contain what. (This is not a problem with the naming -- it is sort of like having a list of programs and not remembering which files contain conditionals).
Anyways, so far, I've been using
cat * | grep "desiredString"
for a string that I know is in there. However, this just gives me the lines which contain the desired string. This is usually enough, but I'd like it to give me the file names instead, if at all possible.
How could I go about doing this?
It sounds like you want grep -l, which will list the files that contain a particular string. You can also just pass the filename arguments directly to grep and skip cat.
grep -l "desiredString" *
In the directory containing the files among which you want to search:
grep -rn "desiredString" .
This can list all the files matching "desiredString", with file names, matching lines and line numbers.

bash - reading multiple input files and creating matching output files by name and sequence

I do not know much bash scripting, but I know the task I would like to do would be greatly simplified by it. I would like to test a program against expected output using many test input files.
For example, I have files named "input1.txt, input2.txt, input3.text..." and expected output in files "output1.txt, output2.txt, output3.txt...". I would like to run my program with each of the input files and output a corresponding "test1.txt, test2.txt, test3.txt...". Then I would do a "cmp output1.txt test1.txt" for each file.
So I think it would start like this.. roughly..
for i in input*;
do
./myprog.py < "$i" > someoutputthing;
done
One question I have is: how would I match the numbers in the filename? Thanks for your help.
If the input file name pattern is inputX.txt, you need to remove input from the beginning. You do not have to remove the extension, as you want to use the same for output:
output=output${i#input}
See Parameter Expansion in man bash.

Resources