Combination awk and rename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combination awk and rename
# 1  
Old 08-16-2013
Combination awk and rename

Hello,
i wondering if anybody can help me with the following:

doing a search for the words "Sample ID:" in a file called test.txt
and I need to have the string after this (Postmatch)
Then I want that the string is used to rename an other file called test.pdf into .... the string.pdf

I already tried some thing but still with errors
Code:
 
awk '/Sample ID:/ { printf "mv test.pdf $3.pdf\n" ,$3,$3 >>"sampleID.txt

The following is working:
Code:
awk '/Sample ID:/ { print $3 }' sampleID.txt

and is printing the correct string that I need

If you think it is easier to do it in perl or python, I am fine with this as well to learn/adapt.

Many thanks for all your help

Kindly regards
Koen
# 2  
Old 08-16-2013
Show some sample input please.
# 3  
Old 08-16-2013
Hello,
First of all thanks for the very quick reply, here is an extract of the SampleID.txt file:

LIEGE
Name: XXXXXX
ID: 123456789
Gender: F
Sample ID: 123456789A1
Sample Type: Serum
Dilution Factor: 1
Priority: Routine
# 4  
Old 08-16-2013
I think you can do something lke
Code:
awk '/Sample ID/ { print "echo mv test.pdf "$3".pdf"); }' sampleid.txt | sh

Remove the echo once you're sure it does what you want.
# 5  
Old 08-16-2013
Thanks a lot,
We are almost there, the only minor thing is that the script is putting an ? before .pdf
110715516102?.pdf instead of 110715516102.pdf

Thanks,
Koen
# 6  
Old 08-16-2013
Your file is full of carriage returns, probably generated by MS Windows.

Code:
awk '/Sample ID/ { sub(/\r/,""); print "echo mv test.pdf "$3".pdf"); }' sampleid.txt | sh

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 08-16-2013
Thanks a lot
You made my script working..
Last and simple question,
Can I add multiple codes with awk in the same bash ?

Reason for asking is that I will have different lookups ?

THANKS !!!
Koen
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk or a combination of commands to read and calculate nth lines from pattern

Two numerical lines, found by either header line, need to be added and the total placed in a new-header section. Also the total should should be rounded or cut to a two decimal anynumber.XX format with the AB string added on the end. For example: The numerical lines from headers 2 and 3 are... (3 Replies)
Discussion started by: jessandr
3 Replies

2. Shell Programming and Scripting

awk to count and rename based on fields

In the below awk using the tab-delimited input, I am trying count the - symbol in $5 and output the count as well as the renamed condition ins. I am also count the - symbol in $6 and output the count as well as the renamed condition del. I am also count the tomes that in $5 and $6 there are... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

awk and rename ?

I have a log file from /m/n/o/A.log with 1 line current content: 19518634,SW,windows_x86_64 and an folder from /x/y/z/testit_folder How do you write a shell script to rename test_folder to changed_monthdate_19518634 If today is 04/23 the name should be changed_0423_19518634 Thanks for your... (1 Reply)
Discussion started by: sabercats
1 Replies

4. Shell Programming and Scripting

Considerable trouble with for loop in combination with awk

I have the text file where each line has the format: chr10 101418889 101418904 0.816327 Right now the interval between column 2 and 3 is 15. I only want the two consecutive positions starting at position 1, write it to a file, then move up one position write to file etc. So that: ... (1 Reply)
Discussion started by: jfern
1 Replies

5. Shell Programming and Scripting

Rename file using sed or awk

I have a filename like 1_DATE_3_4.5_888 and I want to modify the date field (ie the last 4 digits ) alone and remove the last field. Old filename:1_DATE_3_4.5_888 Given date (for eg):120606259532 modified date:120606259899 new filename:1_<modified date>_3.4.5 (14 Replies)
Discussion started by: sandy88
14 Replies

6. Shell Programming and Scripting

help with awk for file combination

1)file1: | *Local Communication Bandwidths (MB/Sec) | Memory copy (bcopy) | | ^ | mmap_bandwidth | | ^ | mmap_read bandwidth | | ^ | memory write bandwidth | | Local Communication Latencies | Pipe Latency | 2)file2 422.6903 1948.9000 ... (9 Replies)
Discussion started by: yanglei_fage
9 Replies

7. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

8. Shell Programming and Scripting

awk split and rename files

I have a file test1.html like below: <dctm_topnav_en_US> <html> ..... </html> <dctm_topnav_en_CA> <html> ..... </html> <dctm_topnav_en_FR> <html> ..... </html> I need to use awk to split this into three file names like en_US.html , en_CA.html, en_FR.html each having content between... (4 Replies)
Discussion started by: vijay52
4 Replies

9. Shell Programming and Scripting

Sed Awk Cut Grep Combination Help ?

I have been reading for a few hours trying to educate myself enough to accomplish this task, so please know I have performed some research. Unfortunately, I am not a *NIX scripting expert, or a coder. I come from a network background instead. SO, here is my desired outcome. I have some Cisco... (5 Replies)
Discussion started by: abbzer0
5 Replies

10. UNIX for Dummies Questions & Answers

awk and file combination

Hi there, I have 3 files and i want to take different fields from each file and combine them in one. I would like to ask if somebody tell me how can I refer to each field of the different files to write an awk command. I mean can I do sth like awk '........... print $1.file1 $3.file2}'... (1 Reply)
Discussion started by: sickboy
1 Replies
Login or Register to Ask a Question