How to create script

Hello everyone,
I am a new member,
I need help: scripting in linux. The following content:
cat file.txt

10.111.111.111
2 gi1/0/1 adcd.abcd.cdfe
4 gi1/0/2 abcd.safa.awrf
2 gi1/0/3 gseg.egew.wqfq
10.111.111.112
2 gi1/0/1 afga.gwet.aqgq
4 gi1/0/2 asgf.qwfg.gvqr
10.111.111.110
2 gi1/0/1 afga.gwet.aqgd
10.111.111.99
4 gi0/28 afga.gwet.aqge

--------------->
output:

2 gi1/0/1 adcd.abcd.cdfe 10.111.111.111
4 gi1/0/2 abcd.safa.awrf 10.111.111.111
2 gi1/0/3 gseg.egew.wqfq 10.111.111.111
2 gi1/0/1 afga.gwet.aqgq 10.111.111.112
4 gi1/0/2 asgf.qwfg.gvqr 10111.111.112
2 gi1/0/1 afga.gwet.aqgd 10.111.111.110
4 gi0/28 afga.gwet.aqge 10.111.111.99

Pls help me,
Thank all!

Please wrap the output chapters between triple-backticks!
This time I have done it for you.

The strategy is to read line by line.
And find a criterion for a line like 10.111.111.111.
This is to be stored, and appended when printing the following lines.

What do you suggest as a criterion?
Please show your first attempt!

2 Likes

Thank you!,
I try: paste -sd" " file.txt
and : awk '{printf "%s%s", (NR>1 ? " " : ""), $0} END{print ""}' file.txt
Result: 10.111.111.111 2 gi1/0/1 adcd.abcd.cdfe 4 gi1/0/2 abcd.safa.awrf 2 gi1/0/3 gseg.egew.wqfq 10.111.111.112 2 gi1/0/1 afga.gwet.aqgq 4 gi1/0/2 asgf.qwfg.gvqr 10.111.111.110 2 gi1/0/1 afga.gwet.aqgd 10.111.111.99 4 gi0/28 afga.gwet.aqge
But I want more than that. Can you guide me?
Thank you very much!

The paste command can only do the same action on all lines.
But you want to treat a line like 10.111.111.111 differently.
Start with grep ing these special lines!

I'm a beginner, so there are a lot of limitations.
Can you give more detailed instructions?
Thank you !

Use the grep command! Never used it before?

I have used the command: 'grep' but I don't know how to have a script with all the information I need. Can you create a script for me?
Thank you!

A criterion can be: a space character.
grep " " file.txt
That shows one type of lines (where an item is to be appended)
grep -v " " file.txt
shows the other lines (the item).

You can use the same in an awk script:
awk '/ / { print $0 }' file.txt
awk '!/ / { print $0 }' file.txt

Here it seems to be better to test the Number of Fields (NF):
awk 'NF > 1 { print $0 }' file.txt
awk 'NF == 1 { print $0 }' file.txt

Now, for the one type, you store the line in a variable, and don't print it.
And for the other type, you print the line along with the variable.
The variable is within awk, so you need one invocation of awk.

2 Likes

Thank you for your guidance.

  • this command: grep " " file.txt
    -> result:
    2 gi1/0/1 adcd.abcd.cdfe
    4 gi1/0/2 abcd.safa.awrf
    2 gi1/0/3 gseg.egew.wqfq
    2 gi1/0/1 afga.gwet.aqgq
    4 gi1/0/2 asgf.qwfg.gvqr
    2 gi1/0/1 afga.gwet.aqgd
    4 gi/28 afga.gwet.aqge
  • this command: grep -v " " file.txt
    -> result:
    10.111.111.111
    10.111.111.112
    10.111.111.110
    10.111.111.99
    But I don't know how to get it right with my desired format like this:
    --------------->
    output:
    2 gi1/0/1 adcd.abcd.cdfe 10.111.111.111
    4 gi1/0/2 abcd.safa.awrf 10.111.111.111
    2 gi1/0/3 gseg.egew.wqfq 10.111.111.111
    2 gi1/0/1 afga.gwet.aqgq 10.111.111.112
    4 gi1/0/2 asgf.qwfg.gvqr 10111.111.112
    2 gi1/0/1 afga.gwet.aqgd 10.111.111.110
    4 gi0/28 afga.gwet.aqge 10.111.111.99

If possible, please help me with a complete script.
Thank you very much!

Please take your time to walk through my awk samples! (I have added the missing endings.)
Another one:

awk 'NF==1 { print "(store this in a variable)" }; NF > 1 { print $0, "stored_value" }' file.txt
1 Like

I'm learning the way you told me. But maybe it takes more time, because I'm a beginner. Thank you very much.

Make sure to read some important stuff in awk manual. You'll have to read some sections quite carefully and experiment a lot, but I bet it will be a great usage of your time to solve this issue.
Make sure you understand:

  1. Filters
  2. Variable assignments and reference
  3. Formatted printing
  4. @MadeInGermany first proposed strategy
  5. @MadeInGermany triple-backticks instruction

Then bring your attempts and results.