Help Needed! - Cut characters after a text string and append to end of filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help Needed! - Cut characters after a text string and append to end of filename
# 1  
Old 12-10-2012
Help Needed! - Cut characters after a text string and append to end of filename

Hi all.. I have several unique files that contain one thing in common, and that is acct#. For all files in the directory, I want to append the 10 characters following the word "ACCOUNT:" to the end of the filename.

for example:
I have file 111_123 that contains ACCOUNT:ABC1234567

The file name should change to 111_123.ABC1234567


Any help is much appreciated.
# 2  
Old 12-10-2012
try:
Code:
for file in *
do
   acct=$(grep "^ACCOUNT:" "$file" | sed 's/^ACCOUNT: *//')
   [[ -n "$acct" ]] && mv -f "$file" "$file.$acct"
done

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 12-10-2012
One way:
Code:
cd /path/to/your/dir
for i in *
do
 if [[ -f $i ]]
 then
  str=$(awk 's=index($0,"ACCOUNT:"){print acct=substr($0,s+8,10);exit(0)}' "$i")
  [[ -n $str ]] && echo mv "$i" "$i.$str"
 fi
done

Remove the echo when everything seems alright.
This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 12-10-2012
Yet another way:

Code:
for i in *;do awk -F: '/ACCOUNT/{system("mv -vv "FILENAME" "FILENAME"."$2)}' $i;done

This User Gave Thanks to in2nix4life For This Post:
# 5  
Old 12-10-2012
Thanks for the quick response guys. I got these to work.
# 6  
Old 12-10-2012
Code:
for file in *
do 
  acc=$(sed -n 's/ACCOUNT:\(.\{10\}\)/\1/p' "$file")
  if [ -n "$acc" ]; then
    mv "$file" "$file.$acc"
  fi
done


Last edited by Scrutinizer; 12-10-2012 at 01:25 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need script to cut string from a filename

Hi, I need a script which do below I have a filename: TEST2013_09_17_XX_XX_XX.csv Now script should create a new file with name: XX_XX_XX.csv Or I should say i need the output as XX_XX_XX.csv Please help. Mant thanks in advance (3 Replies)
Discussion started by: sv0081493
3 Replies

2. Shell Programming and Scripting

Append this string to end of each line

Platform: Solaris 10 I have a file like below $ cat languages.txt Spanish Norwegian English Persian German Portugese Chinese Korean Hindi Malayalam Bengali Italian Greek Arabic I want to append the string " is a great language" at end of each line in this file. (3 Replies)
Discussion started by: omega3
3 Replies

3. Shell Programming and Scripting

sed - Find a String and append a text end of the Line

Hi, I have a File, which have multiple rows. Like below 123456 Test1 FNAME JRW#$% PB MO Approver XXXXXX. YYYY 123457 Test2 FNAME JRW#$% PB MO Super XXXXXX. YYYY 123458 Test3 FNAME JRW#$% PB MO Approver XXXXXX. YYYY I want to search a line which contains PB MO Approver and append... (2 Replies)
Discussion started by: java2006
2 Replies

4. Shell Programming and Scripting

Append string to filename

Hi, i wrote the script like... v_date=`date "+%Y%m%d"`; v_date1="rejects_"$v_date'.txt'; echo $v_date1; awk -F\| 'NF==4{ print $0>$v_date1;next} NF!=4 {print $0 >"murali.txt";next}' test1.txt while append data into file as coming like file is $v_date1,but i want data into file... (4 Replies)
Discussion started by: bmk
4 Replies

5. Shell Programming and Scripting

Append text to end of every line

I've scoured the internet with mixed results. As an amateur I turn to the great minds here. I have a text file of 80 or so lines. I want to add ".pdf" to the end of each line. (For now that's it) Most of the internet points toward using "sed". I don't know coding but can figure things out... (4 Replies)
Discussion started by: spacebase
4 Replies

6. Shell Programming and Scripting

help needed with shell script to append to the end of a specific line in a file on multiple servers

Hi Folks, I was given a task to append three IP's at the end of a specific (and unique) line within a file on multiple servers. I was not able to do that with the help of a script. All I could was: for i in server1 server2 server3 server4 do ssh $i done I know 'sed' could be used to... (5 Replies)
Discussion started by: momin
5 Replies

7. Shell Programming and Scripting

Cut and append the text

I have a text like this ... i need append the text whihc is after 'csb' into the end of the line input ----- sdir;csp os_lib-0.5.24;bdir;cbpdob ---enable-useosstl sdir;csp oc_lib-0.10.4;bdir;cbpdob ---enable-useosstl output sdir;csp os_lib-0.5.24;bdir;cbpdob ---enable-useosstl... (6 Replies)
Discussion started by: girija
6 Replies

8. UNIX for Dummies Questions & Answers

Append characters to end of line

Coding in unix shell script. Hi All, Please help Thanks (6 Replies)
Discussion started by: sam12345
6 Replies

9. Shell Programming and Scripting

sed script to remove nth characters from end of filename

Hi all, I have this basic script to remove, in this case 9 characters from the end of a file name. This is what I have so far, for file in *.mov do newname=`echo $file | sed 's/\(.*\)........./\1/' ` mv "$file" "$newname" done The problem is that it removes the file extension as well.... (2 Replies)
Discussion started by: Monkey Dean
2 Replies

10. AIX

CUT command - cutting characters from end of string

Hello, I need to delete the final few characters from a parameter leaving just the first few. However, the characters which need to remain will not always be a string of the same length. For instance, the parameter will be passed as BN_HSBC_NTRS/hub_mth_ifce.sf. I only need the bit before the... (2 Replies)
Discussion started by: JWilliams
2 Replies
Login or Register to Ask a Question