I'm using the terminal and want to trace all the appearances of a certain sentence.
I have navigated to the folder and written the following command:
grep 'myWord' > findWord.txt
The file is empty and the I need to press ^C in order to exit the command.
I would also like to know in what files this word exists.
You failed to give the input file. Anything after the output redirection operator is considered as output file. So Your grep syntax would be,
grep 'myWord' input-file > output-file
Related
I need to run a repeat command with the different filename to get the header.
However, I need to run each file.
dfits *.fit | grep MSBTITLE
Is there any command I can run several files and show the filename and the header I need?
grep does not know the filename, so you see only the matching lines, but not which file they come from originally. I would in your case write an explicit loop:
for file in *.fit
do
if titleline=$(dfits $file|grep MSBTITLE)
then
echo $file : $titleline
fi
done
Since dfits already obscures the file name in its output, we store the output from grep into a variable, and if there is a match, output this line together with the file name.
I have a directory containing files numbered like this
1>chr1:2111-1111_mask.txt
1>chr1:2111-1111_mask2.txt
1>chr1:2111-1111_mask3.txt
2>chr2:345-678_mask.txt
2>chr2:345-678_mask2.txt
2>chr2:345-678_mask3.txt
100>chr19:444-555_mask.txt
100>chr19:444-555_mask2.txt
100>chr19:444-555_mask3.txt
each file contains a name like >chr1:2111-1111 in the first line and a series of characters in the second line.
I need to sort files in this directory numerically using the number before the > as guide, the execute the command for each one of the files with _mask3 and using.
I have this code
ls ./"$INPUT"_temp/*_mask3.txt | sort -n | for f in ./"$INPUT"_temp/*_mask3.txt
do
read FILE
Do something with each file and list the results in output file including the name of the string
done
It works, but when I check the list of the strings inside the output file they are like this
>chr19:444-555
>chr1:2111-1111
>chr2:345-678
why?
So... I'm not sure what "Works" here like your question stated.
It seems like you have two problems.
Your files are not in sorted order
The file names have the leading digits removed
Addressing 1, your command ls ./"$INPUT"_temp/*_mask3.txt | sort -n | for f in ./"$INPUT"_temp/*_mask3.txt here doesn't make a whole lot of sense. You are getting a list of files from ls, and then piping that to sort. That probably gives you the output you are looking for, but then you pipe that to for, which doesn't make any sense.
In fact you can rewrite your entire script to
for f in ./"$INPUT"_temp/*_mask3.txt
do
read FILE
Do something with each file and list the results in output file including the name of the string
done
And you'll have the exact same output. To get this sorted you could do something like:
for f in `ls ./"$INPUT"_temp/*_mask3.txt | sort -n`
do
read FILE
Do something with each file and list the results in output file including the name of the string
done
As for the unexpected truncation, that > character in your file name is important in your bash shell since it directs the stdout of the preceding command to a specified file. You'll need to insure that when you use variable $f from your loop that you stick quotes around that thing to keep bash from misinterpreting the file name a command > file type of thing.
Sorry for the title, i couldn't find proper words to explain my problem.
Here's the code:
wlan_c=$(iwconfig | sed '/^\(w.*$\)/!d;s/ .*//' > ./wifi_iface)
wlan=$(<./wifi_iface)
echo "$wlan"
I get the following output:
lo no wireless extensions.
enp4s0 no wireless extensions.
wlp2s0
The last line is the result of execution the echo "$wlan".
The previous lines coming from the iwconfig, those that are not getting formatted by sed.
And the file ./wifi_iface also has the info i need.
Everything works as intended.
So i really want to get rid of that unwanted output before the wlp2s0 line.
How do i manage to do this?
That output must be going to stderr rather than stdout. Redirect it to /dev/null
iwconfig 2>/dev/null | sed '/^\(w.*$\)/!d;s/ .*//' > ./wifi_iface
There's no need to assign this to wlan_c. Since you're writing to the file, nothing will be written to stdout, so the assignment will always be empty.
This should be a no-brainer, but apparently I have no brain today.
I have 50 20-gig logs that contain entries from multiple apps, one of which addes a transaction ID to its log lines. I have 42 transaction IDs I need to review, and I'd like to parse out the appropriate lines into separate files.
To do a single file, the command would be simply,
grep CDBBDEADBEEF2020X02393 server.log* > CDBBDEADBEEF2020X02393.log
that creates a log isolated to that transaction, from all 50 server.logs.
Now, I have a file with 42 txnIDs (shortening to 4 here):
CDBBDEADBEEF2020X02393
CDBBDEADBEEF6548X02302
CDBBDE15644F2020X02354
ABBDEADBEEF21014777811
And I wrote:
#/bin/sh
grep $1 server.\* > $1.log
But that is not working. Changing the shebang to #/bin/bash -xv, gives me this weird output (obviously I'm playing with what the correct escape magic must be):
$ ./xtrakt.sh B7F6E465E006B1F1A
#!/bin/bash -xv
grep - ./server\.\*
' grep - './server.*
: No such file or directory
I have also tried the command line
grep - server.* < txids.txt > $1
But OBVIOUSLY that $1 is pointless and I have no idea how to get a file named per txid using the input redirect form of the command.
Thanks in advance for any ideas. I haven't gone the route of doing a foreach in the shell script, because I want grep to put the original filename in the output lines so I can examine context later if I need to.
Also - it would be great to have the server.* files ordered numerically (server.log.1, server.log.2 NOT server.log.1, server.log.10...)
try this:
while read -r txid
do
grep "$txid" server.* > "$txid.log"
done < txids.txt
and for the file ordering - rename files with one digit to two digit, with leading zeroes, e.g. mv server.log.1 server.log.01.
I've been using the egrep command a lot lately (just out of preference over grep) and I have noticed a behaviour that I don't fully understand.
When I run the following command in my home directory:
egrep -r "main" *
it does what I expect and returns all of the lines and respective files that contain "main".
Although if I enter:
egrep -r "main"
and forget to put the star at the end, it seems to just hang forever.
I'm hoping someone might be able to shed some light as to why this is and what it is doing?
Thanks :)
It's waiting for input from stdin because you didn't specify a filename, from the grep manpage:
NAME
grep, egrep, fgrep - print lines matching a pattern
SYNOPSIS
grep [options] PATTERN [FILE...]
grep [options] [-e PATTERN | -f FILE] [FILE...]
DESCRIPTION
Grep searches the named input FILEs (or standard input if no files
are named, or the file name - is given) for lines containing a
match to the given PATTERN. By default, grep prints the matching lines.
grep (or egrep) needs to know what file(s) to search. By including the *, you're telling it to search in all files in the current directory.
If you don't include the *, it doesn't know where to look, so it awaits your input, and will prompt you for input to search (type it in, then press ctrl+d to terminate standard input mode).
This behavior is not unique to grep. Any command that requires input wants that input to come from a file or from standard input (stdin).
stdin data can come from a pipe or it can be typed in manually at the prompt after entering your command (which is what you were seeing when you thought it was hung). As i mentioned, pressing Ctrl+D will get you out of that input mode.
You didn't supply the path so grep waits on stdin for data.
Example with path:
egrep -r "pattern" . <- search in current directory and subdirectories
cat file | egrep "pattern" <- search in data from pipe