# awk copy first column in new lines

**URL:** <https://community.unix.com/t/awk-copy-first-column-in-new-lines/370749>\
**Category:** Shell Programming and Scripting\
**Created:** [February 4, 2018, 5:37pm UTC](https://community.unix.com/t/awk-copy-first-column-in-new-lines/370749 "2018-02-04T17:37:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![alii](https://community.unix.com/letter_avatar/alii/32/6_449c533f586939b6e5f246632a46421f.png) [@alii](https://community.unix.com/u/alii)\
**Post date:** [February 4, 2018, 5:37pm UTC](https://community.unix.com/t/awk-copy-first-column-in-new-lines/370749/1 "2018-02-04T17:37:07Z")

</div>

Hi all  
i have table like this

input

```nohighlight
001_1_174 j 474536 482492          
mo[g/n 482492 504062          
'er/ 504062 517352          
ruze 517352 529562                 
001_1_175 j 960192 966656          
.ire 966656 984416          
.uf/[ 984416 1006166   
.
.
         

```

i want copy number of first line to other line until another number and do this for next number

out

```nohighlight
001_1_174 j 474536 482492          
001_1_174 mo[g/n 482492 504062          
001_1_174 'er/ 504062 517352          
001_1_174 ruze 517352 529562                 
001_1_175 j 960192 966656          
001_1_175 .ire 966656 984416          
001_1_175 .uf/[ 984416 1006166   
.
.

```

may explain your codes please?

thank all

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [February 4, 2018, 6:26pm UTC](https://community.unix.com/t/awk-copy-first-column-in-new-lines/370749/2 "2018-02-04T18:26:19Z")

</div>

First let us start with the standard questions that should be answered in your first post in any technical thread:

1. What operating system are you using?
2. What shell are you using?
3. What have you tried to solve this problem on your own?

Then we can move on to details specific to this thread:

1. Numbers do not contain underscore characters. Why do you call `001_1_174` a number?
2. What criteria are to be used to decide if the first column in an input line is numeric?
3. Is the criteria that the second field on a line is the string "j"?
4. Can your numbers have leading plus or minus signs?
5. Can your numbers have decimal points? If so, what is your decimal point character?
6. Can your numbers have thousands separators?
7. Can your numbers have a leading or trailing currency symbol?
8. Where is this table that you have?
9. What is the format of this table?

If you want us to do your work for you, you can at least give us the details needed to figure out what must be done. Note that we are here to help you learn how to write your own code. We are not here to act as your unpaid programming staff.

---

<div class="post-metadata">

**Author:** ![alii](https://community.unix.com/letter_avatar/alii/32/6_449c533f586939b6e5f246632a46421f.png) [@alii](https://community.unix.com/u/alii)\
**Post date:** [February 5, 2018, 1:20am UTC](https://community.unix.com/t/awk-copy-first-column-in-new-lines/370749/3 "2018-02-05T01:20:53Z")

</div>

thanks so mutch , lots of questios.

> [@don cragun](#):
>
> First let us start with the standard questions that should be answered in your first post in any technical thread:
> 
> 1. What operating system are you using?

1. UBUNTU LTS 16.04  
[quote]
2. What shell are you using?  
[/quote]  
bash  
[quote]
3. What have you tried to solve this problem on your own?  
[/quote]  
no i dont know awk well

> [@](#):
>
> Then we can move on to details specific to this thread:
> 
> 1. Numbers do not contain underscore characters. Why do you call `001_1_174` a number?

1. sorry, they are not number but strings just contain numbers and underline\_  
[quote]
2. What criteria are to be used to decide if the first column in an input line is numeric?  
[/quote]  
not numeric,strings just contain numbers and underline\_  
[quote]
3. Is the criteria that the second field on a line is the string "j"?  
[/quote]  
no, second field contain [a-z] and ./`'`][  
[quote]
4. Can your numbers have leading plus or minus signs?  
[/quote]  
no just digits  
[quote]
5. Can your numbers have decimal points? If so, what is your decimal point character?  
[/quote]  
no, numbers just plus digits contain [0-9]  
[quote]
6. Can your numbers have thousands separators?  
[/quote]  
no  
[quote]
7. Can your numbers have a leading or trailing currency symbol?  
[/quote]  
just digits  
[quote]
8. Where is this table that you have?  
[/quote]  
out of a code  
[quote]
9. What is the format of this table?  
[/quote]  
text.

> [@](#):
>
> If you want us to do your work for you, you can at least give us the details needed to figure out what must be done. Note that we are here to help you learn how to write your own code. We are not here to act as your unpaid programming staff.

so glad to explain me. so thanks.:b: this is not commercial code just my Thesis.

how can learn AWK or some text editors?

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 5, 2018, 4:30am UTC](https://community.unix.com/t/awk-copy-first-column-in-new-lines/370749/4 "2018-02-05T04:30:27Z")

</div>

> [@alii](#):
>
> . . .  
> this is not commercial code just my Thesis.

Thanks for being that open. Usually students' home- and coursework questions need to be posted in the in [this forum](https://www.unix.com/homework-and-coursework-questions/) under [special homework rules](https://www.unix.com/homework-coursework-questions/113751-draft-rules-homework-coursework-questions-forum.html). A thesis in a non-IT field I think is a border line case. Did you consider `awk` to check for `NF` (number of fields) equals 4 (save `$1` then) or less than 4 (add saved `$1` to begin-of-line then)?

> [@](#):
>
> how can learn AWK or some text editors?

There's text books on `awk` , and / or the man page. Look into these forums and try to understand the problems and track the solutions proposed. Start creating your own solutions to simple up to more complex problems.

---

<div class="post-metadata">

**Author:** ![alii](https://community.unix.com/letter_avatar/alii/32/6_449c533f586939b6e5f246632a46421f.png) [@alii](https://community.unix.com/u/alii)\
**Post date:** [February 5, 2018, 4:43am UTC](https://community.unix.com/t/awk-copy-first-column-in-new-lines/370749/5 "2018-02-05T04:43:27Z")

</div>

> [@](#):
>
> A thesis in a non-IT field I think is a border line case.

😃  
can't thesis about IT-field?:eek:

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 5, 2018, 5:07am UTC](https://community.unix.com/t/awk-copy-first-column-in-new-lines/370749/6 "2018-02-05T05:07:18Z")

</div>

If your thesis is in an IT related field, please post in the homework forum, filiing in the ENTIRE form. I presume you will correctly cite the source of any solutions you'll find in these forums.

---

<div class="post-metadata">

**Author:** ![alii](https://community.unix.com/letter_avatar/alii/32/6_449c533f586939b6e5f246632a46421f.png) [@alii](https://community.unix.com/u/alii)\
**Post date:** [February 5, 2018, 5:34am UTC](https://community.unix.com/t/awk-copy-first-column-in-new-lines/370749/7 "2018-02-05T05:34:23Z")

</div>

may help me or NOT?

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [February 5, 2018, 5:41am UTC](https://community.unix.com/t/awk-copy-first-column-in-new-lines/370749/8 "2018-02-05T05:41:41Z")

</div>

IF your thesis is in an IT- related field, and IF the hint given in post#4 does not suffice, please repost in the homework forum.

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [February 5, 2018, 5:47am UTC](https://community.unix.com/t/awk-copy-first-column-in-new-lines/370749/9 "2018-02-05T05:47:12Z")

</div>

> [@alii](#):
>
> 😃  
> can't thesis about IT-field?:eek:

Please write your answers as complete sentences.

> [@alii](#):
>
> may help me or NOT?

In what field of study is your research?

What is the title of your thesis?

What degree are you seeking?

Who is your thesis advisor?

At what university are you studying?

Concering your answer to question VIII in post #3: If your table is in "out of a code", how is an `awk` script supposed to be able to read it? Are you saying that you have/will hard code the table into a `BEGIN` clause in your `awk` script?

Concerning your answer to question IX in post #3: If your table was in a file, the type "text" would say that there are no lines in the file containing more than LINE\_MAX bytes (including the line-terminating \<newline\> characters, that the file contains no \<NUL\> bytes, and that if the file contains any characters at all the last character in the file is a \<newline\> character. That says nothing about how fields in your table are separated. It appears that you want the modified lines in your table to be written with the 1st field from the previous line whose first character is numeric followed by a single \<space\> character followed by the previous contents of that line of the table. Is that correct?

If you can't describe how this table exists, we have great difficulty in making any suggestions as to how to process the table. The art of programming is all about understanding the format and type of the input to be processed, the type and format of the output desired, and the rules that specify how the input needs to be transformed to produce the output.

One can learn a lot about `awk` (not `AWK` ; utility names on UNIX, Linux, and BSD systems are case sensitive), editors, and other utilities by reading their manual pages on your system. For example, you can see the manual page for `awk` by typing the command:  
`man awk`  
into your shell at a command prompt.

You can also look at hundreds (if not thousands) of threads in this forum that contain `awk` scripts to solve lots of different kinds of problems. Many of those scripts have comments that document what every line in the script is doing. For those that don't, if the thread is still open you can always and a post to a thread asking for an explanation of how that script works (or how particular lines in that script work). Use the advanced search feature in this forum for threads with tag "solved" and keyword "awk" for a list of 500 threads with examples.

One classic book describing `awk` is The AWK Programming Language published Jan 11, 1988 and written by the three authors of the utility Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan.

---

<div class="post-metadata">

**Author:** ![alii](https://community.unix.com/letter_avatar/alii/32/6_449c533f586939b6e5f246632a46421f.png) [@alii](https://community.unix.com/u/alii)\
**Post date:** [February 5, 2018, 8:27am UTC](https://community.unix.com/t/awk-copy-first-column-in-new-lines/370749/10 "2018-02-05T08:27:49Z")

</div>

Thank you for giving me your time.:b:

> [@don cragun](#):
>
> Please write your answers as complete sentences.
> 
> In what field of study is your research?
> 
> What is the title of your thesis?
> 
> What degree are you seeking?
> 
> Who is your thesis advisor?
> 
> At what university are you studying?

Are these question important to answer my question? you know speech recognition. label speech signal in phone and word by byte of input signal.

> [@](#):
>
> Concering your answer to question VIII in post #3: If your table is in "out of a code", how is an `awk` script supposed to be able to read it? Are you saying that you have/will hard code the table into a `BEGIN` clause in your `awk` script?

you assume that table save in a directory!

> [@](#):
>
> Concerning your answer to question IX in post #3: If your table was in a file, the type "text" would say that there are no lines in the file containing more than LINE\_MAX bytes (including the line-terminating \<newline\> characters, that the file contains no \<NUL\> bytes, and that if the file contains any characters at all the last character in the file is a \<newline\> character. That says nothing about how fields in your table are separated. It appears that you want the modified lines in your table to be written with the 1st field from the previous line whose first character is numeric followed by a single \<space\> character followed by the previous contents of that line of the table. Is that correct?

i think you say true.  
i confuse. explain again.  
i have a table that first line have four columns.  
column one begin with digits and contain digits and underline. column two contain alphabet not digits and columns three and four contain only digits.  
in following it has three columns similar before except first column.(color string)  
i want copy color string in new lines until next new line begin by another (color) string

```plaintext
input
001_1_174 [g/n 474536 482492          
mo[g/n 482492 504062          
'er/ 504062 517352          
ruze 517352 529562                  
001_1_175 uz[`f 960192 966656           
.ire 966656 984416           
tuf/[ 984416 1006166    

```

```plaintext
001_1_174 [g/n 474536 482492           
001_1_174 mo[g/n 482492 504062           
001_1_174 'er/ 504062 517352           
001_1_174 ruze 517352 529562                  
001_1_175 uz[`f 960192 966656           
001_1_175 .ire 966656 984416           
001_1_175 tuf/[ 984416 1006166    

```

> [@](#):
>
> If you can't describe how this table exists, we have great difficulty in making any suggestions as to how to process the table. The art of programming is all about understanding the format and type of the input to be processed, the type and format of the output desired, and the rules that specify how the input needs to be transformed to produce the output.

> [@](#):
>
> One can learn a lot about `awk` (not `AWK` ; utility names on UNIX, Linux, and BSD systems are case sensitive), editors, and other utilities by reading their manual pages on your system. For example, you can see the manual page for `awk` by typing the command:
> 
> ```plaintext
> man awk
> into your shell at a command prompt.
> 
> You can also look at hundreds (if not thousands) of threads in this forum that contain awk
> 
> ```
> 
> scripts to solve lots of different kinds of problems. Many of those scripts have comments that document what every line in the script is doing. For those that don't, if the thread is still open you can always and a post to a thread asking for an explanation of how that script works (or how particular lines in that script work). Use the advanced search feature in this forum for threads with tag "solved" and keyword "awk" for a list of 500 threads with examples.
> 
> One classic book describing `awk` is The AWK Programming Language published Jan 11, 1988 and written by the three authors of the utility Alfred V. Aho, Peter J. Weinberger, and Brian W. Kernighan.

thanks:b:

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [February 5, 2018, 9:54am UTC](https://community.unix.com/t/awk-copy-first-column-in-new-lines/370749/12 "2018-02-05T09:54:44Z")

</div>

I don't ask questions for the fun of asking questions. I was trying to get you to directly answer the questions needed to determine whether or not you are seeking a degree in information technology. Since you are unwilling to answer the questions of what university you're attending and who your thesis advisor is, we have to assume that you are asking us to do your homework for you and it is important for you to hide this information from us.

Homework and coursework questions can only be posted in [the Homework & Coursework Questions forum](https://www.unix.com/homework-coursework-questions/) under [special homework rules](https://www.unix.com/homework-coursework-questions/113751-draft-rules-homework-coursework-questions-forum.html).

Please review the [rules](https://www.unix.com/misc.php?do=cfrules), which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulted in a forum infraction.

For the record, the last thing I would assume is that you stored your table in a file of type directory. I assumed that you would have stored your table in:

- a single shell variable,
- a single shell array variable (with each element of the array being a line n your table),
- in three or four shell array variables (with each array being a column in your table),
- a regular file (which would be a preferred choice),
- a shared memory segment,
- in a pipe (or named FIFO) written to by a process that is creating the data in your table (which would also be a preferred choice),
- hardcoded into an `awk` , `bc` , `dc` , `ed` , `ex` , or `sed` script, or
- any of several less common ways of storing data.

Your refusal to tell us how your table is stored and in what form you want the output to be produced makes it impossible for us to even guess at what tool should be used to process your data.

This thread is closed.
