I need a script that combines files with the same name. These files are on a windows directory but the PC has Cygwin so i have a limited unix command set.
What I've got;
WebData_9_2007-09-20.txt
WebData_9_2007-09-20.txt
WebData_9_2007-09-21.txt
WebData_9_2007-09-20.txt
WebData_9_2007-09-22.txt
WebData_9_2007-09-22.txt
WebData_9_2007-09-22.txt
WebData_9_2007-09-25.txt
What i need;
WebData_9_2007-09-20.txt
WebData_9_2007-09-21.txt
WebData_9_2007-09-20.txt
WebData_9_2007-09-22.txt
WebData_9_2007-09-25.txt
I Appreciate any help.
How do you have 2 files with the same name?
Sorry, I left this out...
The number in _9_ position can change. Someone talked about an awk script they found but I'm still researching.
Ok this is just quick and I am sure that someone could make this a lot cleaner anyway try this script just change it for each _?_ you have.
All files which end in 20.txt have the word Hi in them.
# ls -latr
-rw-r--r-- 1 root root 0 Jan 10 20:14 WebData_9_2007-09-21.txt
-rw-r--r-- 1 root root 0 Jan 10 20:14 WebData_7_2007-09-21.txt
-rw-r--r-- 1 root root 0 Jan 10 20:14 WebData_8_2007-09-21.txt
-rw-r--r-- 1 root root 0 Jan 10 20:14 WebData_6_2007-09-21.txt
-rw-r--r-- 1 root root 3 Jan 10 20:16 WebData_6_2007-09-20.txt
-rw-r--r-- 1 root root 3 Jan 10 20:16 WebData_7_2007-09-20.txt
-rw-r--r-- 1 root root 3 Jan 10 20:16 WebData_8_2007-09-20.txt
-rw-r--r-- 1 root root 3 Jan 10 20:16 WebData_9_2007-09-20.txt
Script
--------------------- CUT ---------------------------------
#!/bin/sh
#
# combo for the 20.txt
#
x="20.txt"
y=` ls -la |grep "20.txt" |awk '{print $9 }'|cut -f1 -d ''|head -1`
z=` ls -la |grep "20.txt" |awk '{print $9 }'|cut -f3 -d ''|head -1`
#
for i in `ls -la |grep $x |awk '{print $9 }'`
do
cat $i >> $y"_all_"$z
done
--------------------- CUT ---------------------------------
Make script executable chmod 755 whatever
Run script from dir with files
./combo.sh
# ls -la
total 72
drwxr-xr-x 2 root root 4096 Jan 10 20:38 .
drwxr-xr-x 9 root root 4096 Jan 10 20:38 ..
-rw-r--r-- 1 root root 3 Jan 10 20:16 WebData_6_2007-09-20.txt
-rw-r--r-- 1 root root 0 Jan 10 20:14 WebData_6_2007-09-21.txt
-rw-r--r-- 1 root root 3 Jan 10 20:16 WebData_7_2007-09-20.txt
-rw-r--r-- 1 root root 0 Jan 10 20:14 WebData_7_2007-09-21.txt
-rw-r--r-- 1 root root 3 Jan 10 20:16 WebData_8_2007-09-20.txt
-rw-r--r-- 1 root root 0 Jan 10 20:14 WebData_8_2007-09-21.txt
-rw-r--r-- 1 root root 3 Jan 10 20:16 WebData_9_2007-09-20.txt
-rw-r--r-- 1 root root 0 Jan 10 20:14 WebData_9_2007-09-21.txt
-rw-r--r-- 1 root root 12 Jan 10 20:38 WebData_all_2007-09-20.txt
cat new file it will have all text from other files.
cat WebData_all_2007-09-20.txt
Hi
Hi
Hi
Hi
You will need to add a clean up line to remove the other files but this should get you started.
This was done on a fedora linux box will be diff under cygwin...
Good luck...
[quote="flatopokey,post:4,topic:181313"]
...
y=` ls -la |grep "20.txt" |awk '{print $9 }'|cut -f1 -d '_'|head -1`
...
generally, this is can be stripped down to
ls -a *20.txt | cut -f1 -d '_'|head -1
since we are not doing anything with the other columns of the ls output except the filename. ( However, also note that the assumption is all filenames are without spaces)
better to use while loop
ls -a *20.txt | while read files
do
...
done
however, since the purpose is to combine files with 20.txt in the filename, you can just use
cat *20.txt > outputfile