Help needed in replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed in replace
# 1  
Old 09-28-2008
Help needed in replace

Hi,

I have a pipe delimited file. I need to change the timestamp in the second field in all the records to a specific value. can you please let me know how i can do it?

sample file with 2 reocrds. The time i need to change is in red below.

Quote:
234876119|2008-09-23 10:29:31|9999-12-31|1012|1089|I|1|2008-09-23 10:29:31.000336||
234876121|2008-09-24 11:31:01|9999-12-31|1012|1089|I|1|2008-09-24 11:31:01.000337||
Please let me know how I can change only the time in the second field for all the records in the file.
# 2  
Old 09-28-2008
Code:
sed 's/\([^|]\+|[^ ]\+ \)[^|]\+\(.*\)/\1SOME_VALUE\2/' input_file

where SOME_VALUE is your intended value.

Code:
neked@mercurial:~$ echo "234876119|2008-09-23 10:29:31|9999-12-31|1012|1089|I|1|2008-09-23 10:29:31.000336||" \
> | sed 's/\([^|]\+|[^ ]\+ \)[^|]\+\(.*\)/\1SOME_VALUE\2/'

234876119|2008-09-23 SOME_VALUE|9999-12-31|1012|1089|I|1|2008-09-23 10:29:31.000336||

# 3  
Old 09-28-2008
With awk:

Code:
awk 'BEGIN{FS=OFS="|"}{split($2,s," ");$2=s[1]" NEW_VALUE"}1' file

Regards
# 4  
Old 09-28-2008
Thanks Franklin. This works like magic. But if I want to replace the whole field with a different value what would be the syntax?

Neked,

For some reason the code doesnt seem to work for me. Thanks though.
# 5  
Old 09-28-2008
Quote:
Originally Posted by Franklin52
With awk:

Code:
awk 'BEGIN{FS=OFS="|"}{split($2,s," ");$2=s[1]" NEW_VALUE"}1' file

Regards
That's a pretty cool implementation, but one question to you, what does the '1' at the end of the awk directive do? I notice your implementation will not work without it...
# 6  
Old 09-28-2008
Quote:
Originally Posted by dsravan
Thanks Franklin. This works like magic. But if I want to replace the whole field with a different value what would be the syntax?
Using Franklin's technique:

Code:
awk 'BEGIN{FS=OFS="|"}{$2="NEW_VALUE"}1' test

# 7  
Old 09-28-2008
Thanks So much Neked and Franklin.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace char between chars - help needed

Hello, I have a csv file with "^" as text delimiters and "|" as field delimiters. It's converted from a xls file. One record looks like this: ^Tablete Internet^|Archos|501838|^Tableta Internet ARCHOS 80 G9 ...| ... (more lines) ... "501|838"^|330.00|USD|sl|12|0|Link|^router wireless 150... (10 Replies)
Discussion started by: go0ogl3
10 Replies

2. Shell Programming and Scripting

sed replace after pattern help needed

Hello, I have a file with multiple lines like this: /film/4295/"_class="titre_article">50/50I would like to change all occurence of / after > with _ to have this: /film/4295/"_class="titre_article">50_50Thank you edit: This could also be change all / starting with the 4th occurrence... (2 Replies)
Discussion started by: patx
2 Replies

3. Shell Programming and Scripting

Search for a pattern and replace. Help needed

I have three variables $a, $b and $c $a = file_abc_123.txt $b = 123 $c = 100 I want to search if $b is present in $a. If it is present, then i want to replace that portion by $c. Here $b = 123 is present in "file_abc_123.txt", so i need the output as "file_abc_100.txt' How can this be... (3 Replies)
Discussion started by: irudayaraj
3 Replies

4. Shell Programming and Scripting

Urgent help needed !!!....to replace a exact string

Hi experts, As i am a novice unix player...so need help for the below query...banged my head from quite a while...:confused: i have a set of html files, in which i need to search for string "Page"(case sensitive) and then replace the same with some numeric code ,say, "XXX1234". Here in... (1 Reply)
Discussion started by: rahulfhp
1 Replies

5. UNIX for Advanced & Expert Users

Assistance Needed With Find/Replace in Vi

Hello All I have always had a question about find and replace in Vi. As this uses Vi, sed, and RegEx I never knew how or where to post the question but I thought I would give it a shot here. Say I have a text file filled with the following: Sue, your IP address is 192.168.1.10 which is... (4 Replies)
Discussion started by: NoSalt
4 Replies

6. Shell Programming and Scripting

Complex Search/Replace Multiple Files Script Needed

I have a rather complicated search and replace I need to do among several dozen files and over a hundred occurrences. My site is written in PHP and throughout the old code, you will find things like die("Operation Aborted due to....."); For my new design skins for the site, I need to get... (2 Replies)
Discussion started by: UCCCC
2 Replies

7. Shell Programming and Scripting

Help needed using sed to replace a url in 1000's of web pages

Hi, I'm new to scripting. I understand the concepts and syntax of some commands but have difficulty with others and combining actions to achieve what I'm trying to do so hope someone on here can help. A long while back I inherited a website with 1000's of pages most of which were created by a... (2 Replies)
Discussion started by: bob_from_brid
2 Replies

8. UNIX for Advanced & Expert Users

search a replace each line- help needed ASAP

can someone help me with the find and replace command. I have a input file which is in the below format: 0011200ALN00000000009EGYPT 000000000000199900000 0011200ALN00000000009EGYPT 000000000000199900000 0011200ALN00000000008EGYPT 000000000000199800000 0011200ALN00000000009EGYPT ... (20 Replies)
Discussion started by: bsandeep_80
20 Replies

9. Shell Programming and Scripting

awk: find and replace in certain field only, help needed

I got a sample file like this. $ cat test 12|13|100|s 12|13|100|s 100|13|100|s 12|13|100|s I want to replace all 100 by 2000 only in 3rd field using "awk" This is replacing all 100's :-( $ awk -F "|" '{gsub( /100/,"2000");print}' test 12|13|2000|s 12|13|2000|s 2000|13|2000|s... (5 Replies)
Discussion started by: jkl_jkl
5 Replies

10. UNIX for Dummies Questions & Answers

Find and Replace code help needed

I have a file "dbshot.xml" that contains lines that need replacing in a batch format but the parameters are based on two lines. Ex. <role roletype="01"> <status>1 needs to be changed to <role roletype="01"> <status>0 I can't use simply "<status>1" replace since the... (2 Replies)
Discussion started by: E Orgill
2 Replies
Login or Register to Ask a Question