Splitting a string and putting another string in the middle?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Splitting a string and putting another string in the middle?
# 1  
Old 07-24-2009
Bug Splitting a string and putting another string in the middle?

Hello all!

I'm trying to put together a small script that will take in a file name and attach a datestamp to the end of it (but before the file type extension).

To illustrate...

Before:
filename.txt
anotherfilename.txt

After:
filename_20090724.txt
anotherfilename_20090724.txt

Searching around the forums I've found some pieces that I think are parts of the solution:

Code:
 
date +%Y%m%d ---->gets me my datestamp format
expr index string . ------>gets me the character position of '.'

So now my question is how to substring the file name from the beginning to the period and also substring from the period to the end...giving me my two halves of the full name so that I can concatenate my datestamp in between. Any help is very appreciated.

Thanks!
Glen
# 2  
Old 07-24-2009
Try this:

Code:
echo 'filename.txt' | 
awk -F"." -v d=$(date "+%Y%m%d") '{print $1 "_" d FS $2}'

# 3  
Old 07-24-2009
That works perfectly! Thanks for the response, I figured there was a pretty simple one line answer to this (I usually make things more complicated then they should be). Now for added complexity, I'd like to store this in a variable so that I can use it in an 'mv' statement (i.e. mv filename.txt $variable):

Code:
 
export temp = `echo 'filename.txt' | awk -F"." -v d=$(date "+%Y%m%d") '{print $1 "_" d FS $2}'`
echo $temp

This almost works, I get the following error messages:

Code:
test.shl: line 4: export: `=': not a valid identifier
test.shl: line 4: export: `filename_20090724.txt': not a valid identifier

I seem to be doing something wrong with the variable assignment. Any suggestions?

Thanks in advance!
Glen
# 4  
Old 07-24-2009
Remove the spaces around the equal sign =.
Why do you export the variables? It's not necessary if you use the variables in the current shell.

Regards
# 5  
Old 07-24-2009
I was working off an example script which is why the export is there. Now that I know it's not necessary I've taken it off Smilie I'm pretty new at this so I'm kind of learning as I go.

Removing the spaces did the trick, thanks! One more question, I'm trying to implement this whole apparatus inside of a loop statement so that I can read all these filenames from a list file. From an example I found online I put together this:

Code:
 
#!/bin/ksh
 
listfile=myfiles.lst
 
for i in ${listfile}
 
do
 
  temp=`echo $i | awk -F"." -v d=$(date "+%Y%m%d") '{print $1 "_" d FS $2}'`
  mv $i $temp
 
done

The problem is that it works but in the wrong way, instead of applying the datestamp to the items in the list, it takes the actual list file and applies the datestamp to it: myfiles_20090724.lst. I'm guessing I'm doing something wrong in the first line of the for statement where I'm trying to isolate a single line item in the variable $i. Can you shed a little light on the subject?

Thanks again,
Glen
# 6  
Old 07-24-2009
You're using the filename instead of the content of the file and it's more efficient to call the date command once:

Code:
#!/bin/ksh

dd=$(date "+%Y%m%d")

while read i
do
  temp=`echo $i | awk -F"." -v d=$dd '{print $1 "_" d FS $2}'`
  mv "$i" "$temp"
done < myfiles.lst

Actualy this command is sufficient to do the job:

Code:
awk -F"." -v d=$(date "+%Y%m%d") '{system("mv " $0 " " $1 "_" d FS $2)}' myfiles.txt

# 7  
Old 07-24-2009
Quote:
Originally Posted by Franklin52
You're using the filename instead of the content of the file and it's more efficient to call the date command once:

Code:
#!/bin/ksh
 
dd=$(date "+%Y%m%d")
 
while read i
do
  temp=`echo $i | awk -F"." -v d=$dd '{print $1 "_" d FS $2}'`
  mv "$i" "$temp"
done < myfiles.lst

Actualy this command is sufficient to do the job:

Code:
awk -F"." -v d=$(date "+%Y%m%d") '{system("mv " $0 " " $1 "_" d FS $2)}' myfiles.txt

That's very interesting, so the list file name is actually fed into the loop (or command) at the very end of the script as opposed to the front. Is there a specific reason for this? Or is it just the way the syntax works? This is all very informative for me, I appreciate it! Smilie

Also, I'd like to put in some error handling. Specifically to watch for file names that would not be present. Would I be looking for the program code variable "$?"? I'm seeing from other examples that checking $? for a non-zero value indicates that the command failed.

Last edited by jisoo411; 07-24-2009 at 06:50 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Map a string in a the middle of a record

Hi Folks! I hope you can help me figure this out. I need to print a record which contains the contents of a config file. The contents of the config file should be found within the 21st and the 30th position of the fixed width reference file. Config File: aaaa bbbb cccc Reference... (3 Replies)
Discussion started by: kokoro
3 Replies

2. Shell Programming and Scripting

Putting white Space at the end of the string

Hi Guys, Hope, you all are doing good out there. I am writing a shell script and currrint in need of your help. This is what I need to do; I have position based plain file. One of the fields is 15 character long. I need to fill that field. The problem is that the value is dynamic, it could... (4 Replies)
Discussion started by: singh.chandan18
4 Replies

3. Linux

Remove newline in middle of string

my file input is with tab as delimiter, and in every line, there would be a skip of line with an unexcepted newline breaker. I'd like to remove this \n and put the information in the same line. INPUT a1 b1b2 c1 c2 d1 a2 b3 c3 d4 OUTPUT a1 b1b2 c1c2 ... (9 Replies)
Discussion started by: kinkichin
9 Replies

4. Shell Programming and Scripting

Find the middle character from a string using sed

Input: qwertmyuiop It should print "m". What's the command to do this with sed? (6 Replies)
Discussion started by: cola
6 Replies

5. UNIX for Dummies Questions & Answers

Help with splitting a string..

Hello I need help with the following. I have strings like #if defined(__def1__) #if defined(__def1__) || defined(__def2__) #if defined(__def1__) && defined(__def2__) && defined(__def3__). #if defined(__def1__) || defined(__def2__) || defined(__def3__). I need to print what is there in... (4 Replies)
Discussion started by: srk
4 Replies

6. UNIX for Dummies Questions & Answers

Add string to middle of a file

Hi, I want to write a script that takes a file and a string as params and adds the string to the middle line of the file. Also, I want to output the results back to the original file passed without using temp files. I am very much new to UNIX so this is all a little like black magic to me at... (15 Replies)
Discussion started by: Chiefos
15 Replies

7. Shell Programming and Scripting

stripping certain characters in at the middle of a string

I am trying to strip out certain characters from a string on both (left & right) sides. For example, line=see@hear|touch, i only want to echo the "hear" part. Well i have tried this approach: line=see@hear|touch templine=${line#*@} #removed "see@" echo ${templine%%\|*} #removed... (4 Replies)
Discussion started by: mcoblefias
4 Replies

8. Shell Programming and Scripting

Splitting a string

Hi, I am new to shell scripting, Can any body suggest me how I can split a string with a delimiter as whitespace into words and store into a array. I have read a line from file, now I want to split the line into words and store in a array for further use. eg : the sky is blue want... (3 Replies)
Discussion started by: smk
3 Replies

9. Shell Programming and Scripting

add a string in the middle of the file

i want to add a string in a very top of a file without using VI or SED or AWK this is what ive done: (echo '0a'; echo 'LINE OF TEXT'; echo '.'; echo 'wq') | ed -s myfile to add astrng right in the middle i could have count the lines of the file and just chenge the address. ... (6 Replies)
Discussion started by: ciroredz
6 Replies

10. Shell Programming and Scripting

Problem to add the string(without sed & awk) into the middle of file

Hi, I have tried many times to add the string into the first line of the file or the middle of the file but could not find the solution. I first tried by $echo "paki" >> file This code only append paki string at the end of file "file" but how can i add this "paki" into the first line or... (5 Replies)
Discussion started by: ali hussain
5 Replies
Login or Register to Ask a Question