Removing date field from the string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing date field from the string
# 1  
Old 11-23-2014
Removing date field from the string

Hii
I am trying to remove the date field from the following string.
Code:
ODS_EPP_COVRG_STN_DETL_FILE_10032014.TXT

. My output should be
Code:
ODS_EPP_COVRG_STN_DETL_FILE.TXT

I tried couple of things:
Code:
echo ODS_EPP_COVRG_STN_DETL_FILE_10032014.TXT|sed 's/_[0-9]*\.[0-9]*//g'

I am getting:
Code:
ODS_EPP_COVRG_STN_DETL_FILETXT

but i need
Code:
ODS_EPP_COVRG_STN_DETL_FILE.TXT



Thanks for your help

Last edited by Scrutinizer; 11-23-2014 at 07:30 PM.. Reason: code tags
# 2  
Old 11-23-2014
A minor modification to your code is all that is required...
Code:
echo ODS_EPP_COVRG_STN_DETL_FILE_10032014.TXT | sed 's/_[0-9]*\.[0-9]*/\./g'

# 3  
Old 11-23-2014
Or:
Code:
sed 's/_[0-9]*\././g'

or

Code:
s=ODS_EPP_COVRG_STN_DETL_FILE_10032014.TXT
echo "${s%_*.TXT}.TXT"

# 4  
Old 11-24-2014
Hello skatpally,

Following may help too in same.

Code:
echo "ODS_EPP_COVRG_STN_DETL_FILE_10032014.TXT" | awk -F"_[0-9]+" '{print $1 $2}'

Output will be as follows.
Code:
ODS_EPP_COVRG_STN_DETL_FILE.TXT

Thanks,
R. Singh
# 5  
Old 11-26-2014
You could use variable substitution in the shell too to save the cost of awk, but if the above works and you're not in a large loop then you may be happy already.



Robin
# 6  
Old 11-26-2014
Quote:
Originally Posted by rbatte1
You could use variable substitution in the shell too to save the cost of awk, but if the above works and you're not in a large loop then you may be happy already.


Robin
Is there a better way than?
Code:
string=ODS_EPP_COVRG_STN_DETL_FILE_10032014.TXT
echo ${string/_[0-9]*/.TXT}

Output:
Code:
ODS_EPP_COVRG_STN_DETL_FILE.TXT

hth
This User Gave Thanks to sea For This Post:
# 7  
Old 11-26-2014
Even older, works on ksh, too:
Code:
string=ODS_EPP_COVRG_STN_DETL_FILE_10032014.TXT
echo ${string%_[0-9]*}.TXT

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk Associative Array and/or Referring to Field by String (Nonconstant String Value)

I will start with an example of what I'm trying to do and then describe how I am approaching the issue. File PS028,005 Lexeme HRS # M # PhraseType 1(1:1) 7(7) PhraseLab 501 503 ClauseType ZYq0 PS028,005 Lexeme W # L> # BNH # M #... (17 Replies)
Discussion started by: jvoot
17 Replies

2. Shell Programming and Scripting

Removing line breaks inside a field

Hi all, I have a csv input file with total 60 fields and the fields are not enclosed with double quotes.One of the field(50th field) in this file has line breaks in it which results in the row getting split into multiple lines.This is causing my load(to table) to fail.I tried to enforce double... (3 Replies)
Discussion started by: Bobby_2000
3 Replies

3. Shell Programming and Scripting

Removing duplicated first field rows

Hello, I am trying to eliminate rows where the first field is duplicated, leaving the row where the last field is "NET". Data file: 345234|22.34|LST 546543|55.33|LST 793929|98.23|LST 793929|64.69|NET 149593|49.22|LST Desired output: 345234|22.34|LST 546543|55.33|LST... (2 Replies)
Discussion started by: palex
2 Replies

4. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

5. Shell Programming and Scripting

removing a particular field from some of the lines in a filed

Hi All, i have a file having multiple lines but mainly constitues of following two types 1) Sat,Oct,1,01:04:51,2011,Local,ESSBASE0,Info(1051037),Logging,out,user,210073155,,,active,for,63,minutes,GETS_SAL,loaded 2)... (2 Replies)
Discussion started by: dev.devil.1983
2 Replies

6. UNIX for Dummies Questions & Answers

Removing spaces in the second field alone

Consider my input string as "abc|b f g|bj gy" I am expecting the output as "abc|bfg|bj gy". Please let me know how to achieve this in unix? Thanks (8 Replies)
Discussion started by: pandeesh
8 Replies

7. Shell Programming and Scripting

Awk Search text string in field, not all in field.

Hello, I am using awk to match text in a tab separated field and am able to do so when matching the exact word. My problem is that I would like to match any sequence of text in the tab-separated field without having to match it all. Any help will be appreciated. Please see the code below. awk... (3 Replies)
Discussion started by: rocket_dog
3 Replies

8. Shell Programming and Scripting

Removing duplicate field from MARC Record

Hello, I'm new to Perl programming and I have a duplicate 035 tag Voyager application field. The first 035 tag has the information I need but the second 035 tag created the bib id, which I don't need. This incident was performed on several records so I would have to run this script on several... (1 Reply)
Discussion started by: rcnick
1 Replies

9. Shell Programming and Scripting

Removing parts of a specific field

All, I have a field in a comma seperated file with hundreds of lines and about 20 columns and I wish to remove all numbers after the decimal point in field 4 on each line and output the rest to another file or write it back to itself. File is like this 20070126, 123.0, GBP, 1234.5678,... (9 Replies)
Discussion started by: kieranh
9 Replies

10. UNIX for Dummies Questions & Answers

removing last field of the line

I have a text file containing /database/sp/NTR_Vlr_Upload.sql /database/tables/StatsTables.sql /mib/ntr.mib /mib/ntr.v2.mib /scripts/operations/ntr/IMSITracer.ph i want the last field after "/" removed like /database/sp/ /database/tables/ /mib/ /mib/ ... (4 Replies)
Discussion started by: adddy
4 Replies
Login or Register to Ask a Question