Change Position of word character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Change Position of word character
# 1  
Old 03-12-2007
Change Position of word character

Hi,

I have following format in file aaa with content below, and would like to seek help from forumer about how to change and swap the position on 2nd field.

5874957|901125|
95874960|650614|
95874966|870308|

901125 to be changed as 25-11-1990 for eg

Can someone help please ?? Smilie

Regards,
# 2  
Old 03-12-2007
Quote:
Originally Posted by cedrichiu
Hi,

I have following format in file aaa with content below, and would like to seek help from forumer about how to change and swap the position on 2nd field.

5874957|901125|
95874960|650614|
95874966|870308|

901125 to be changed as 25-11-1990 for eg

Can someone help please ?? Smilie

Regards,
The change from 901125 to 25-11-1990 results in a valid date. What happens if you have 040101 ? Would it become 01-01-2004 or 01-01-1904 ?
# 3  
Old 03-12-2007
Quote:
Originally Posted by vino
The change from 901125 to 25-11-1990 results in a valid date. What happens if you have 040101 ? Would it become 01-01-2004 or 01-01-1904 ?
Using current century,

Code:
date +%C

should be ideal

anyhow OP should clarify
# 4  
Old 03-12-2007
based on the above assumption,

Code:
awk -F"|" -v var=`date +%C` '{ printf "%d|%d-%d-%d%d|\n", $1, substr($2, length($2) - 1, 2), substr($2, length($2) - 3, 2), var, substr($2, 0, 2) }' aaa

# 5  
Old 03-12-2007
Plz Give a try on this.

sed 's/\(.*|\)\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)\(|\)/\1\4-\3-19\2\6/' filename

Note:This will change 04 to 1904,not 2004

Thnx.
Dennis
# 6  
Old 03-12-2007
Quote:
Originally Posted by jacoden
Plz Give a try on this.

sed 's/\(.*|\)\([0-9][0-9]\)\([0-9][0-9]\)\([0-9][0-9]\)\(|\)/\1\4-\3-19\2\6/' filename

Note:This will change 04 to 1904,not 2004

Thnx.
Dennis
.* results in a greedy match. If there are more 2 delimiters, then it would pick up the last field.

Your sed statement should look like

Code:
sed -e "s/\([^|].*|\)\(..\)\(..\)\(..\)/\1\4-\3-$(date +%C)\2/g" in.txt

# 7  
Old 03-12-2007
if you have Python and you know the language, here's an alternative
Code:
#!/usr/bin/python
# assumption: doesn't take care of dates before 1970.
import time
for line in open("file"):
    line=line.strip().split("|")
    first,second = line[0],line[1]
    t = time.strptime(second , "%y%m%d")
    thedate = time.strftime("%d-%m-%Y",t)
    print "%s|%s" % (first,thedate)

output:
Code:
# ./test.py
5874957|25-11-1990
95874960|14-06-2065
95874966|08-03-1987

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

2. Shell Programming and Scripting

Position of character in word

How can I represent the position of 1 (considering only the 1s after the colon) in the word from field5 and above; counting from right to left. Input: TT-124 06-03-14 08-02-10 FAS CAT1:10 TT-125-1 05-03-14 10-06-08 CAS CAT2:1010 FAT1:10000 TT-125-3 07-03-14 11-02-06 FAS FAT1:1101... (6 Replies)
Discussion started by: aydj
6 Replies

3. Shell Programming and Scripting

Grep the 5th and 6th position character of a word in a file

I am trying to find/grep the 5th and 6th position character (TX) of a word in a file. I tried to do grep "....TX" file The output gives me everything in the file with TX in it. I only need the output with the TX in the 5th and 6th position of the word. Any idea Example: test1 car... (5 Replies)
Discussion started by: e_mikey_2000
5 Replies

4. Shell Programming and Scripting

Find character and Replace character for given position

Hi, i want find the character '-' in a file from position 284-298, if it occurs i need to replace it with 'O ' for the position in the file. How to do that using SED command. thanks in advance, Sara (9 Replies)
Discussion started by: Sara183
9 Replies

5. UNIX for Dummies Questions & Answers

Change a character based on its position number

Hi I have a text file that I want to change some of the characters based on their position. My file contain multiple lines and characters should be counted continuously line by line. For example, I want to convert the 150th T to C. What can I do? Here is a portion of my file:... (10 Replies)
Discussion started by: a_bahreini
10 Replies

6. Shell Programming and Scripting

Identify the position of character

Hi, Can some one guide me to identify the position of a character using index in UNIX. I have a record like "17/11/2010 15:16:39;reject;10.44.48.65;daemon alert; src: 10.44.48.112; dst: 172.21.52.88" . I need to identify the value which comes after _src:_ (_ denotes space). I am able to... (15 Replies)
Discussion started by: suneel.mekala
15 Replies

7. Shell Programming and Scripting

Command to parse a word character by character

Hi All, Can anyone help me please, I have a word like below. 6,76 I want to read this word and check if it has "," (comma) and if yes then i want to replace it with "." (dot). That means i want to be changed to 6.76 If the word doesnot contain "," then its should not be changed. Eg. ... (6 Replies)
Discussion started by: girish.raos
6 Replies

8. Shell Programming and Scripting

find a word in a file, and change a word beneath it ??

Hi all, I have a file with lines written somewhat like this. aaaa ccc aa linux browse = no xssxw cdcedc dcsdcd csdw police dwed dwd browse = no cdecec (2 Replies)
Discussion started by: vikas027
2 Replies

9. Shell Programming and Scripting

How to check a word position in a file ?

Hello everybody, I have a file like this : "window 1 truck 3 duck 2... fire 1... etc..." and I would like to print the following number of a word I am searching for. (For example here, if I search for the word "fire", I will print "1") Thank you for your help ! (7 Replies)
Discussion started by: tibo
7 Replies

10. UNIX for Dummies Questions & Answers

Character position

Hi , I am required to view the fixed postion file very often . I am looking for the utility like this if the file has a one or multile line abcdefghijklmnopqr Utility should make my file look like this 12345678910111213141516-------------------------- abcdefghijk l m n o p q r ... (4 Replies)
Discussion started by: akrathi
4 Replies
Login or Register to Ask a Question