# Appending the first word of each line to the end of each line

**URL:** <https://community.unix.com/t/appending-the-first-word-of-each-line-to-the-end-of-each-line/262008>\
**Category:** Shell Programming and Scripting\
**Created:** [March 18, 2010, 10:51am UTC](https://community.unix.com/t/appending-the-first-word-of-each-line-to-the-end-of-each-line/262008 "2010-03-18T10:51:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hj007](https://community.unix.com/user_avatar/community.unix.com/hj007/32/5877_2.png) [@hj007](https://community.unix.com/u/hj007)\
**Post date:** [March 18, 2010, 10:51am UTC](https://community.unix.com/t/appending-the-first-word-of-each-line-to-the-end-of-each-line/262008/1 "2010-03-18T10:51:16Z")

</div>

Hi Experts,

Am relatively new to shell programming so would appreciate some help in this regard.  
I am looking at reading from a file, line by line, picking the first word of each line and appending it to the end of the line.

Any suggestions?

INPUT FILE -

```nohighlight
3735051 : 10gdb_nonfa_noldapsync_ps2 : http://dte.com/dte30/faces/monitorPage/jobId=
3735052 : 11gdb_nonfa_noldapsync_ps2 : http://dte.com/dte30/faces/monitorPage/jobId=
3735053 : 112gdb_nonfa_noldapsync_ps2 : http://dte.com/dte30/faces/monitorPage/jobId=

```

This is how id like my output file to look like -

```nohighlight
3735051 : 10gdb_nonfa_noldapsync_ps2 : http://dte.com/dte30/faces/monitorPage/jobId=3735051
3735052 : 11gdb_nonfa_noldapsync_ps2 : http://dte.com/dte30/faces/monitorPage/jobId=3735052
3735053 : 112gdb_nonfa_noldapsync_ps2 : http://dte.com/dte30/faces/monitorPage/jobId=3735053

```

TIA

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [March 18, 2010, 10:58am UTC](https://community.unix.com/t/appending-the-first-word-of-each-line-to-the-end-of-each-line/262008/2 "2010-03-18T10:58:47Z")

</div>

Use code tags next time please. If not sure what I am talking about, leave me a note.

To your problem:

```nohighlight
awk '{print $0$1}' infile

```

---

<div class="post-metadata">

**Author:** ![hj007](https://community.unix.com/user_avatar/community.unix.com/hj007/32/5877_2.png) [@hj007](https://community.unix.com/u/hj007)\
**Post date:** [March 18, 2010, 12:09pm UTC](https://community.unix.com/t/appending-the-first-word-of-each-line-to-the-end-of-each-line/262008/3 "2010-03-18T12:09:24Z")

</div>

This works well, thanks !!  
My bad about the code tags, will keep in mind in future !

---

<div class="post-metadata">

**Author:** ![durden\_tyler](https://community.unix.com/user_avatar/community.unix.com/durden_tyler/32/1450_2.png) [@durden\_tyler](https://community.unix.com/u/durden_tyler)\
**Post date:** [March 18, 2010, 7:06pm UTC](https://community.unix.com/t/appending-the-first-word-of-each-line-to-the-end-of-each-line/262008/4 "2010-03-18T19:06:27Z")

</div>

And yet another -

```nohighlight
perl -lane 'print $_,$F[0]' yourfile

```

tyler\_durden

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/5_5575768a8748004e209b776fc1b2916d.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [March 18, 2010, 7:18pm UTC](https://community.unix.com/t/appending-the-first-word-of-each-line-to-the-end-of-each-line/262008/5 "2010-03-18T19:18:53Z")

</div>

... and another 🙂

```nohighlight
sed 's/^\([^]*\).*/&\1/' file

```

Cheers,  
Alister

---

<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:** [March 18, 2010, 7:56pm UTC](https://community.unix.com/t/appending-the-first-word-of-each-line-to-the-end-of-each-line/262008/6 "2010-03-18T19:56:45Z")

</div>

```nohighlight
awk '$0=$0$1' urfile

```

---

<div class="post-metadata">

**Author:** ![kurumi](https://community.unix.com/letter_avatar/kurumi/32/5_5575768a8748004e209b776fc1b2916d.png) [@kurumi](https://community.unix.com/u/kurumi)\
**Post date:** [March 23, 2010, 1:39am UTC](https://community.unix.com/t/appending-the-first-word-of-each-line-to-the-end-of-each-line/262008/7 "2010-03-23T01:39:48Z")

</div>

```nohighlight
sed 's/\(.[^:]*\)\(.*\)/\1\2\1/' file

```

---

<div class="post-metadata">

**Author:** ![summer\_cherry](https://community.unix.com/letter_avatar/summer_cherry/32/5_5575768a8748004e209b776fc1b2916d.png) [@summer\_cherry](https://community.unix.com/u/summer_cherry)\
**Post date:** [March 23, 2010, 11:30pm UTC](https://community.unix.com/t/appending-the-first-word-of-each-line-to-the-end-of-each-line/262008/8 "2010-03-23T23:30:11Z")

</div>

```nohighlight
awk 'BEGIN{FS=OFS=":"}
        {
        $NF=sprintf("%s%s",$NF,$1)
        print
        }' 

```
