Extract value between the delimiters and replace it with another value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract value between the delimiters and replace it with another value
# 1  
Old 05-21-2013
Extract value between the delimiters and replace it with another value

Hi All,

i have file name like below
ABC_065224_123456_123456_your_130413_163005.txt
ABC_065224_123456_MAIN_20130413_163005.txt
ABC_065224_123456_123456_MAIN_130413_163005.txt
ABC_065224_123456_123456_434567_MAIN_130413_163005.txt

i need to find out the number of characters in the filed shown in red and if it is like 6 characters 130413 (YYMMDD) then replece value with 20130413(YYYYMMDD) or num of characters are 8 like 20130413(YYYYMMDD) then keep as is.

it's position is always 2nd from the last.

Thanks in advance,
Siva Santosh
# 2  
Old 05-21-2013
Using awk
Code:
awk -F'_' 'length($(NF-1))!=8{$(NF-1)="20"$(NF-1)}1' OFS='_'

# 3  
Old 05-21-2013
If you want to rename the files as well, we can incorporate Yoda's logic into a script..

Code:
#!/bin/bash
for i in *.txt
do a=`ls $i | awk -F"_" 'length($(NF-1))!=8{$(NF-1)="20"$(NF-1)}{print}' OFS='_'`
mv $i $a
done

If there is no changes in file names, then this script might generate .. are identical message, but this can be neglected.
Hope this helps...
This User Gave Thanks to juzz4fun For This Post:
# 4  
Old 05-21-2013
@juzz4fun, if you are using a for loop, then there is no need to use ls command inside.

That useless of ls command. You can simply use the variable: i
Code:
for i in *.txt
do
   printf "%s" "$i" | awk ..
done

These 2 Users Gave Thanks to Yoda For This Post:
# 5  
Old 05-21-2013
@Yoda: Thanks for the input... just few months old to the world of Unix... Smilie
This User Gave Thanks to juzz4fun For This Post:
# 6  
Old 05-21-2013
Yes..need to replace the file name
# 7  
Old 05-21-2013
You can also use Parameter Substitution in any POSIX conforming shell:
Code:
for file in *.txt
do
        dt=${file%_*}
        dt=${dt##*_}
        if [ ${#dt} -ne 8 ]
        then
                n_dt="20${dt}"
                n_file="${file/$dt/$n_dt}"
                echo mv "$file" "$n_file"
        fi
done

Remove the echo statement if the output looks good.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

find & Replace text using two non-unique delimiters.

I can find and replace text when the delimiters are unique. What I cannot do is replace text using two NON-unique delimiters: Ex., "This html code <text blah >contains <garbage blah blah >. All tags must go,<text > but some must be replaced with <garbage blah blah > without erasing other... (5 Replies)
Discussion started by: bedtime
5 Replies

2. Shell Programming and Scripting

How to replace string between delimiters?

Hello, I would need to replace a delimiter in a flat file using.I would like to replace the semicolon (";") but only if it was contained in a string between quotes. For example: Original flat file example: abc;abc;"abc;abc";cd;"ef;ef";abc aa;bb;"aa";cc;"ddd;eee";ff Desired output:... (9 Replies)
Discussion started by: bartleby
9 Replies

3. Shell Programming and Scripting

Skip the delimiter with in double quotes and count the number of delimiters during data extract

Hi All, I'm stuck-up in finding a way to skip the delimiter which come within double quotes using awk or any other better option. can someone please help me out. Below are the details: Delimited: | Sample data: 742433154|"SYN|THESIS MED CHEM PTY.... (2 Replies)
Discussion started by: BrahmaNaiduA
2 Replies

4. UNIX for Dummies Questions & Answers

Replace Delimiters with Space

Hi All, How to Replace the delimiter for a particular field. I have used awk to replace the field values based on the position, but I tried to remove/replace delimiters with space on particular positions. I tried tr command with combination of awk not sure if this is the correct way, but I am... (3 Replies)
Discussion started by: mora
3 Replies

5. Shell Programming and Scripting

How to Replace string between delimiters?

Basically , i want to delete strings of a particular pattern from the flat file which is " | " pipe delimited. Below are the valid formats : 1) AAA (0) 111-111-111, AAA, BB 2) AAA (0) 111-111-1111;X, AAA, BB original flat file example : |ABC ABC XHAMK|AAA (0) 111-111-111, AAA,... (3 Replies)
Discussion started by: Ravi_007
3 Replies

6. Shell Programming and Scripting

Extract strings within XML file between different delimiters

Good afternoon! I have an XML file from which I want to extract only certain elements contained within each line. The problem is that the format of each line is not exactly the same (though similiar). For example, oa_var will be in each line, however, there may be no value or other... (3 Replies)
Discussion started by: bab@faa
3 Replies

7. Shell Programming and Scripting

sub-string extract between variable delimiters

I need to extract certain pieces from a string, wher delimiters may vary. For example A0 B0 C0 12345677 X0 Y0 Z0 A1-B1 C1 12345678 X1 Y0 Z0 A1/B2 C77 12345679 X2 Y0 Z0 I need to get C0 12345677 X0 C1 12345678 X1 C77 12345679 X2 I tried sed, see example below: echo 'A0 B0... (2 Replies)
Discussion started by: migurus
2 Replies

8. Shell Programming and Scripting

how to extract values b/w two delimiters

Hi, Please help me to extrat values b/w two delimiters. $ echo $abc i want to extract the value 12345 b/w %. (5 Replies)
Discussion started by: tsaravanan
5 Replies

9. UNIX for Advanced & Expert Users

extract text b/w two delimiters

I have an input file which looks like " @$SCRIPT/atp_asrmt_adj.sql $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1005w.pls $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1006w.pls start $SCRIPT/cim1020d.sql;^M spool $DATA/cim1021m.sql @$DATA/cim1021m.sql ! rm $DATA/cim1021m.sql spool $DATA/cim1021m.sql... (6 Replies)
Discussion started by: dowsed4u8
6 Replies

10. Solaris

To extract everything between two delimiters

My input file looks like " @$SCRIPT/atp_asrmt_adj.sql $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1005w.pls $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1006w.pls start $SCRIPT/cim1020d.sql;^M spool $DATA/cim1021m.sql @$DATA/cim1021m.sql ! rm $DATA/cim1021m.sql spool $DATA/cim1021m.sql... (1 Reply)
Discussion started by: dowsed4u8
1 Replies
Login or Register to Ask a Question