Removing part of a file name and appending into a single file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing part of a file name and appending into a single file
# 8  
Old 05-04-2012
Quote:
Originally Posted by varlax
How do i extract the date part of the existing file in the process to use it in the log flog_<date part extracted>. Also i want ot remove the original files after the merge

Use this:
Code:
#!/bin/sh 
filemerge=/path/merge 
newfile=/path/abc_def_xxx.txt. 
# 
ls /path/directory/ | grep abc_def* > $filemerge 
# 
for j in `cat $filemerge` 
do 
cat $j >> $newfile 
echo "adding $filemerge to $newfile"
rm $j
done 
rm $filemerge

---------- Post updated at 03:50 PM ---------- Previous update was at 03:44 PM ----------

Code:
#!/bin/sh 
filemerge=/path/merge # this is an arbitrary file that you are using. Just make up a path and filename
newfile=/path/abc_def_xxx.txt   #This is the new file that you want everything in 
# 
ls /path/directory/ | grep abc_def* > $filemerge     #this is doing an ls and putting all of the filenames 
                                                     #into a file that you will run through
# 
for j in `cat $filemerge`           #This is reading the file one line at a time and doing the following: 
do 
cat $j >> $newfile                  #adding info to newfile...note that it is appending everything to it
echo "adding $filemerge to $newfile"   #just letting you know where it is in process
rm $j                       #deleting old file
done 
rm $filemerge     #deleting file that had list of all filenames

# 9  
Old 05-04-2012
Try this. $1 contains the first parameter to the script
Code:
pat=$1 
suffix=${pat##*.} 
first=${pat%.*}
for file in ${first}_*_*.${suffix}
do
  last=${file##*_}
  cat "$file" >> "${first}_${last}"
done

You can try removing the files by adding && rm "$file"
Code:
cat "$file" >> "${first}_${last}" && rm "$file"

You can write to a log by using echo statements in the loop and
Code:
done >> filemerge.log

Obviously this would need to be thoroughly tested first..



--

( Or, in ultra-compact form just for fun Smilie)
Code:
for f in ${1%.*}_*_*.${1##*.}; do
  cat "$f" >> "${1%.*}_${f##*_}"
done


Last edited by Scrutinizer; 05-04-2012 at 06:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing duplicates on a single "column" (delimited file)

Hello ! I'm quite new to linux but haven't found a script to do this task, unfortunately my knowledge is quite limited on shellscripts... Could you guys help me removing the duplicate lines of a file, based only on a single "column"? For example: M202034357;01/2008;J30RJ021;Ciclo 01... (4 Replies)
Discussion started by: Rufinofr
4 Replies

2. Shell Programming and Scripting

Shell script - Replace just part of a single line in a file.....

Hey guy's.... I new here, But im working on a school project, and I am not really good at programming. In fact, this is the only programming class that I need because programming is not what I am majoring in. But I have everything done in this shell script except for this last part..... ... (9 Replies)
Discussion started by: hxdrummerxc
9 Replies

3. Shell Programming and Scripting

removing part of a file

Right this is quite a long one, I have a script which complies all listed stats files into one file and emails it out, However this has to be run manually and i would like it to run automatically, I have a list of files eg sa17 sa18 sa19 sa20 sa21 one file for each of last weeks... (9 Replies)
Discussion started by: Bdoydie
9 Replies

4. Shell Programming and Scripting

Removing duplicate records in a file based on single column explanation

I was reading this thread. It looks like a simpler way to say this is to only keep uniq lines based on field or column 1. https://www.unix.com/shell-programming-scripting/165717-removing-duplicate-records-file-based-single-column.html Can someone explain this command please? How are there no... (5 Replies)
Discussion started by: cokedude
5 Replies

5. Shell Programming and Scripting

Removing duplicate records in a file based on single column

Hi, I want to remove duplicate records including the first line based on column1. For example inputfile(filer.txt): ------------- 1,3000,5000 1,4000,6000 2,4000,600 2,5000,700 3,60000,4000 4,7000,7777 5,999,8888 expected output: ---------------- 3,60000,4000 4,7000,7777... (5 Replies)
Discussion started by: G.K.K
5 Replies

6. UNIX for Dummies Questions & Answers

appending in a single text file

actually, i have to grep the value of cpu usage from six differrent servers and it has to generated in a single report( in a single text or excel file). i have used the below command for grepping the value. top -n 1 > arun.txt cat arun.txt | grep 'init' | cut -c 49-53 > output1.csv like... (2 Replies)
Discussion started by: arunmanas
2 Replies

7. Shell Programming and Scripting

rename file by removing some part of the file name

I am special requirements to rename file. I have files with names like below: 1_firstname1_lastname1.html 2_firstname2_lastname2.html 3_fistname3_lastname2.html I would like these file to be renamed as below firstname1_lastname1.html firstname2_lastname2.html... (5 Replies)
Discussion started by: McLan
5 Replies

8. UNIX for Dummies Questions & Answers

Need advice! Removing multiple entries in a single file!

Hello, I have a file Test.txt with 9 columns that looks like this: 1g12 A 14 19 2OAY A 326 331 AAAASA 1l7v A 68 73 1l7v A 68 73 AALAIS 1l7v A 68 73 1XVW B 72 77 AALAIS 1l7v A 68 73 1XXU A 65 70 AALAIS 1l7v A 68 73 1XXU B 65 70 AALAIS 1l7v A 68 73 1XXU C 65 70 AALAIS 1l7v A 68 73 1XXU D... (4 Replies)
Discussion started by: InfoSeeker
4 Replies

9. UNIX for Dummies Questions & Answers

replace part of single string in a file

hi! i have a file consisting of the following lines: (BTW, = space) . . . 12ME_T1mapping_flip30bshortf 13DCE_whole_brainbshortf 13DCE_3Dbshortf . . . the list of scans starts at 1 and goes on sometimes up to 60 scans. i would like to change only the lines that contain 'whole' to... (2 Replies)
Discussion started by: nixjennings
2 Replies
Login or Register to Ask a Question