Inserting the last field first


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Inserting the last field first
# 1  
Old 04-24-2015
Inserting the last field first

I have a list of more than 1000 files on the following format.

Code:
roman_pottery_in_the_archaeological_record_2007.pdf 
power_politics_and_religion_in_timurid_iran_2007.pdf 
toward_a_theory_of_human_rights_religion_law_courts_2006.pdf 
i_was_wrong_the_meanings_of_apologies_2008.pdf

I want to take the year and put it as the first field. I am trying to do the
task using rename but have not come up with anything.

I want to get

Code:
$ ls

2007_roman_pottery_in_the_archaeological_record.pdf 
2007_power_politics_and_religion_in_timurid_iran.pdf 
2006_toward_a_theory_of_human_rights_religion_law_courts.pdf 
2008_i_was_wrong_the_meanings_of_apologies.pdf

I have had some success in getting

Code:
$ ls

2007_roman_pottery_in_the_archaeological_record_2007.pdf 
2007_power_politics_and_religion_in_timurid_iran_2007.pdf 
2006_toward_a_theory_of_human_rights_religion_law_courts_2006.pdf 
2008_i_was_wrong_the_meanings_of_apologies_2008.pdf

The remaining task is now to remove the last field that holds the year.

Last edited by kristinu; 04-24-2015 at 05:07 AM..
# 2  
Old 04-24-2015
Hello Kristinu,

Not sure if I understood your reuirement completly but following may help you. Let me know if you have any queries on same. Please always show us the expected output of yours which will help us to understand the requirement clearly.
Code:
awk '{A=$0;gsub(/.*_|\..*/,X,$0);print $0 OFS A;}' Input_file

Output will be as follows.
Code:
2007 roman_pottery_in_the_archaeological_record_2007.pdf
2007 power_politics_and_religion_in_timurid_iran_2007.pdf
2006 toward_a_theory_of_human_rights_religion_law_courts_2006.pdf
2008 i_was_wrong_the_meanings_of_apologies_2008.pdf

Thanks,
R. Singh
# 3  
Old 04-24-2015
How about
Code:
ls | while read FN; do NE=${FN%.*}; echo mv ${FN} ${NE##*_}_${FN%_*}${FN#${NE}}; done
mv roman_pottery_in_the_archaeological_record_2007.pdf 2007_roman_pottery_in_the_archaeological_record.pdf
mv power_politics_and_religion_in_timurid_iran_2007.pdf 2007_power_politics_and_religion_in_timurid_iran.pdf
mv toward_a_theory_of_human_rights_religion_law_courts_2006.pdf 2006_toward_a_theory_of_human_rights_religion_law_courts.pdf
mv i_was_wrong_the_meanings_of_apologies_2008.pdf 2008_i_was_wrong_the_meanings_of_apologies.pdf

Remove the echo if happy.
This User Gave Thanks to RudiC 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

Inserting a field without disturbing field separator on other fields

Hi All, I have the input as below: cat input 032016002 2.891 97.109 16.605 27.172 24.017 32.207 0.233 0.021 39.810 0.077 0.026 19.644 13.882 0.131 11.646 0.102 11.449 76.265 23.735 16.991 83.009 8.840 91.160 0.020 99.980 52.102 47.898 44.004 55.996 39.963 18.625 0.121 1.126 40.189... (15 Replies)
Discussion started by: am24
15 Replies

2. Shell Programming and Scripting

Inserting some text if a field in the last column changes

Hi, I have a file which looks like this: A 01 00 B 02 00 C 04 00 D 00 01 E 01 01 F 02 01 G 01 04 H 02 04 I want to insert some text if the field if the last column changes. It should look like this: Value 00 A 01 00 B 02 00 C 04 00 Value 01 (6 Replies)
Discussion started by: wenclu
6 Replies

3. UNIX for Dummies Questions & Answers

Inserting a sequential number into a field on a flat file

I have a csv flatfile with a few million rows. I need to replace a field (field number is 85) in the file with a sequential number. As an example, let's assume there are only 4 fields in the file: A,A,,32 A,A,,27 A,B,,43 C,C,,354 If I wanted to amend the 3rd field in this way my... (2 Replies)
Discussion started by: BristolSmithy
2 Replies

4. Shell Programming and Scripting

Inserting string in between field in comma separated file

Hello Mates, I have one txt file having commo seperated values. I have to insert string "FALSE" in 2nd field from the end. E.G SE18 6RN,,,,5439070,1786840,,1000002148671600,123434 Out put should be: SE18 6RN,,,,5439070,1786840,FALSE,1000002148671600,123434 Can some one help me to... (8 Replies)
Discussion started by: krsnadasa
8 Replies

5. Shell Programming and Scripting

Inserting a new field inbetween two exisitng field

I have a '|' delimited file. My file looks like below 23|nationalhoilday|feb12||||||||||||||california|northdistrict|| In the same way, each record has 164 fields. I have to insert one more field after the 85th field. Expected output... (3 Replies)
Discussion started by: machomaddy
3 Replies

6. UNIX for Dummies Questions & Answers

Best method for inserting a field from a file into another file?

Hello, Hoping someone can help! I have a file (fileA) with a potentially different number of | delimited fields the file looks like: fileA A|B|C|D|E|F|G| A|B|C|D|E|F|G| This file could have 100+ fields and have 2million+ records I have another file (fileB) which contains an 8 digit... (3 Replies)
Discussion started by: dendright
3 Replies

7. Shell Programming and Scripting

ORA-01756 Error while inserting a file in CLOB field

Hi, Please guide me where i am doing wrong, i am getting ORA-01756:quoted string not properly terminated when i am trying to insert file into CLOB cloumn of Oracle DB. Please find below the code where log file variable is myLogFile. Please let me know where i am doing wrong. ... (0 Replies)
Discussion started by: rajeshorpu
0 Replies

8. Shell Programming and Scripting

gawk help for inserting a field of a .txt file in the same file

i had the following type of data file vchrdump: Vouchers For Date :05/01/2009 * ... (4 Replies)
Discussion started by: KANNI786
4 Replies

9. Shell Programming and Scripting

Sort alpha on 1st field, numerical on 2nd field (sci notation)

I want to sort alphabetically on the first field and sort in descending numerical order on the 2nd field. With a normal "sort -r -n" it does this: abc ||| 5e-05 ||| bla abc ||| 3 ||| ble def ||| 1 ||| abc def ||| 0.2 ||| def As you can see it ignores the fact that 5e-05 is actually 0.00005... (1 Reply)
Discussion started by: FrancoisCN
1 Replies

10. Shell Programming and Scripting

Question about sed. Inserting text in field?

Hi, I have tried to develop a sed script that inserts date and time in the third field in the first and second row below. The third row is an example and it shows where the date and time should be inserted. The script should check if the row already has date and time in the third field and if it... (2 Replies)
Discussion started by: pcrs
2 Replies
Login or Register to Ask a Question