# Help needed in replace

**URL:** <https://community.unix.com/t/help-needed-in-replace/214314>\
**Category:** Shell Programming and Scripting\
**Created:** [September 28, 2008, 2:17pm UTC](https://community.unix.com/t/help-needed-in-replace/214314 "2008-09-28T14:17:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsravan](https://community.unix.com/letter_avatar/dsravan/32/6_449c533f586939b6e5f246632a46421f.png) [@dsravan](https://community.unix.com/u/dsravan)\
**Post date:** [September 28, 2008, 2:17pm UTC](https://community.unix.com/t/help-needed-in-replace/214314/1 "2008-09-28T14:17:06Z")

</div>

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.

> [@](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![neked](https://community.unix.com/user_avatar/community.unix.com/neked/32/679_2.png) [@neked](https://community.unix.com/u/neked)\
**Post date:** [September 28, 2008, 2:59pm UTC](https://community.unix.com/t/help-needed-in-replace/214314/2 "2008-09-28T14:59:07Z")

</div>

```nohighlight
sed 's/\([^|]\+|[^]\+ \)[^|]\+\(.*\)/\1SOME_VALUE\2/' input_file

```

where SOME\_VALUE is your intended value.

```nohighlight
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||

```

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [September 28, 2008, 3:14pm UTC](https://community.unix.com/t/help-needed-in-replace/214314/3 "2008-09-28T15:14:11Z")

</div>

With awk:

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

```

Regards

---

<div class="post-metadata">

**Author:** ![dsravan](https://community.unix.com/letter_avatar/dsravan/32/6_449c533f586939b6e5f246632a46421f.png) [@dsravan](https://community.unix.com/u/dsravan)\
**Post date:** [September 28, 2008, 3:17pm UTC](https://community.unix.com/t/help-needed-in-replace/214314/4 "2008-09-28T15:17:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![neked](https://community.unix.com/user_avatar/community.unix.com/neked/32/679_2.png) [@neked](https://community.unix.com/u/neked)\
**Post date:** [September 28, 2008, 3:18pm UTC](https://community.unix.com/t/help-needed-in-replace/214314/5 "2008-09-28T15:18:57Z")

</div>

> [@franklin52](#):
>
> With awk:
> 
> ```plaintext
> 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...

---

<div class="post-metadata">

**Author:** ![neked](https://community.unix.com/user_avatar/community.unix.com/neked/32/679_2.png) [@neked](https://community.unix.com/u/neked)\
**Post date:** [September 28, 2008, 3:20pm UTC](https://community.unix.com/t/help-needed-in-replace/214314/6 "2008-09-28T15:20:44Z")

</div>

> [@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:

```plaintext
awk 'BEGIN{FS=OFS="|"}{$2="NEW_VALUE"}1' test

```

---

<div class="post-metadata">

**Author:** ![dsravan](https://community.unix.com/letter_avatar/dsravan/32/6_449c533f586939b6e5f246632a46421f.png) [@dsravan](https://community.unix.com/u/dsravan)\
**Post date:** [September 28, 2008, 3:27pm UTC](https://community.unix.com/t/help-needed-in-replace/214314/7 "2008-09-28T15:27:55Z")

</div>

Thanks So much Neked and Franklin.

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [September 28, 2008, 3:30pm UTC](https://community.unix.com/t/help-needed-in-replace/214314/8 "2008-09-28T15:30:19Z")

</div>

> [@neked](#):
>
> 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...

awk evaluates the 1 as true and the prints the entire line by default, similar to {print $0}.

Regards
