Grep and rename ?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep and rename ?
# 1  
Old 04-23-2015
Grep 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 help.

Last edited by sabercats; 04-23-2015 at 11:13 PM..
# 2  
Old 04-23-2015
Code:
NUM=`cat /m/n/o/A.log | awk '{ print $3}' | awk -F, '{ print $1}'`
DATE=`date '+%m%d'`
mv /x/y/z/testit_folder   /x/y/z/changed_$DATE_$NUM

# 3  
Old 04-24-2015
Thanks, it is almost perfect. I cannot get date
Code:
 date '+%m%d'
date: extra operand `+%m%d'
Try `date --help' for more information.
Tried date --help
Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

  -d, --date=STRING         display time described by STRING, not `now'
  -f, --file=DATEFILE       like --date once for each line of DATEFILE
  -r, --reference=FILE      display the last modification time of FILE
  -R, --rfc-2822            output date and time in RFC 2822 format
      --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.
                            TIMESPEC=`date', `seconds', or `ns' for
                            date and time to the indicated precision.
  -s, --set=STRING          set time described by STRING
  -u, --utc, --universal    print or set Coordinated Universal Time
      --help     display this help and exit
      --version  output version information and exit

FORMAT controls the output.  The only valid option for the second form
specifies Coordinated Universal Time.  Interpreted sequences are:

  %%   a literal %
  %a   locale's abbreviated weekday name (e.g., Sun)
  %A   locale's full weekday name (e.g., Sunday)
  %b   locale's abbreviated month name (e.g., Jan)
  %B   locale's full month name (e.g., January)
  %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
  %C   century; like %Y, except omit last two digits (e.g., 21)
  %d   day of month (e.g, 01)

---------- Post updated 04-24-15 at 01:23 AM ---------- Previous update was 04-23-15 at 11:23 PM ----------

It works now. Thanks for your help
# 4  
Old 04-24-2015
Getting rid of the need to call cat and awk twice (and just using standard shell built-ins) and fixing the reference to $DATE_ (instead of $DATE), you could also try:
Code:
#!/bin/ksh
IFS=" ," read x x NUM x < /m/n/o/A.log
DATE=$(date '+%m%d')
mv /x/y/z/testit_folder /x/y/z/change_${DATE}_$NUM

which with your sample input in /m/n/o/A.log runs the command:
Code:
mv /x/y/z/testit_folder /x/y/z/change_0424_19518634

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using rename

Renaming files using rename on the following files rename 's/.99999/.99999.sac.pzs/g' *.99999 sac_pzs_iv_esml_hhz__2013.074.10.18.23.0000_2599.365.23.59.59.99999 sac_pzs_iv_favr_hhe__2010.187.00.00.00.0000_2599.365.23.59.59.99999... (1 Reply)
Discussion started by: kristinu
1 Replies

2. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

3. UNIX for Dummies Questions & Answers

Using rename

I want to rename files Files show like this 1977SSD_rsdtst.pdf 1976SDP_rstdtsr.pdf 1943FDT_rstdsrt.pdf 1996DFF_stdstrd.pdf I want to introduce _ after the year, and move the characters to the end to get 1977_rsdtst_SSD.pdf 1976_rstdtsr_SDP.pdf 1943_rstdsrt_FDT.pdf... (4 Replies)
Discussion started by: kristinu
4 Replies

4. UNIX for Dummies Questions & Answers

need help with rename

hi guys i am writing a script to change the filename which is enterered as input to lower case letter even if one letter is upper case i have to change it to lower case i get the input and use sed comand should i use like that sed/s/a-z/A-Z/d will it be like that can u please help me (8 Replies)
Discussion started by: farhan_t49
8 Replies

5. Shell Programming and Scripting

Grep and rename the filename

Hi All, Can you please help me. The situation is like this. There are many different file name in this directory. I have to grep all the file that the name start with "PTWO" and rename it to COM with the current date. This is the script that I have done and it hit an... (16 Replies)
Discussion started by: badbunny9316
16 Replies

6. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

7. Shell Programming and Scripting

rename

hi, im doin an assignment which requires you to build a shell script to do the MS DOS style equivilant (sp?) of the rename function. What i have to do is a loop which checks the following rules and output messages (same): Can't have anything after target Can't have more than one dot Can't... (1 Reply)
Discussion started by: bohoo
1 Replies

8. UNIX for Dummies Questions & Answers

how can I rename the following=-^

I have a file named -^, I want to look at it, rename, etc. Any help out there?? (5 Replies)
Discussion started by: nj78
5 Replies

9. UNIX for Dummies Questions & Answers

rename using mv ?

Hi all how can can remove the underscore from this number in this series _1234567.abc _1234567.abcd I was trying mv _1234567* 1234567 but did not work ? thanks s (2 Replies)
Discussion started by: simon2000
2 Replies

10. UNIX for Dummies Questions & Answers

Rename a number of files using grep

Hi all, I'm trying to rename a directory of html-files: index.php?page=start should become start.html I've lost track of all the pipes and brackets and decided I need some help ;-) Many thanks. (4 Replies)
Discussion started by: TylerDurden
4 Replies
Login or Register to Ask a Question