I have a directory with ~500 files that look like below. I need to copy each unique entry up until the second_ only once and put a .txt in place of the _. I am not quite sure how but have the below as a startThank you :).
cp -u file1.txt "$(cat output.txt)"
file1.txt
xxxx_Noonan14_..........
xxxx_Noonan14_..........
xxxx_Noonan14_..........
aaa_Marfan20_..........
aaa_Marfan20_..........
aaa_Marfan20_..........
bbb_Epilepsy70_..........
bbb_Epilepsy70_..........
bbb_Epilepsy70_..........
Desired output
xxxx_Noonan14.txt
aaa_Marfan20.txt
bbb_Epilepsy70.txt
[user@host ~]$ cat file
xxxx_Noonan14_..........
xxxx_Noonan14_..........
xxxx_Noonan14_..........
aaa_Marfan20_..........
aaa_Marfan20_..........
aaa_Marfan20_..........
bbb_Epilepsy70_..........
bbb_Epilepsy70_..........
bbb_Epilepsy70_..........
[user@host ~]$ sed 's/\(^[^_]*_[^_]*\)_.*/\1.txt/' file | uniq
xxxx_Noonan14.txt
aaa_Marfan20.txt
bbb_Epilepsy70.txt
[user@host ~]$
[akshay@localhost tmp]$ cat file
xxxx_Noonan14_..........
xxxx_Noonan14_..........
xxxx_Noonan14_..........
aaa_Marfan20_..........
aaa_Marfan20_..........
aaa_Marfan20_..........
bbb_Epilepsy70_..........
bbb_Epilepsy70_..........
bbb_Epilepsy70_..........
[akshay@localhost tmp]$ awk -F'_' '{f = $1 FS $2 ".txt"}!x[f]++{print f}' file
xxxx_Noonan14.txt
aaa_Marfan20.txt
bbb_Epilepsy70.txt
I mis-spoke in my original post, there is no file.txt rather ~500 files all listed in a directory. So could the directory be used instead of file.txt
awk -F'_' '{f = $1 FS $2 ".txt"}!x[f]++{print f}' 'N:\path' > output.txt
or
sed 's/\(^[^_]*_[^_]*\)_.*/\1.txt/' 'N:\path' | uniq > output.txt
Thank you :).
Files are just "frozen processes", as the saying goes. The following is based on balajesuri's suggestion:
ls | sed 's/\(^[^_]*_[^_]*\)_.*/\1.txt/' | uniq > output.txt
Before you ask, because i have the suspicion that in fact you want to rename the files listed that way:
ls | sed 's/\(^[^_]*_[^_]*\)_.*/& \1.txt/' |\
while read SRC TGT ; do
mv "$SRC" "$TGT"
done
A similar solution based on Akshay Hedge's solution is equally possible and left as an exercise to the reader.
I hope this helps.
bakunin