Swop postions of characters in a file name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Swop postions of characters in a file name
# 1  
Old 06-15-2008
Swop postions of characters in a file name

Im hoping someone here can help me with the following please?!

I need to change the position of 3 characters in a set of file names.

I have the follow file name structure c2pMOW051102_winds.nc. I need to change the position of the characters c2p to MOW051102c2p_winds.nc.

Can this be done using the 'sed' command? Or does any one know any other way of doing this?

Thanks for any help!
# 2  
Old 06-15-2008
Sed:
Code:
sed 's/\(.\{3\}\)\(.\{9\}\)\(.*\)/\2\1\3/'


Last edited by danmero; 06-15-2008 at 05:05 PM..
# 3  
Old 06-15-2008
With shell:

Code:
for f in *.nc; do
  t="${f#???}" p="${f%$t}_" h="${t%_*}$p" r="${f#*_}"
  mv "$f" "$h$r"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Outputting characters after a given string and reporting the characters in the row below --sed

I have this fastq file: @M04961:22:000000000-B5VGJ:1:1101:9280:7106 1:N:0:86 GGGGGGGGGGGGCATGAAAACATACAAACCGTCTTTCCAGAAATTGTTCCAAGTATCGGCAACAGCTTTATCAATACCATGAAAAATATCAACCACACCA +test-1 GGGGGGGGGGGGGGGGGCCGGGGGFF,EDFFGEDFG,@DGGCGGEGGG7DCGGGF68CGFFFGGGG@CGDGFFDFEFEFF:30CGAFFDFEFF8CAF;;8... (10 Replies)
Discussion started by: Xterra
10 Replies

2. UNIX for Advanced & Expert Users

Replacing characters in a file

Suppose I have a file which has 1000 columns (5 SHOWN FOR EXAMPLE) two alphabets are separated by a space and then tab A A"\t"C C"\t"G G"\t"0 0"\t"T T A G"\t"C C"\t"G G"\t"A T"\t"0 0 G A"\t"0 0"\t"G C"\t"A A"\t"T C whenever there is a 0 0 in any column, the output should be printed as A... (12 Replies)
Discussion started by: rossi
12 Replies

3. Linux

File conversion and removing special characters from a file in Linux

I have a .CSV file when I check for the special characters in the file using the command cat -vet filename.csv, i get very lengthy lines with "^@", "^I^@" and "^@^M" characters in between each alphabet in all of the records. Using the code below file filename.csv I get the output as I have a... (2 Replies)
Discussion started by: dhruuv369
2 Replies

4. Shell Programming and Scripting

awk script to count characters in file 1 in file 2

I need a scripting AWK to compare 2 files. file 1 and 2 are list of keywords 1 is a b c d 2 is aa aaa b bb ccc d I want the AWK script to give us the number of times every keyword in file 1 occurs in file 2. output should be a 2 (7 Replies)
Discussion started by: anhtt
7 Replies

5. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

6. Shell Programming and Scripting

File contains ^M characters

Hello, I tried with this command,it's working fine,file contains huge data it's not removing the control+M character dos2unix test1.txt test2.txt Please help me,is there any other option. :wall: Thanks, Murali ---------- Post updated at 06:41 AM ---------- Previous update... (6 Replies)
Discussion started by: muralikri
6 Replies

7. Shell Programming and Scripting

Need help on getting the first few characters of a file.

Friends , I need some help in writing a shell script to get the first few characters from a file for all the lines in that file. Here is sample data inthe file. 9 JACOBS 0175 10 VENDOR_0175 11 JACOBS 0175 100 0175 I want the ouput as 9,10,11,100 It doesn't have any fixed... (7 Replies)
Discussion started by: srini02
7 Replies

8. Shell Programming and Scripting

Unix Script file to Append Characters before rows in file.

Hi Experts, I am working on HP-UX. I am new to shell scripting. I would like to have a shell script which will prefix: 1. "H|" before first row of my file and, 2. "T" for all other rows of the file. For Example - File before running the script 20100430|4123451810|218.50|TC 20100430 ... (4 Replies)
Discussion started by: phani333
4 Replies

9. Shell Programming and Scripting

\r characters in the file

hi I copied a file to unix box from windows. When i run this file it throws error on every command written in that file. The reason is that the file contains some \r charecters that came into picture while copying the file. This could be viewed by using od -c command. My question is that... (2 Replies)
Discussion started by: infyanurag
2 Replies

10. UNIX for Dummies Questions & Answers

Rename file based on first 3 characters of data in file

I'm looking to determine if I can use a grep command to read file and rename the file based on the first 3 characters of the data in the file. An example is: Read FileA If the first 3 positions of the data in the file are "ITP", then rename the file as FileA_ITP, else if the first 3... (3 Replies)
Discussion started by: jchappel
3 Replies
Login or Register to Ask a Question