Create a name of a file by comparison of strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Create a name of a file by comparison of strings
# 1  
Old 10-02-2012
Create a name of a file by comparison of strings

Hello everybody,

I need to create a file name from string. The thing is:

I have a directory with files e.g:

test.123
test.234
test.345
test.987

The name should be made from the first and last name of file alphabetically and from the directory, they are in, eg.

201208_test.123_test.987

I used this commands to do that:
Code:
test_nums=`find ./$subdir -maxdepth 1 -mindepth 1 -name "batch.*" -newer  begin.tmp ! -newer end.tmp -printf '%f\n' | sort | tr '\n' ' ' | awk '{ print $1"_"$NF }'

and $subdir is in the form like 2012/08:
Code:
subdir1=`echo $subdir | awk -F"/" '{ print $1$NF }'`

Then the file is moved to different directory. In this directory might be a file, which already has a prefix 201208, eg. 201208_test.012_test.034.
So what I need to do, is create a file, which has a name like 201208_test.012_test.987.

What is the best way to do that? Thank you for your help and time.
# 2  
Old 10-02-2012
Code:
#!/bin/ksh

cd TestNum;

TNoFiD=`ls -rtl | wc -l` #the Total Number of Files in the Directory.. keep in mind this also counts the total line in ls -rtl

TNoAFiD=$TNoFiD - 1 #we want to remove the total line, so Total No of Actual Files in Directory

FFN=`ls -rtl | head -2 | tail -1 | awk '{print $9}'` # First File Name

LFN=`ls -rtl | tail -1 | awk '{print $9}'` # Last File Name

DAM=`date +%Y%m` #Date And Month

desiredname="$DAM"_"$FFN"_"$LFN"

echo $desiredname


Last edited by ramky79; 10-02-2012 at 04:46 PM.. Reason: Code tags, please...
# 3  
Old 10-02-2012
Code:
# subdir1 string
subdir="2008/08"
subdir1=${subdir%/*}${subdir#*/}
echo $subdir1
 
# test_nums strings
ls -1 $subdir | awk 'NR==1{printf $0 " "};{l=$0;}END {print l}' | read ff lf
test_nums=${subdir1}_${ff}_${lf}
echo $test_nums

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Use strings from nth field from one file to match strings in entire line in another file, awk

I cannot seem to get what should be a simple awk one-liner to work correctly and cannot figure out why. I would like to use patterns from a specific field in one file as regex to search for matching strings in the entire line ($0) of another file. I would like to output the lines of File2 which... (1 Reply)
Discussion started by: jvoot
1 Replies

2. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

3. Shell Programming and Scripting

create an array which can store the strings from the user input in shell script

I want to create an array which can store the strings from the user input in shell script . example :- I want to store the 5 fruits name in a single array which the user provides . (1 Reply)
Discussion started by: Pkast
1 Replies

4. Shell Programming and Scripting

Comparison treating strings as zero integers

I'm trying to write a bash script to perform basic arithmetic operations but I want to run a comparison on the arguments first to check that they're a number greater than zero. I want an error to pop up if the arguments args aren't >= 0 so I have: if ! ]; then echo "bad number: $1" fi ... (14 Replies)
Discussion started by: TierAngst
14 Replies

5. Shell Programming and Scripting

Extract two strings from a file and create a new file with these strings

I have the following lines in a log file. It would be great if some one can help me to create a new file with the just entries in the below format. 66.150.161.195 HPSAC=Z05 66.150.161.196 HPSAC=A05 That is just extract the IP address and the string DPSAC=its value 66.150.161.195 -... (1 Reply)
Discussion started by: Tuxidow
1 Replies

6. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

7. Shell Programming and Scripting

Case insensitive comparison of strings

Hi All, In one shell script I have In variable "i" I am getting a full path of a file. Now I want to compare something like -- upper(*Nav*)) I dont want to do like below because in each CASE statement I doing so many operations. Please guide me. Thanks in advance... (4 Replies)
Discussion started by: vishalaksha
4 Replies

8. Shell Programming and Scripting

Comparison of two files which contains strings using Shell scripting or PERL

Hi, I need sample code to compare the two files line by line which contains text strings and to print the difference in the third file. Thanks in advance (1 Reply)
Discussion started by: sudhakaryadav
1 Replies

9. Shell Programming and Scripting

comparison of strings in unix shell scripting

Hi STORAGE_TYPE=$1 echo "########### SQL SESSION STARTED ###########" VALUE=`sqlplus -S /nolog << THEEND connect tcupro/tcupro_dev@bdd1optn SET SERVEROUTPUT ON DECLARE V_STORAGE_TYPE varchar2(3); V_ERR_MSG varchar2(255) ; V_LOG_LEVEL varchar2(200); BEGIN... (1 Reply)
Discussion started by: piscean_n
1 Replies

10. Shell Programming and Scripting

Comparison of 2 strings

I need some help with a script which accepts multiple rows of input (analysis from a virtual robot's testrun) from the standard in, and compares it as a whole with another file featuring erroneous data from a virtual robot's test run. What I want it to be able to do is slice off all the erroneous... (2 Replies)
Discussion started by: Retribution
2 Replies
Login or Register to Ask a Question