Copy text and create new file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy text and create new file
# 1  
Old 05-16-2015
Copy text and create new file

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 Smilie.

Code:
 cp -u file1.txt "$(cat output.txt)"

file1.txt
Code:
xxxx_Noonan14_..........
xxxx_Noonan14_..........
xxxx_Noonan14_..........
aaa_Marfan20_..........
aaa_Marfan20_..........
aaa_Marfan20_..........
bbb_Epilepsy70_..........
bbb_Epilepsy70_..........
bbb_Epilepsy70_..........

Desired output
Code:
xxxx_Noonan14.txt
aaa_Marfan20.txt
bbb_Epilepsy70.txt


Last edited by cmccabe; 05-17-2015 at 10:39 AM.. Reason: modified code
# 2  
Old 05-18-2015
Code:
[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 ~]$

# 3  
Old 05-18-2015
Code:
[akshay@localhost tmp]$ cat file
xxxx_Noonan14_..........
xxxx_Noonan14_..........
xxxx_Noonan14_..........
aaa_Marfan20_..........
aaa_Marfan20_..........
aaa_Marfan20_..........
bbb_Epilepsy70_..........
bbb_Epilepsy70_..........
bbb_Epilepsy70_..........

Code:
[akshay@localhost tmp]$ awk -F'_' '{f = $1 FS $2 ".txt"}!x[f]++{print f}' file
xxxx_Noonan14.txt
aaa_Marfan20.txt
bbb_Epilepsy70.txt

# 4  
Old 05-18-2015
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

Code:
 awk -F'_' '{f = $1 FS $2 ".txt"}!x[f]++{print f}' 'N:\path' > output.txt

or

Code:
 sed 's/\(^[^_]*_[^_]*\)_.*/\1.txt/' 'N:\path' | uniq > output.txt

Thank you Smilie.
# 5  
Old 05-18-2015
Quote:
Originally Posted by cmccabe
I mis-spoke in my original post, there is no file.txt rather ~500 files all listed in a directory.
Files are just "frozen processes", as the saying goes. The following is based on balajesuri's suggestion:

Code:
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:

Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy the content from txt file and create a html file

I have a txt file with a list of error messages in a xml tag format, and each error message is separated with a identifier(endresult).Need to split that and copy and create a new html file.Error message has some special character. how to escape the special character and insert my data into the... (7 Replies)
Discussion started by: DevAakash
7 Replies

2. Shell Programming and Scripting

Script to create folder, copy file, and send email on success

I am very new to UNIX as well as scripting so please be gentle, haha. I have a Linux client which has a mount point mapped to my /etc folder on a NetApp FAS system. I woudl like to be able to copy the hosts and rc files once a week from the FAS to a different location for backup. When the... (4 Replies)
Discussion started by: minnino
4 Replies

3. Shell Programming and Scripting

Copy data form File A and Create File B

File A I have list of : ABCND1 ABCND2 ABCnd3 ABCnd4 I want file B like below Start+ S Pate=ABCND1 AAlo1 S Pate=ABCND1 Q1234 S Pate=ABCND1,P12345 (7 Replies)
Discussion started by: asavaliya
7 Replies

4. UNIX for Dummies Questions & Answers

Hoe to copy selected strings from file into another text file

Hi Experts, I just want to copy some selected strings from a a file into a new .txt file . I am using below command to find the data now want to copy the search results into another .txt file please help me . find /Path -exec grep -w "filename1|filename1|filename1|" '{}' \;... (2 Replies)
Discussion started by: mumakhij
2 Replies

5. Shell Programming and Scripting

Script to create a text file whose content is the text of another files

Hello everyone, I work under Ubuntu 11.10 (c-shell) I need a script to create a new text file whose content is the text of another text files that are in the directory $DIRMAIL at this moment. I will show you an example: - On the one hand, there is a directory $DIRMAIL where there are... (1 Reply)
Discussion started by: tenteyu
1 Replies

6. Shell Programming and Scripting

How to copy one file into another except some text

I have file input like- abc,"123",""123","123","123","123","123","123","123","123","123","123","123","123", abc,"123","123","123","123","123","123","123","123","123""123",abc etc Input file consists of 1000 records and i want to copy into final file except abc abc is at fixed position... (6 Replies)
Discussion started by: AhmedLakadkutta
6 Replies

7. Shell Programming and Scripting

How to copy text with ed to new file ?

Alright, so in ed i do a subsititution: ed file << 'HERE' s/.*h2.*>\(.*\)<.*/\1/p HERE This gives me my desired line: Errors found while checking this document as XHTML 1.0 Transitional! But how would i put this line in a temporary file ? (4 Replies)
Discussion started by: Bertieboy7
4 Replies

8. Shell Programming and Scripting

Create multiple text file from a single text file on AIX

Hi I need to create multiple text files from onc text file on AIX. The data of text files is as below: ********************************************** ********************************************** DBVERIFY: Release 10.2.0.4.0 - Production on Tue Nov 10 13:45:42 2009 Copyright (c) 1982,... (11 Replies)
Discussion started by: lodhi1978
11 Replies

9. UNIX for Dummies Questions & Answers

Shell script to search for text in a file and copy file

Compete noob question.... I need a script to search through a directory and find files containing text string abcde1234 for example and then copy that file with that text string to another directory help please :eek: (9 Replies)
Discussion started by: imeadows
9 Replies

10. UNIX for Advanced & Expert Users

How to copy a string to a text file

I am using the following command to email a tex file as an attachment- cat mailtext.txt | elm -s "Subject" emailAddr where content of mailtext.txt is - "Body of email" This will attach foo.txt with the email. My problem is that the file foo.txt is ceated dynamically everytime with a... (5 Replies)
Discussion started by: hpuxlxboy
5 Replies
Login or Register to Ask a Question