File name change with sed

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers File name change with sed
# 1  
Old 12-08-2017
File name change with sed

I have a bunch of text files like this:
Code:
Sample_S1_L001_R1.txt
Sample_S10_L001_R1.txt
Sample_S11_L001_R1.txt

I am using the following script to add a 0 to those files with a single digit after the S:
Code:
ls *.txt | sed 's/\(.*_S\)\([1-9]_.*\)/mv & \10\2/' | sh

And then the following script to move and replace the SXX to the beginning of the file name:
Code:
ls *.txt | sed 's/\(.*_\)\(S\)\([0-9]*_\)\(.*\)/mv & MID-\3\1\4/' | sh

I am actually generating the desired output:
Code:
MID-01_Sample_L001_R1.txt
MID-10_Sample_L001_R1.txt
MID-11_Sample_L001_R1.txt

I just think that should be a way to modify my second script so I can accomplish the same task
Any help will be greatly appreciated
# 2  
Old 12-08-2017
Hmmm - I wildly guess you want to
- correct the first script to run error free
- combine the two into one single script
?

EDIT: Try
Code:
ls *.txt | sed -r 'h; s/(.*_S)([1-9]_.*)/\10\2/; s/^(.*_)S([0-9]{2}_)/ MID-\2\1/; H; g; s/\n//; s/^/mv /'

and pipe through sh if happy with what you get.

Last edited by RudiC; 12-08-2017 at 04:05 PM..
# 3  
Old 12-08-2017
Another solution.
The substitution for the two digit is straight forward;
a second substitution cares about the one digit.
Code:
ls *.txt | sed '
s/\(.*_\)S\([0-9][0-9]_\)\(.*\)/mv & MID-\2\1\3/
s/\(.*_\)S\([0-9]_\)\(.*\)/mv & MID-0\2\1\3/
'

You can put a t command after the first substitution, then, if successful, the second substitution will be skipped.
Code:
ls *.txt | sed '
s/\(.*_\)S\([0-9][0-9]_\)\(.*\)/mv & MID-\2\1\3/
t
s/\(.*_\)S\([0-9]_\)\(.*\)/mv & MID-0\2\1\3/
'

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 12-08-2017
Slightly shorter:
Code:
ls *.txt | sed -r 's/^(.*_)S([0-9]{1,2}_)(.*)/mv & MID-0\2\1\3/; s/-0([0-9]{2})/-\1/'

This User Gave Thanks to RudiC For This Post:
# 5  
Old 12-08-2017
Rudy
Thanks a TON! Would you mind helping me understand what your code does?
I guess I do not understand all that well what the script is doing
Thanks once again!

************************************

Never mind! I got it

Last edited by Xterra; 12-08-2017 at 04:56 PM.. Reason: I got it!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using sed to change file into an awk script

Hi I want to use sed to change a text files input into an awk script. For example if the input says " chord -- english " I want to change this using sed 's/pattern 1 /pattern 2 /'g filename but I don't understand how to use part of the pattern 1 to input that into pattern 2 . Like after... (10 Replies)
Discussion started by: enforcer
10 Replies

2. Shell Programming and Scripting

sed command not working for me to change text in a file

UNIX gurus I need your help with the following (The server is an AIX box). I have a text file with the following information: ******************************************************** SOME LINES case :WORD1 SOME LINES :WORD2 SOME LINES :WORD3 SOME LINES esac SOME LINES... (7 Replies)
Discussion started by: curiousmal
7 Replies

3. Shell Programming and Scripting

Datestamp format 2nd change in csv file (awk or sed)

I have a csv file formatted like this: 2014-08-21 18:06:26,A,B,12345,123,C,1232,26/08/14 18:07and I'm trying to change it to MM/DD/YYYY HH:MM for both occurances. I have got this: awk -F, 'NR <=1 {print;next}{"date +%d/%m/%Y\" \"%H:%m -d\""$1 "\""| getline dte;$1=dte}1' OFS="," test.csvThis... (6 Replies)
Discussion started by: say170
6 Replies

4. Shell Programming and Scripting

Trying to take file numbers from a file, pass them to sed to change strings in corresponding lines

I have a bunch of file numbers in the file 'test': I'm trying the above command to change all the instances of "H" to "Na+" in the file testsds.pdb at the line numbers indicated in the file 'test'. I've tried the following and various similar alternatives but nothing is working: cat test |... (3 Replies)
Discussion started by: crunchgargoyle
3 Replies

5. Shell Programming and Scripting

sed substitution or awk, need to direct change the file

I want change the file when the line contains $(AA) but NOT contains $(BB), then change $(AA) to $(AA) $(BB) eg: $(AA) something $(AA) $(BB) something (7 Replies)
Discussion started by: yanglei_fage
7 Replies

6. Shell Programming and Scripting

Creating a sed script to change ip addresses in a file

So I'm new to this sed command and I am trying to create a script that replaces ip addresses when I name a file but can't tweak it to work. Here is what it looks like: #!/bin/bash # file=$1 # sed -e 's/-CPUaddr 10.30.10.166/-CPUaddr 10.30.10.151/g' -i "$file" sed -e 's/-CPUaddr... (10 Replies)
Discussion started by: uradunce
10 Replies

7. UNIX for Dummies Questions & Answers

sed question - change 2d to last line in file

The file is repeating blocks of text separated by a blank line. Some of the lines are unique. e.g.: This is unique line This is line 2 This is line 3 This is line 4 This is unique line This is line 2 This is line 3 This is line 4 ... The goal is to change/replace line 4... (2 Replies)
Discussion started by: uiop44
2 Replies

8. Shell Programming and Scripting

sed to change a configuration file

Hi I want to create a configuration file from a template one. The only thing to change for now is the port number that I need to take from another file that has this format: server:port So I need to take the 2nd field. server is passed in the script argument. how to do that? I... (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

9. Shell Programming and Scripting

how to change a number in a file with sed?

Hello, I have a file which contains some line as follows, 9.9 TEMP 9.9 MUCOEFF 0.0 EPSILON And I want to increase this MUCOEFF by 0.2 as 9.9 TEMP 10.1 MUCOEFF 0.0 ... (7 Replies)
Discussion started by: lorenzz
7 Replies

10. Shell Programming and Scripting

How to change this file with SED?

I have a file like below: I want to delete the rows which begining with "BC" "CD" and "TY" . then change every fields into one row like this at last delete the head words : USing awk to change it is not hard. I want to know how to do this work using SED ? Thank you! ... (4 Replies)
Discussion started by: bejgirl
4 Replies
Login or Register to Ask a Question