In my project, I've called one method in multiple files. I want to see all files in which that method is called. Using command line, is there a way to list all files which contain specific word?
You could use grep
grep [word_to_find] [files]
For example, find all method definitions in python files
grep def *.py
Related
I need to combine all the csv files in some directory (.csv), provided that there are other files with the same name in this directory, but with different expansion (.csv.done).
If a csv file doesn't have .done in this extension then I don't need it for combine process.
What is the best way to do it using Bash ?
This approach is a solution to your problem. I see you've commented that it "didn't work", but whatever the reason is for it not working, it's likely simple to fix e.g. if you forgot to include key details, or failed to adapt it appropriately to suit your specific situation. If you need further help troubleshooting, add more info to your question.
The approach:
for f in *.csv.done
do
cat "${f%.*}" >> combined_file.csv
done
How it works:
In your example, you have 3 files named 1.csv 2.csv 3.csv and two 'done' files named 1.csv.done 2.csv.done.
This script begins by making a list of all files that end in .csv.done (two files: 1.csv.done 2.csv.done).
It then uses a parameter expansion, specifically ${parameter%word}, to 'shorten' the name of the two files in the list to .csv (instead of .csv.done).
Then it 'prints' the content of the two 'shortened' filenames (1.csv and 2.csv) into a 'combined' file.
It doesn't 'print' the content of 1.csv.done or 2.csv.done, or 3.csv, because these files weren't in the original 'list'.
If you run this script multiple times, it will keep adding the contents of files 1.csv and 2.csv to the 'combined' file (only run it once, or delete the 'combined' file before running it again)
I have several files. I need to run a perl script calling all the files in a folder, simultaneously calling one file with extension .pep and another that is a similar file and matches the same name but different extension, .pep.nuc (like Oh01.pep and Oh001.pep.nuc)
I have this script so far, but I am missing something of course.
for file *.pep; do ./script.pl *.pep *.pep.nuc > "${file%.*}.nucleo"; done
This should do it:
for file in *.pep; do
./script.pl "$file" "${file}.nuc" > "${file%.pep}.nucleo";
done
First, my problem:
I have thousands of test files that currently call a function inside of another function. This could be placed anywhere in the file but can only be called once. The structure is as follows:
md = mod.solver(mod,TestFunctionCall());
I need to replace the function call TestFunctionCall() with a string, specifically, I need to remove Call() and replace it with a ' character. I also need to put a ' mark at the beginning of TestFunctionCall(). So essentially I want to do the following:
TestFunctionCall() -> 'TestFunction'
So my final output should look like this:
md = mod.solver(mod,'TestFunction');
The problem is, I have many different names for TestFunction and although they end in Call(), they might have different names (ie. MySolverCall(), HelloWorldCall(), etc.).
Since all the files I want have the name and extension test*.txt, I figured this would be easy enough for find and sed. So I used the following command:
find . -type f -name "test*.txt" -exec sed "s/solver(mod,\(.*\)Call()/solver(md,\'\1\'/g" {} \;
This spits out a ton of text at me with the files I'm editing, and then it does correctly replace FunctionNameCall() with 'FunctionName', however it only does so in about 10 files before deciding to stop. It seems no matter how many different files I call this command on, it will only edit around 10 files before stopping.
How can I make it edit every file with the name test*.txt in the current directory?
I'm using a python script I wrote to take some standard input from ls and load the data in the files described by that path. It looks something like this:
ls -d /path/to/files/* | python read_files.py
The files have a certain name structure based on what data they have in them but are all stored in the same directory. The files I want to use have the name structure A<fileID>_###.txt (where ### is always some 3 digit number). I can accomplish getting only the files that start with A by just changing what I have above slightly to ls -d /path/to/files/A*. HOWEVER, some files have a suffix flag called B (so the file looks like A<fileID>_###B.txt) and I DO NOT want to include those.
So, my question is, is there a way to exclude those files that end in ...B.txt (or a way to only include files that end in a number)? I thought about something to the effect of:
ls -d /path/to/file/R*%d.txt
to only include files that end in a number followed by the file extension, but couldn't find any documentation on anything of the sort.
You could try this : ls A*[^B].txt
With extended globbing.
shopt -s extglob
ls R*!(B).txt
My websites file structure has gotten very messy over the years from uploading random files to test different things out. I have a list of all my files such as this:
file1.html
another.html
otherstuff.php
cool.jpg
whatsthisdo.js
hmmmm.js
Is there any way I can input my list of files via command line and search the contents of all the other files on my website and output a list of the files that aren't mentioned anywhere on my other files?
For example, if cool.jpg and hmmmm.js weren't mentioned in any of my other files then it could output them in a list like this:
cool.jpg
hmmmm.js
And then any of those other files mentioned above aren't listed because they are mentioned somewhere in another file. Note: I don't want it to just automatically delete the unused files, I'll do that manually.
Also, of course I have multiple folders so it will need to search recursively from my current location and output all the unused (unreferenced) files.
I'm thinking command line would be the fastest/easiest way, unless someone knows of another. Thanks in advance for any help that you guys can be!
Yep! This is pretty easy to do with grep. In this case, you would run a command like:
$ for orphan in `cat orphans.txt`; do \
echo "Checking for presence of ${orphan} in present directory..." ;
grep -rl $orphan . ; done
And orphans.txt would look like your list of files above, one file per line. You can add -i to the grep above if you want to grep case-insensitively. And you would want to run that command in /var/www or wherever your distribution keeps its webroots. If, after you see the above "Checking for..." and no matches below, you haven't got any files matching that name.