I am using this code to combine all text files, however it is putting the name of the files with their extension in the final file, I would like to know how to clear this:
Find /v "" *.txt> "Combined.txt"
The file text:
---------- 001.TXT
abc
---------- 002.TXT
blue
123
---------- 003.TXT
abc
---------- 004.TXT
yellow
123
---------- 005.TXT
abc
---------- COMBINED.TXT
Try this:
Find/V "" *.txt|Find/V "---------- ">"Combined.txt"
…it should work fine unless you have a text file name containing ten contiguous dashes followed by a space, (which is much less likely than not having a filename containing TXT).
When I provided you with the Find option yesterday, I provided it as an additional option which also should prepend files with their names. Remember also that I explained that there were options too I you didn't want to include Combined.txt in the results.
You can of course just change the extension then rename it thus:
Find/V "" *.txt|Find/V "---------- ">"Combined.log"&&Ren "Combined.log" "Combined.txt"
I solve my problem with this code:
Find /v "" *.txt> "Combined.txt"
Type Combined.txt | findstr /v TXT > Output.txt
del Combined.txt
ren Output.txt Combined.txt
First line combine all text files.
Second line Search and Delete lines containg the string "TXT" and create new Output.txt file.
Third line delete the Combined.txt file.
Fourth Rename Output.txt to Combined.txt
The result looks like this:
abc
blue
123
abc
yellow
123
abc
It's all :)
del combined.txt
copy *.txt >combined.tmp
ren combined.tmp combined.txt
Remove the first line to include the current "combined.txt" into the final version.
Related
I have 336 txt files and each txt file has 4 columns. I need help to find string that are common or matched in a column 2 (Gene) in all txt files and extract that information in new txt file.
For example: how many times “kdpDE beta” present and if it is present then print ‘1’ in the next column of output txt file if “kdpDE beta” is absent then print ‘0’.
Thank you for your help.
File_1.txt
Name Gene Family Class
KB2908 kdpE beta aminoglycoside lactamase
KB2908 ugd peptide transferase
File_2.txt
Name Gene Family Class
KB2909 kdpE beta aminoglycoside lactamase
KB2909 ugd peptide transferase
KB2909 PmrF macrolide phosphotransferase
You can use grep with wc to get a count of a certain string within a file. You can loop through it with a script to do this for every file in a directory. The following will loop through the directory, count the number of times <search term> appears in each file, and output it to a file called output.txt.
for FILE in *; do
echo $FILE >> output.txt
grep -o -i '<search term>' $FILE | wc -l >> output.txt
echo >> output.txt
done
I have 500 wave files in a folder ABC which are named like
F1001
F1002
F1003
...
F1100
F2001
F2002
...
F2100
M3001
M3002
...
M3100
M4001
M4002
...
M4100
all with extension .wav.
Also I have a text file which contains 3 digit numbers like
001
003
098
034 .... (200 in total).
I want to select wave files from the folder ABC whose names end with these 3 digits.
Expecting MATLAB or bash script solutions.
I read this:
Copy or move files to another directory based on partial names in a text file. But I don't know how to use it for me.
for Matlab
1) Get all the file names in the folder using functions dir or rdir.
2) Using for loop go through every filename and add the last 3 digits of every filename to an array (array A). You will need str2num() here
3) Parse all 3 digit numbers to an array (array B)
4) Using function ismember(B, A) find which elements of B are contained in A
5) Load corresponding filenames
find . -name "*.wav" | grep -f <(awk '{print $0 ".wav"}' file)
grep -f will use the patterns stored in file, one per line, and look for them in your find result. But you want the three numbers to be at the end, so in the above command the last awk statement will provide a modified file with ".wav" appended at each line. So for the line 001, "0001.wav" will match but any file 0010.wav will not.
see: process substitution syntax and grep --help
function wavelist()
wavefiles=dir('*.wav'); % loaded wave files
myfolder='/home/adspr/Desktop/exps_sree/waves/selectedfiles'; %Output folder to store files
for i=1: numel(wavefiles) %for each wave file
filename=wavefiles(i).name;
[~,name,~] = fileparts(filename); % found the name of file without extension
a=name(end-2:end); %get the last 3 digits in the file name
fileID = fopen('nameslist','r');
while ~feof(fileID)
b=fgetl(fileID); % get each line in the list file
if strcmp(a,b) % compare
movefile(filename,myfolder); % moved to otput folder
end
end
fclose(fileID);
end
end
I don't think this is a simple answer, thats why I asked here. Anyway, my problem solved, thats why I posted this as an answer.
Thank you all.
Suppose the input file file.txt is
abc/def/ghi/jkl/mno
pqr/st/u/vwxy/z
bla/123/45678/9
How to split the lines based on the character '/' and write the specified columns (here it is second and fourth) to another file so that the file should look like
def jkl
st vwxy
123 9
You can use perl, for example:
cat file.txt | perl -ne 'chomp(#cols = split("/", $_)); print "#cols[1, 3]\n";' > output
I'd like to add a specific line to a file in a specific line number (second) without over-writing the data which is already there.
I've tried this:
sed -i '2i - jstat' FILE
But this just over-writes the second line with "- jstat".
Instead, I want to add a new second line and push the next line to be number 3.
Let's say the file looks like that:
[root#puppet roles]# head -5 !$
head -5 buncher.yaml
classes:
- workspace
- fstab
- role_specific
I'd like to add a new module on the second line and I want the "workspace" module to become the third line.
More than that, I'd like the new line to start with a tab size of 2 chars and then "- jstat" as in: "TabTab - jstat", how can it be done?
Well to insert a line after before line number x, you can use the below sed command:
sed 'x i\LINE_TO_ADD' filename > temp_file
mv temp_file filename
Here is the example for adding <TAB><TAB>Hello How are you! to the tempfile whose content are:
123
234
345
To add before line number 2 I wrote the following sed command:
sed '2 i\\t\tHello How are you!' tempfile > temp_file
mv temp_file tempfile
Final content of tempfile are:
123
<TAB><TAB>Hello How are you!
234
345
For more detail, try referring this Sed tutorial
I need your help in unix.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for &abc and &ccc. now i have to substitute the value of &abc and &ccc in the place of them as shown in the output file.
Input File
go to &abc=ddd;
if file found &ccc=10;
no the value name is &abc;
and the age is &ccc;
Output:
go to &abc=ddd;
if file found &ccc=10;
now the value name is ddd;
and the age is 10;
Try using sed.
#!/bin/bash
# The input file is a command line argument.
input_file="${1}"
# The map of variables to their values
declare -A value_map=( [abc]=ddd [ccc]=10 )
# Loop over the keys in our map.
for variable in "${!value_map[#]}" ; do
echo "Replacing ${variable} with ${value_map[${variable}]} in ${input_file}..."
sed -i "s|${variable}|${value_map[${variable}]}|g" "${input_file}"
done
This simple bash script will replace abc with ddd and ccc with 10 in the given file. Here is an example of it working on a simple file:
$ cat file.txt
so boo aaa abc
duh
abc
ccc
abcccc
hmm
$ ./replace.sh file.txt
Replacing abc with ddd in file.txt...
Replacing ccc with 10 in file.txt...
$ cat file.txt
so boo aaa ddd
duh
ddd
10
ddd10
hmm