shell


cat files together with newline in shellscript


I am cating files together recursively in a directory, however, some files are missing a newline after the last line, so this creates a problem. How do I append a newline to each file in the process? here's the shellscript
find $1 -type f |xargs cat > test.csv
I realize this is a fairly old question, but I had the same problem. None of the answers above seemed to work with files with whitespace in them, because of the usage of backticks and /usr/bin/find. Doing so would require me to change $IFS which is absolutely not necessary unless you only need to use files.
This is my take on it:
for i in *; do cat "$i" && echo ""; done
You can use a loop:
find $1 -type f | while IFS= read -r file
do
cat "$file"
echo
done > test.csv
Use a for-loop:
for file in $(find .); do cat $file >> huge_file; echo " " >> huge_file; done
find $1 -type f |xargs awk 'FNR==1 && NR>1 {print ""} {print}'
or
find $1 -type f |xargs awk 'ENDFILE {print ""} {print}'
If you wanted to add a newline for any file that does not have a newline on its own as the last line, you can do the following:
for f in `find $1 -type f`; do sed -E '$s/(.*\S+.*)/\1\n/' $f; done
EDIT: My first answer used xargs, but that didn't provide the expected result, so fell back on a loop.

Related Links

shell: how to pagination file with code highlight?
Read and print Line between two tags
Whats the difference between $ExecutionContext.SessionState.Path.CurrentLocation and $pwd in Powershell?
Pro*C unable to read parameter passing from korn shell script
No environment variables are set after sourcing. Vagrant provisioning with Ansible and shell
How to set PATH of SGE in cronjob
Unix terminal, “cd ..” for a specific number of directories
Sonar.sh script is getting stopped immediately in Centos Docker Continer
How to calculate the difference in date time on Sun OS?
How to insert tab in awk system() command string
shell script - AWS s3 bucket, find last added file
Shell script to create loop for command
Run plist on wake
Error while using sh script
how to add a condition before attaching a file in a shell script in unix?
what is the maximum length of the record in a unix file?

Categories

HOME
beautifulsoup
azure-stream-analytics
facebook-oauth
developer-tools
ggplot2
jmeter
nunit
yahoo-finance
hessian
sublimetext
pdfbox
settings
special-characters
computer-science
minecraft-forge
project-intu
code-generation
symbol
esoteric-languages
symfony-2.8
google-picker
foreign-keys
spring-async
angular-routing
php-mysqlidb
short-url
expressionengine3
surveymonkey
android-things
quantlib
browser-sync
magento-1.8
corenlp-server
cognos-bi
git-squash
installscript
python-import
yowsup
roblox
hivemq
xcode7.3
flatmap
android-augmented-reality
tcc
xmldocument
vaadin-charts
rfc3339
magento-2.0.7
clarifai
spring-integration-sftp
mschart
dbcontext
colorbar
otp
pocketsphinx-android
vcloud-director-rest-api
usart
cudd
autosar
actor-platform
flex4.6
nikeplus-api
x-tag
wso2developerstudio
tooleap
lean
jtwig
cycle2
httpruntime.cache
piracy-prevention
user-management
dllimport
zen-cart
viola-jones
avahi
execve
crowd
big-ip
punycode
formview
personalization
grails-domain-class
file-not-found
vbe
getopt
sbcl
spring-3
breakout
nebula
http-status-code-410
os.system
adobe-media-server
subproject
josql
azman
predicatebuilder
flex-mobile
user-tracking
winmain
google-ajax-api
appledoc
presentation-layer
luajava
metaweblog
objective-j
vs-android
qtabbar
interop-domino
soappy
netbeans6.1

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App