Bringing together words from multiple lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bringing together words from multiple lines
# 1  
Old 06-26-2012
Bringing together words from multiple lines

I want to get a file together that lists router name and IP address of an interface together like so...

Code:
SOMERTR1A    10.10.10.20
SOMERTR1B    10.10.10.30
OTHRRTR1A    192.168.1.120


The file I'm trying to extract the text from looks like the below:

Code:
SOMERTR1A#show run int Lo0 | i add
 ip address 10.10.10.20 255.255.255.255
SOMERTR1B#show run int Lo0 | i add
 ip address 10.10.10.30 255.255.255.255
OTHRRTR1A#show run int Lo0 | i add
 ip address 192.168.1.120 255.255.255.255

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.



I believe this will take using sed and/or awk in some way but am not certain. I have very limited working knowledge of both and am trying to practice using them more with examples like the above. I've searched briefly tonight but thought I would throw up a post before going to bed and picking this up tomorrow.

Last edited by radoulov; 06-26-2012 at 09:21 AM..
# 2  
Old 06-26-2012
Try..

Code:
awk -F "[ #]" /#show/'{router=$1} /^ip address/ {print router,$3}'  file

Output:

Code:
SOMERTR1A 10.10.10.20
SOMERTR1B 10.10.10.30
OTHRRTR1A 192.168.1.120

Please note this works only if the patterns are exactly same as you posted in the sample.
# 3  
Old 06-26-2012
Code:
$ awk '/#/{split($1,a,"#");r=a[1]}/ip address/{print a[1],$3}' a.txt 
SOMERTR1A 10.10.10.20
SOMERTR1B 10.10.10.30
OTHRRTR1A 192.168.1.120

These 2 Users Gave Thanks to itkamaraj For This Post:
# 4  
Old 06-26-2012
Code:
sed 'N;s/#.*address\|255.255.255.255//g' infile

# 5  
Old 06-27-2012
Thanks for the replies. I tried all three and itkamaraj's solution worked!

clx - when attempting your solution, I received the following result:
Code:
awk: syntax error near line 1
  awk: bailing out near line 1

Either it wasn't quite right or perhaps the example strings I provided, on which you used to provide your suggestion, were not a close enough resemblance to the actual file. Either way, thanks for the response as it gives me another way to look at it and maybe play around a bit. I am just about following along close enough to understand what your suggestion was attempting to do.

huaihaizi3 - your solution seems to have printed out the entire file as if I used "cat filename". Thanks to you as well. I don't understand sed really at all yet but will hopefully start spending a bit more time with it here shortly.

Last edited by radoulov; 06-27-2012 at 06:18 AM..
# 6  
Old 06-27-2012
On Solaris use /usr/xpg4/bin/awk rather than awk
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed parser behaving strange on replacing multiple words in multiple files

I have 4000 files like $cat clus_grp_seq10_g.phy 18 1002 anig_OJJ65951_1 ATGGTTTCGCAGCGTGATAGAGAATTGTTTAGGGATGATATTCGCTCGCGAGGAACGAAGCTCAATGCTGCCGAGCGCGAGAGTCTGCTAAGGCCATATCTGCCAGATCCGTCTGACCTTCCACGCAGGCCACTTCAGCGGCGCAAGAAGGTTCCTCG aver_OOF92921_1 ... (1 Reply)
Discussion started by: sammy777888
1 Replies

2. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

3. Shell Programming and Scripting

How to replace multiple words together?

I am looking for any smart perl solution for multiple word replacement together. My awk is working but for big files it is superslow. awk 'NR==FNR {a=$2;next} {for ( i in a) gsub(i,a)}1' new_word_list.txt oldfile > newfile new_word_list.txt looks like AB733 ST756 AB734 ST219 AB11 ... (4 Replies)
Discussion started by: sammy777888
4 Replies

4. Shell Programming and Scripting

How to sort lines according words?

Hello I greped some lines from an xml file and generated a new file. but some entries are missing my table is unsorted. e.g. NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333" NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333" NAME="Adel" NUMBER="2" POSTCODE="33333"... (5 Replies)
Discussion started by: witchblade
5 Replies

5. UNIX for Dummies Questions & Answers

Extract lines with specific words with addition 2 lines before and after

Dear all, Greetings. I would like to ask for your help to extract lines with specific words in addition 2 lines before and after these lines by using awk or sed. For example, the input file is: 1 ak1 abc1.0 1 ak2 abc1.0 1 ak3 abc1.0 1 ak4 abc1.0 1 ak5 abc1.1 1 ak6 abc1.1 1 ak7... (7 Replies)
Discussion started by: Amanda Low
7 Replies

6. UNIX Desktop Questions & Answers

Display a specific words from a multiple lines

well, i am so not familiar with this kind of things but i am gonna explain extactly what i am looking for so hopfully someone can figure it out :) i have a command that shows memory usage besides the process name, for example(the command output): 500 kb process_1 600 kb process_2 700 kb... (4 Replies)
Discussion started by: Portabello
4 Replies

7. Shell Programming and Scripting

Count the no of lines between two words

Please help in the following problem: Input is: Pritam 123 456 Patil myname youname Pritam myproject thisproject iclic Patil remaining text some more text I need the command which will display the no of lines between two words in the whole file. e.g. Display all the no of lines... (5 Replies)
Discussion started by: zsudarshan
5 Replies

8. Shell Programming and Scripting

how to remove words between /* and */ in several lines of file

hi, I want to remove comments in somany files. coments started from /* and ends with */. so i wnt to remove words /* to */ how to remove words between /* and */ in several lines of file EX : cat haha.txt "HI",/*hk*/ob1,raju,/*hjh*/boju,beeju output should be : "HI",ob1,raju,boju,beeju ... (8 Replies)
Discussion started by: spc432
8 Replies

9. Shell Programming and Scripting

Delete lines that contain 3 or more words?

How can I delete lines that contain 3 or more words? I have a file, old.txt, that has multi-word phrases in it and I want to remove the lines with 3 words or more and send the output to new.txt. I've tried the following using sed but it doesn't seem to work: sed '/(\b\w+\b){3,}/d' old.txt >... (5 Replies)
Discussion started by: revax
5 Replies

10. Shell Programming and Scripting

greping out multiple words

now i have a lot of words i need to grep out of a certain line but i have to keep typing this: ex ls -l | grep -v this | grep -v that | grep -v those | grep -v them ..... i mean, it goes on like that. there has to be a way to grep all those words without having to specify that "grep... (5 Replies)
Discussion started by: Terrible
5 Replies
Login or Register to Ask a Question