# need help in awk command

**URL:** <https://community.unix.com/t/need-help-in-awk-command/293582>\
**Category:** Shell Programming and Scripting\
**Created:** [August 16, 2011, 5:28am UTC](https://community.unix.com/t/need-help-in-awk-command/293582 "2011-08-16T05:28:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![agathaeleanor](https://community.unix.com/letter_avatar/agathaeleanor/32/5_5575768a8748004e209b776fc1b2916d.png) [@agathaeleanor](https://community.unix.com/u/agathaeleanor)\
**Post date:** [August 16, 2011, 5:28am UTC](https://community.unix.com/t/need-help-in-awk-command/293582/1 "2011-08-16T05:28:33Z")

</div>

Hi all gurus,

Need your help in modifying the script written by tukuyomi and shipra\_31

```nohighlight
grep -v '^$' file.txt | nawk -F\| 'FNR==1{n=NF}NF<n{l=$0;getline;$0=l" "$0}1' > file_1.txt

```

This script is replacing all carriage return with space.  
However, i need to embed another condition whereas, in 23th column, if there is a carriage return, will need to replace it with colon \<:::\>

Thanks in advance for your help.

---

<div class="post-metadata">

**Author:** ![shamrock](https://community.unix.com/letter_avatar/shamrock/32/5_5575768a8748004e209b776fc1b2916d.png) [@shamrock](https://community.unix.com/u/shamrock)\
**Post date:** [August 16, 2011, 12:06pm UTC](https://community.unix.com/t/need-help-in-awk-command/293582/2 "2011-08-16T12:06:10Z")

</div>

Can you post a sample of your input file...

---

<div class="post-metadata">

**Author:** ![agathaeleanor](https://community.unix.com/letter_avatar/agathaeleanor/32/5_5575768a8748004e209b776fc1b2916d.png) [@agathaeleanor](https://community.unix.com/u/agathaeleanor)\
**Post date:** [August 16, 2011, 9:15pm UTC](https://community.unix.com/t/need-help-in-awk-command/293582/3 "2011-08-16T21:15:48Z")

</div>

i attached the input(Input.txt) and desired output (Output.txt) file as requested.

Requirement:

1. all carriage return existed in all columns except column 23 will replace with a space (the script that i posted has done this)

2. carriage return existed in column 23 will replace with 3 colon \<:::\>

Thanks.

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/5_5575768a8748004e209b776fc1b2916d.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [August 16, 2011, 10:15pm UTC](https://community.unix.com/t/need-help-in-awk-command/293582/4 "2011-08-16T22:15:36Z")

</div>

```nohighlight
awk '{printf /^[0-9]+\|/||NR==1?RS $0:FS $0}' input.txt

```

---

<div class="post-metadata">

**Author:** ![agathaeleanor](https://community.unix.com/letter_avatar/agathaeleanor/32/5_5575768a8748004e209b776fc1b2916d.png) [@agathaeleanor](https://community.unix.com/u/agathaeleanor)\
**Post date:** [August 16, 2011, 10:33pm UTC](https://community.unix.com/t/need-help-in-awk-command/293582/5 "2011-08-16T22:33:50Z")

</div>

Thanks rdcwayx for your code.

When i tried the code, it hits error.

nawk: syntax error at source line 1  
context is  
{print \>\>\> /^[0-9]+\|/||NR== \<\<\<  
nawk: illegal statement at source line 1

fyi, OS is available for nawk.

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/5_5575768a8748004e209b776fc1b2916d.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [August 16, 2011, 10:46pm UTC](https://community.unix.com/t/need-help-in-awk-command/293582/6 "2011-08-16T22:46:18Z")

</div>

Seems nawk don't support, change to /usr/xpg4/bin/awk

```nohighlight
/usr/xpg4/bin/awk '{printf /^[0-9]+\|/||NR==1?RS $0:FS $0}' input.txt

```

---

<div class="post-metadata">

**Author:** ![agathaeleanor](https://community.unix.com/letter_avatar/agathaeleanor/32/5_5575768a8748004e209b776fc1b2916d.png) [@agathaeleanor](https://community.unix.com/u/agathaeleanor)\
**Post date:** [August 16, 2011, 10:54pm UTC](https://community.unix.com/t/need-help-in-awk-command/293582/7 "2011-08-16T22:54:56Z")

</div>

Sorry for missing out something in the Output.txt  
I have compensated it as attached.

The code works fine, just that in column 23, if there is a carriage return, I need to replace it with 3 colon \<:::\> (as Output.txt)

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/5_5575768a8748004e209b776fc1b2916d.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [August 16, 2011, 11:15pm UTC](https://community.unix.com/t/need-help-in-awk-command/293582/8 "2011-08-16T23:15:58Z")

</div>

```nohighlight
/usr/xpg4/bin/awk '{printf /^[0-9]+\|/||NR==1?RS $0:":::" $0}' input.txt |/usr/xpg4/bin/awk '{for (i=1;i<=NF;i++) if (i!=23) gsub(/:::/,"",$i)}1' FS="|" OFS="|"

```

---

<div class="post-metadata">

**Author:** ![agathaeleanor](https://community.unix.com/letter_avatar/agathaeleanor/32/5_5575768a8748004e209b776fc1b2916d.png) [@agathaeleanor](https://community.unix.com/u/agathaeleanor)\
**Post date:** [August 16, 2011, 11:28pm UTC](https://community.unix.com/t/need-help-in-awk-command/293582/9 "2011-08-16T23:28:35Z")

</div>

Thanks a lot rdcwayx. It works.

I have one last question, when i print the output to file.txt the first line in file.txt is empty row. How to avoid that?
