Cutting a string using more than one character as delimiter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cutting a string using more than one character as delimiter
# 1  
Old 05-31-2013
Cutting a string using more than one character as delimiter

Hi ,


I have a set of files in a folder which i need to cut in to two parts....


Sample files

Code:
touch AE_JUNFOR_2014_MTD_2013-05-30-03-30-02.TXT
touch AE_JUNFOR_2014_YTD_2013-05-30-03-30-02.TXT
touch temp_AE_JUNFOR_2014_MTD_2013-05-30-03-30-02.TXT
touch AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT
touch AE_ACTUAL_2013_APR_YTD_2013-05-03-09-59-44.TXT
touch temp_AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT


I need the file name in the above files to be sent to two variables.

Only temp* files are considered for the cut.

Sample file :- "temp_AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT"


var1 = AE_ACTUAL_2013_APR
var 2 = 2013-05-03-09-59-44.TXT

I tried the below but Cut takes in only one character as delmiter... I cannot use "M" in MTD as a delimiter because for March files the file name will have MAR before MTD and the result will be wrong.
Code:
 echo temp_AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT|cut -d "MTD" -f1

 echo temp_AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT|cut -d "MTD" -f2




Please help me to find a efficient way to cut the file names .



Thanks
Arun
# 2  
Old 05-31-2013
This loop splits apart on both _ and -, then puts back together looking for the token MTD:

Code:
OLDIFS="$IFS"
IFS="-_"
for FILE in temp*
do
        STR=""
        set -- $FILE
        shift
        while [ "$1" != "MTD" ]
        do
                STR="${STR}_$1"
                shift
        done
        shift

        STR1="${STR:1}"
        STR2="$*"
        echo "$STR1"
        echo "$STR2"
done
IFS="$OLDIFS"

# 3  
Old 05-31-2013
Code:
$ echo temp_AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT| awk -F"_MTD_" ' { print $1 } '
temp_AE_ACTUAL_2013_APR
$ echo temp_AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT| awk -F"_MTD_" ' { print $2 } '
2013-05-03-09-59-44.TXT

# 4  
Old 05-31-2013
Actually it should be...
Quote:
Originally Posted by anbu23
Code:
$ echo temp_AE_ACTUAL_2013_APR_MTD_2013-05-03-09-59-44.TXT| awk -F"temp_|_MTD_" ' { print $2, $3 }'

This User Gave Thanks to shamrock For This Post:
# 5  
Old 05-31-2013
Thank you all ,


The AWK worked as expected.

Thank you all again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. Shell Programming and Scripting

Cutting values with delimiter

Hi All, I have a string with , delimiter america,finland,netherlands Now i want these values to be stored in file as below with newline character at end of each value america finland netherlands Regards Prasad (3 Replies)
Discussion started by: krishna_gnv
3 Replies

3. Shell Programming and Scripting

Cutting a part of line till delimiter

here are the few scenarios... isoSizeKB text NOT NULL, reserved1 varchar(255), KEY `deviceId` (`deviceId`) `d5` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, `dHead` enum('HistoryInfo','Diversion') COLLATE utf8_unicode_ci, `ePR` int(11) DEFAULT '0', PRIMARY KEY (`id`) ... (7 Replies)
Discussion started by: vivek d r
7 Replies

4. Shell Programming and Scripting

Perl Cutting character(s) from a word

Hello, How to cut a char(s) for a word using perl?? I want to cut the number(s) from these sample words: Port-channel24 Po78 Po99 Port-channel34 $word = "Port-channel24"; I want to put only the number in a varible. Appreaciate using simple way in order to use it... (3 Replies)
Discussion started by: ahmed_zaher
3 Replies

5. UNIX for Dummies Questions & Answers

cutting columns if delimiter has more than one charecter (|^)

Hi All, I am having a file with the delimiter '|^'. File name:test_dlim.csv I want to cut the first field of this using awk command. I tried with the help of the following link:... (2 Replies)
Discussion started by: boopathyvasagam
2 Replies

6. Shell Programming and Scripting

Cutting the value from a string

Hi all I have a string variable ${WHERE_SQL1} where i want to cut the first value of a variable. Eg ${WHERE_SQL1} = 'Where a.id =.................' I the string to be 'a.id =.......' Thanks in advance (2 Replies)
Discussion started by: theeights
2 Replies

7. UNIX for Dummies Questions & Answers

Cutting a string based on the third occcurence of a character

Hello, I am new to unix hence struggling with my requirement. I have a string like : ECR/CHQ/GBP/12345.out I need to get only the ECR/CHQ/GBP portion of the string(cut the string based on the third occurrence of / )...How do it do it? Many thanks (3 Replies)
Discussion started by: valluvan
3 Replies

8. UNIX for Dummies Questions & Answers

Cutting a portion of a line seperated by pipe delimiter

Hi, In the below line a|b|10065353|tefe|rhraqs|135364|5347575 dgd|rg|4333|fhra|grhrt|46423|urdsgd Here i want to cut the characters in between the second and third pipe delimiter and then between fifth and sixth delimiter and retain the rest of the line. My output should be ... (3 Replies)
Discussion started by: ragavhere
3 Replies

9. Shell Programming and Scripting

Cutting a tab delimiter file

I have a 30 column tab delimited record file. I need to extract the first 10column. The following command to cut was not working cut -f 1-10 -d "\t" filename. Could any one keep on this . Thanks in Advance (4 Replies)
Discussion started by: vinod.thayil
4 Replies

10. UNIX for Dummies Questions & Answers

cutting columns if delimiter has more than one charecter

Hi, My file looks like abc$%sdfhs$%sdf$%sdfaf$% here as seen delimiter is $%...now how cas i take out second field as cut command expect delimiter as single charecter only.....is there is any other way thanks and regards mahabunta (9 Replies)
Discussion started by: mahabunta
9 Replies
Login or Register to Ask a Question