0

I have two files. One being a schema, and the other a list of data. The schema looks like:

line_name:
shot_edit:
channel edit:
cable_edit

and repeats regularly for the number of lines that I have.

The other file is a list of line names and goes:

name1
name2
name3

I'd like to copy all the line names into just after the line_name field, but I'm really not all that good with awk. Could anyone give me some pointers?

1
  • 4
    This is not very clear. Could you give an example of your desired output? Do you want line_name: name1 name2 name3 or do you want something more complex? How many lines more or less? Does cable_edit not have a :? What are the names? Do they contain spaces?
    – terdon
    Dec 30, 2013 at 16:48

3 Answers 3

1

Both of these commands give the following output

sed '/line_name/ r linenames' schema
awk '{print} /line_name/ {while (getline < "linenames") print}' schema
line_name:
name1
name2
name3
shot_edit:
channel edit:
cable_edit
0

This command insert each line of the second file (linenames) after each first line in the file schema

  awk '{print} /line_name/ { getline < "linenames" ; print}' schema 

with this output

line_name:
name1
shot_edit:
channel edit:
cable_edit
line_name:
name2
shot_edit:
channel edit:
cable_edit
....

This command instead

awk '{print} /line_name/ { getline < "linenames" ; print}' schema | sed ':a;N;$!ba;s/line_name:\n/line_name: /g'

Insert the name just after the tag line_name:

line_name: name1
shot_edit:
channel edit:
cable_edit
line_name: name2
shot_edit:
channel edit:
cable_edit
line_name: name3
...
0

If you want to insert in the same line with line_name:

$ awk '/line_name/{getline name < "linenames";$0 = $0" "name}1' schema
line_name: name1
shot_edit:
channel edit:
cable_edit
line_name: name2
shot_edit:
channel edit:
cable_edit
line_name: name3
shot_edit:
channel edit:
cable_edit
1
  • @ashumeow I believe it would be more clear if you posted your variant of the code separately; then we could decide what the differences between the proposed variants are. I don't believe that editing this suggested code is a good idea, because we might break the intented behavior (intended by he author of the answer). May 10, 2014 at 22:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .