How to add a new line (\n) between each file being concatenated - bash

How should I modify this line to add a new line (\n) between each file being concatenated?
find /disk/data/source/* -name '*.xml' -exec cat {} \; > /home/userid/merged-file.xml

find accepts multiple -execs in one command. For example:
find /disk/data/source/* -name '*.xml' -exec cat {} \; -exec echo "" \; > /home/userid/merged-file.xml

Using awk instead of cat:
find /disk/data/source/* -name '*.xml' \
-exec awk 'NR!=FNR&&FNR==1{print ""} 1' {} + > /home/userid/merged-file.xml

Since you are already using find, try this:
find /disk/data/source/* -name '*.xml' -exec cat {} \; -exec echo \; > /home/userid/merged-file.xml
And if you want to get rid of the extra newline at the end, you can add | head -n-1.

Related

Append line break to every cat in pipe

I have the following pipeline:
find /my/place -name 'test_*_blub' | xargs cat
While this works fine, I also want to have all file content terminated by a line break (\n).
Could not yet figure out how to append the newline.
To print a linebreak \n after each file content - use one of the following approaches:
1) running shell commands
find /my/place -name 'test_*_blub' | xargs -I % sh -c 'cat %; echo "";'
sh -c 'cat %; echo "";' - multiple commands executed one-by-one
2) with -exec action:
find /my/place -name 'test_*_blub' -exec cat {} \; -exec echo "" \;
3) with -printf action:
find /my/place -name 'test_*_blub' -exec cat {} \; -printf "\n"
Figured out an easy way:
find /my/place -name 'test_*_blub' | xargs cat | xargs -I '{}' echo '{}'

How do I recursively search and replace directory paths in terminal Mac?

I've looked at the following other threads which recommend a particular 'sed' command:
https://superuser.com/questions/428493/how-can-i-do-a-recursive-find-and-replace-from-the-command-line
Find/sed: How can I recursively search/replace a string in files but only for lines that match a particular regexp
Recursive search and replace in text files on Mac and Linux
perl -pi -w -e 's/SEARCH_FOR/REPLACE_WITH/g;' *.txt
And
find . -type f -name '*.txt' -exec sed -i '' s/this/that/ {} +
I am trying to replace this sample below that occurs in multiple files in the directory:
<script src="http://localhost/massignition/wp-content/themes/twentythirteen/js/html5.js"></script>
with the following (substituting localhost/massignition with www.site.com)
I type the following:
find . -type f -name '*.txt' -exec sed -i '' s%localhost/massignition%www.site.com%/ {} +
But nothing changes.
Remove / which is present in before curly braces in your command, that is
find . -type f -name '*.txt' -exec sed -i '' s%localhost/massignition%www.site.com% {} +
instead of
find . -type f -name '*.txt' -exec sed -i '' s%localhost/massignition%www.site.com%/ {} +

"find -exec print" does not work

> find . -type f -exec print {} \;
find: cannot execute print:: No such file or directory
> find . -type f -exec echo {} \;
f1.txt
f2.txt
...
Why "find -exec print" does not work?
Shell - ksh.
I think print is a shell builtin, not an executable.

How do I use pipe in a specific exec of a find (in bash)

I have to construct a csv with the output of a shell command; the csv file must contain for each row some information get by the output of stat command and in the last column the md5sum (only the sum without the filename)
I tried some command like:
find . -exec stat --printf='"%a";"%F"' {} \; -exec sh -c "md5sum $1 | cut -b-32" {} {} \;
but this block and ask me for input
and this,
find . -exec stat --printf='"%a";"%F";' {} \; -exec md5sum {} | cut -b-32 \;
but in this case the pipe doesn't work.
How can I solve it?
I think you have {} and ; misplaced. This one is working fine for me on Linux:
find . -exec stat --printf='"%a";"%F";' {} \; -exec sh -c "md5sum {} | cut -b-32" \;
Update 1
You can combine all in one -exec option like this also:
find . -exec sh -c "stat --printf='\"%a\";\"%F\";' {} && md5sum {} | cut -b-32" \;

find -exec with multiple commands

I am trying to use find -exec with multiple commands without any success. Does anybody know if commands such as the following are possible?
find *.txt -exec echo "$(tail -1 '{}'),$(ls '{}')" \;
Basically, I am trying to print the last line of each txt file in the current directory and print at the end of the line, a comma followed by the filename.
find accepts multiple -exec portions to the command. For example:
find . -name "*.txt" -exec echo {} \; -exec grep banana {} \;
Note that in this case the second command will only run if the first one returns successfully, as mentioned by #Caleb. If you want both commands to run regardless of their success or failure, you could use this construct:
find . -name "*.txt" \( -exec echo {} \; -o -exec true \; \) -exec grep banana {} \;
find . -type d -exec sh -c "echo -n {}; echo -n ' x '; echo {}" \;
One of the following:
find *.txt -exec awk 'END {print $0 "," FILENAME}' {} \;
find *.txt -exec sh -c 'echo "$(tail -n 1 "$1"),$1"' _ {} \;
find *.txt -exec sh -c 'echo "$(sed -n "\$p" "$1"),$1"' _ {} \;
Another way is like this:
multiple_cmd() {
tail -n1 $1;
ls $1
};
export -f multiple_cmd;
find *.txt -exec bash -c 'multiple_cmd "$0"' {} \;
in one line
multiple_cmd() { tail -1 $1; ls $1 }; export -f multiple_cmd; find *.txt -exec bash -c 'multiple_cmd "$0"' {} \;
"multiple_cmd()" - is a function
"export -f multiple_cmd" - will export it so any other subshell can see it
"find *.txt -exec bash -c 'multiple_cmd "$0"' {} \;" - find that will execute the function on your example
In this way multiple_cmd can be as long and as complex, as you need.
Hope this helps.
There's an easier way:
find ... | while read -r file; do
echo "look at my $file, my $file is amazing";
done
Alternatively:
while read -r file; do
echo "look at my $file, my $file is amazing";
done <<< "$(find ...)"
Extending #Tinker's answer,
In my case, I needed to make a command | command | command inside the -exec to print both the filename and the found text in files containing a certain text.
I was able to do it with:
find . -name config -type f \( -exec grep "bitbucket" {} \; -a -exec echo {} \; \)
the result is:
url = git#bitbucket.org:a/a.git
./a/.git/config
url = git#bitbucket.org:b/b.git
./b/.git/config
url = git#bitbucket.org:c/c.git
./c/.git/config
I don't know if you can do this with find, but an alternate solution would be to create a shell script and to run this with find.
lastline.sh:
echo $(tail -1 $1),$1
Make the script executable
chmod +x lastline.sh
Use find:
find . -name "*.txt" -exec ./lastline.sh {} \;
Thanks to Camilo Martin, I was able to answer a related question:
What I wanted to do was
find ... -exec zcat {} | wc -l \;
which didn't work. However,
find ... | while read -r file; do echo "$file: `zcat $file | wc -l`"; done
does work, so thank you!
1st answer of Denis is the answer to resolve the trouble. But in fact it is no more a find with several commands in only one exec like the title suggest. To answer the one exec with several commands thing we will have to look for something else to resolv. Here is a example:
Keep last 10000 lines of .log files which has been modified in the last 7 days using 1 exec command using severals {} references
1) see what the command will do on which files:
find / -name "*.log" -a -type f -a -mtime -7 -exec sh -c "echo tail -10000 {} \> fictmp; echo cat fictmp \> {} " \;
2) Do it: (note no more "\>" but only ">" this is wanted)
find / -name "*.log" -a -type f -a -mtime -7 -exec sh -c "tail -10000 {} > fictmp; cat fictmp > {} ; rm fictmp" \;
I usually embed the find in a small for loop one liner, where the find is executed in a subcommand with $().
Your command would look like this then:
for f in $(find *.txt); do echo "$(tail -1 $f), $(ls $f)"; done
The good thing is that instead of {} you just use $f and instead of the -exec … you write all your commands between do and ; done.
Not sure what you actually want to do, but maybe something like this?
for f in $(find *.txt); do echo $f; tail -1 $f; ls -l $f; echo; done
should use xargs :)
find *.txt -type f -exec tail -1 {} \; | xargs -ICONSTANT echo $(pwd),CONSTANT
another one (working on osx)
find *.txt -type f -exec echo ,$(PWD) {} + -exec tail -1 {} + | tr ' ' '/'
A find+xargs answer.
The example below finds all .html files and creates a copy with the .BAK extension appended (e.g. 1.html > 1.html.BAK).
Single command with multiple placeholders
find . -iname "*.html" -print0 | xargs -0 -I {} cp -- "{}" "{}.BAK"
Multiple commands with multiple placeholders
find . -iname "*.html" -print0 | xargs -0 -I {} echo "cp -- {} {}.BAK ; echo {} >> /tmp/log.txt" | sh
# if you need to do anything bash-specific then pipe to bash instead of sh
This command will also work with files that start with a hyphen or contain spaces such as -my file.html thanks to parameter quoting and the -- after cp which signals to cp the end of parameters and the beginning of the actual file names.
-print0 pipes the results with null-byte terminators.
for xargs the -I {} parameter defines {} as the placeholder; you can use whichever placeholder you like; -0 indicates that input items are null-separated.
I found this solution (maybe it is already said in a comment, but I could not find any answer with this)
you can execute MULTIPLE COMMANDS in a row using "bash -c"
find . <SOMETHING> -exec bash -c "EXECUTE 1 && EXECUTE 2 ; EXECUTE 3" \;
in your case
find . -name "*.txt" -exec bash -c "tail -1 '{}' && ls '{}'" \;
i tested it with a test file:
[gek#tuffoserver tmp]$ ls *.txt
casualfile.txt
[gek#tuffoserver tmp]$ find . -name "*.txt" -exec bash -c "tail -1 '{}' && ls '{}'" \;
testonline1=some TEXT
./casualfile.txt
Here is my bash script that you can use to find multiple files and then process them all using a command.
Example of usage. This command applies a file linux command to each found file:
./finder.sh file fb2 txt
Finder script:
# Find files and process them using an external command.
# Usage:
# ./finder.sh ./processing_script.sh txt fb2 fb2.zip doc docx
counter=0
find_results=()
for ext in "${#:2}"
do
# #see https://stackoverflow.com/a/54561526/10452175
readarray -d '' ext_results < <(find . -type f -name "*.${ext}" -print0)
for file in "${ext_results[#]}"
do
counter=$((counter+1))
find_results+=("${file}")
echo ${counter}") ${file}"
done
done
countOfResults=$((counter))
echo -e "Found ${countOfResults} files.\n"
echo "Processing..."
counter=0
for file in "${find_results[#]}"
do
counter=$((counter+1))
echo -n ${counter}"/${countOfResults}) "
eval "$1 '${file}'"
done
echo "All files have been processed."

Resources