LINUX - How to add a prefix to a set of files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers LINUX - How to add a prefix to a set of files
# 1  
Old 07-30-2012
LINUX - How to add a prefix to a set of files

Hi All,

I have a file (lets say filenames.txt) which contains a list of file names.

Quote:
ie cat filenames.txt

2012_a.txt
2012_b.txt
2012_c.txt
2012_d.txt etc
Is there a way I can append a prefix to the filenames as below,

Quote:
2012_bus_a.txt
2012_bus_b.txt
2012_bus_c.txt
2012_bus_d.txt etc
Thanks much for your help
Freddie
# 2  
Old 07-30-2012
Like this?

Code:
$ cat sample19.txt
2012_a.txt
2012_b.txt
2012_c.txt
2012_d.txt etc

$ awk -F"_" '{print $1"_bus_"$2}' <sample19.txt
2012_bus_a.txt
2012_bus_b.txt
2012_bus_c.txt
2012_bus_d.txt etc

$

# 3  
Old 07-30-2012
Hi,

Try this one,
Code:
awk 'BEGIN{FS=OFS="_";}{$2="bus_" $2;}1' filenames.txt
sed 's/_/_bus_/' filenames.txt

Cheers,
Ranga:-)
# 4  
Old 07-30-2012
Code:
perl -i.ORIG -pe 's/(\d{4}\_)(\w\.\w+)/$1bus_$2/g' filenames.txt

2012_bus_a.txt
2012_bus_b.txt
2012_bus_c.txt
2012_bus_d.txt etc

This User Gave Thanks to in2nix4life For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to add unique prefix to extracted zip folder

In the bash below in each .zip there is a folder to be extracted Variants that I am trying to make unique by adding the prefix, before the _ from the .zip. The script does execute, but the prefix is not added to the extracted folder. Rather the Variants folder is added to each file within it. Thank... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

Need to add prefix using sed or awk from cat the file

I need the use sed or AWK using cat the file Node1 TDEV RW 1035788 TDEV RW 1035788 Server1 TDEV RW 69053 Server2 TDEV RW 69053 TDEV RW 103579 Server3 TDEV RW 69053 server4 RDF1+TDEV RW 69053 RDF1+TDEV RW 517894 RDF1+TDEV RW 621473 server6 TDEV RW 34526 TDEV RW 34526 (22 Replies)
Discussion started by: ranjancom2000
22 Replies

3. Shell Programming and Scripting

Extract Uniq prefix from a start and end prefix

Dear All, assume i have a file with content: <Start>6000</Start> <Stop>7599</Stop> the output is: 6000 7000 7100 7200 7300 7400 7599 how should we use any awk, sed, perl can do this task, means to extract the uniq prefixes from the start and stop prefix. Thanks Jimmy (3 Replies)
Discussion started by: jimmy_y
3 Replies

4. UNIX for Dummies Questions & Answers

Add foldername as prefix to files

Hi there, I am looping through folders in order to rename the files in the current folder. So, given me being a newbie here, I would do that with a for-loop. The renaming per se should be like this: I want to add the folder-name as a prefix to the files in the folder in question. For instance,... (6 Replies)
Discussion started by: jahndavik
6 Replies

5. Shell Programming and Scripting

How to add a line to the end of a set of files without using sed command?

I understand that the SED command reads all the lines in the file before adding a required line to the end of the file. Is there another command that adds a line to the end of files without reading the entire file.... SED is increasing the processing time as the number of lines in each of the... (1 Reply)
Discussion started by: Kanch
1 Replies

6. Shell Programming and Scripting

Finding compound words from a set of files from another set of files

Hi All, I am completely stuck here. I have a set of files (with names A.txt, B.txt until L.txt) which contain words like these: computer random access memory computer networking mouse terminal windows All the files from A.txt to L.txt have the same format i.e. complete words in... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

7. Shell Programming and Scripting

Help with script to add two zeros to certain lines in a set of files?

Hello... I should be better with scripting but I am not so turning here for some help. I did search quite a bit using google and didn't find anything meeting my needs for this. What am after here is a script (in a redhat linux env) that will read in a small series of files (netbackup vault... (6 Replies)
Discussion started by: abg1969
6 Replies

8. Shell Programming and Scripting

How to add date as a prefix for each outputline

What is the best way of printing date-time at the beginning of each output line? DTTIME=$(date +%Y%m%d%H%M%S) myfile.log 0.0 5.2 0.0 41.6 0.1 0.8 15.0 160.2 8 40 d0 0.0 0.8 0.0 6.4 0.0 0.0 13.7 53.2 1 4 d2 3.3 109.4 16.9 721.5 0.9 7.0 ... (5 Replies)
Discussion started by: kchinnam
5 Replies

9. Shell Programming and Scripting

Need awk script to add a prefix to each line in file

Hello , I have file with below content : '165567885', '165568443', '165568805', I need an awk script that would add a prefix zero after first ' . Like '0165567885', '0165568443', '0165568805', Please help. Thanks in advance. (5 Replies)
Discussion started by: rmv
5 Replies

10. Shell Programming and Scripting

Help removing the prefix of more than 10,000 files

I have more than 10,000 files that all start with "raw_ddd_04_*". How can I remove the prefix on these files in a single command? (8 Replies)
Discussion started by: bbbngowc
8 Replies
Login or Register to Ask a Question