Run a command in a file on another text file - linux

I have a filter command saved in a file (say filter.txt). The command could be something similar to following
grep "PASS"
Then I also have a output file of a testcase (say output.log). I want to us the filter command saved in the filter.txt file on the output.log.
I was looking for something like
cat output.log | `cat filter.txt`
But seems like it does not work. Is there a proper way to do this?

This works:
cat output.log | bash filter.txt
You need some program (like bash) that interprets the lines in filter.txt as commands to be executed.

Related

Sed: read from file and display in STDOUT

Learning sed, and I was using a live editor so I can experiment/see changes.
sed -nf '/START FROM HERE/,${/NEXTLINE/{n;p;q}}'
When trying to run the same code, on Linux, I receive error No such file or Directory when I execute as ./xxx.sed text0.txt
I've tried a couple of things but I am not sure how to use sed like this.
The -f option means that the next argument is the name of a file containing the sed commands. So you need to put
/START FROM HERE/,${/NEXTLINE/{n;p;q}}
in the file xxx.sed. Then you do:
sed -nf xxx.sed test0.txt
If you want to be able to execute xxx.sed as a command, it needs a shebang line:
#!/usr/bin/sed -nf
/START FROM HERE/,${/NEXTLINE/{n;p;q}}
Then you can make the file executable and do:
./xxx.sed file0.txt

Command to open a file which contains the given data

I had this question in interview.
He put a situation in front of me that there are 12 files in your Linux operating system.
Give me a command which will open a file containing data "Hello"..
I told him I just know grep command which will give you the names of files having "Hello" data.
Please tell me if there is any command to open a file in this way..
Assuming it will be only one file containing the word hello:
less $(grep -H "hello" *.txt | sed s/:.*//)
Here it is first capturing the file name using grep with -H parameter. Then using sed removing everything except the filename. And finally its using less to open the file.
Maybe this could help:
$ echo "foo" > file1.txt
$ echo "bar" > file2.txt
$ grep -l foo * | xargs cat
foo
You have 2 files, and you are looking for the one with the string "foo" in it. Change cat with your command of choice to open files. Might try vi, emacs, nano, pico... (no, another flame war!)
You may want to try a different approach if there are several files that contains the string you are looking for... Just thought of only one file containing the string.

How to save screen output to a text file in Makefile

I was using the original makefile to build my code with Perl scripts written in makefile.
Now I want to print all the log shown on screen to a txt file.
What command can I use in my makefile in order to do this?
I was meant to use some command in makefile to output what will be shown on screen to a txt file.
for example, if the makefile looks like:
all:
perl filename.pl
how should I write in my makefile so that every time I type"make all" in command line, it will automatically save the output to a txt file?
Hi now I need to improve it, I need to save the output to a txt file whose directory should according to the input of my perl script.
For example, in the makefile:
all:
$(X)make -C .. DIR=$(DIR) Y=$(Y) Z=$(Z)
perl filename.pl $(DIR)/$(Y)/i.lst 2>&1 | tee log.txt
how should I change in the makefile so that the log.txt will be saved at the directory equals the input $DIR? Ans I also want to change the name "log" to the input, how could I do this?
Can anyone help?
You can have a make rule that invokes make recursively, redirecting things to a file:
all:
$(MAKE) everything >log.txt
everything: ...whatever you had before for all
Of course, if you just have one command, you might as well put the redirection in there:
all:
perl filename.pl >log.txt
If you want the output to both appear on the screen and be copied to the file, you can use tee:
all:
perl filename.pl | tee log.txt
..and if you want to include stderr output in the file, you can redirect that too:
all:
perl filename.pl 2>&1 | tee log.txt
From the command line, piping the output from make to a text file should be pretty straightforward:
make > new_file.txt
Use two arrows instead if you want it to concatenate instead of replace each time:
make >> new_file.txt

Bash standard output display and redirection at the same time

In terminal, sometimes I would like to display the standard output and also save it as a backup. but if I use redirection ( > &> etc), it does not display the output in the terminal anymore.
I think I can do for example ls > localbackup.txt | cat localbackup.txt. But it just doesn't feel right. Is there any shortcut to achieve this?
Thank you!
tee is the command you are looking for:
ls | tee localbackup.txt
In addition to using tee to duplicate the output (and it's worth mentioning that tee is able to append to the file instead of overwriting it, by using tee -a, so that you can run several commands in sequence and retain all of the output), you can also use tail -f to "follow" the output file from a parallel process (e.g. a separate terminal):
command1 >localbackup.txt # create output file
command2 >>localbackup.txt # append to output
and from a separate terminal, at the same time:
tail -f localbackup.txt # this will keep outputting as text is appended to the file

Send multiple outputs to sed

When there is a program which, upon execution, prints several lines on stout, how can I redirect all those lines to sed and perform some operations on them while they are being generated?
For example:
7zip a -t7z output_folder input_folder -mx9 > sed 's/.*[ \t][ \t]*\([0-9][0-9]*\)%.*/\1/'
7zip generates a series of lines as output, each including a percentage value, and I would like sed to display these values only, while they are being generated. The above script unfortunately does not work...
What is the best way to do this?
You should use the pipe | instead of redirection > so that sed uses first command output as its input.
The above script line must have created a sed file in the current directory.
Furthermore, maybe 7zip outputs these lines to stderr instead of stdout. If it is the case, first redirect standard error to standard output before piping: 2>&1 |

Resources