Sed Awk Cut Grep Combination Help ?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed Awk Cut Grep Combination Help ?
# 1  
Old 03-05-2009
Sed Awk Cut Grep Combination Help ?

I have been reading for a few hours trying to educate myself enough to accomplish this task, so please know I have performed some research. Unfortunately, I am not a *NIX scripting expert, or a coder. I come from a network background instead.

SO, here is my desired outcome. I have some Cisco CSS boxes I am trying to organize data off of into a more useful format for mass migrations. I will give some sample of my raw / formatted data, and the way I would like to end up formatting it.

Here is the data (there are literally hundreds of the following data blocks in a text file. I have also changed the IP, and name for protection):

Name: vcstest019-asdfghjkl-9-www Index: 51
Type: Local State: Down
Rule ( ANY ANY )
Session Redundancy: Disabled
Redirect Domain:
Redirect String:
Keepalive: (HTTP-80:HEAD: 5 3 5 )
Keepalive Error: Not reachable failure
Keepalive Encryption: Disabled
Last Clearing of Stats Counters: 03/03/2009 00:24:17
Mtu: 1500 State Transitions: 0
Total Local Connections: 0 Total Backup Connections: 0
Current Local Connections: 0 Current Backup Connections: 0
Total Connections: 0 Max Connections: 65534
Total Reused Conns: 0 Weight Reporting: None
Weight: 1 Load: 255

What I *WANT* to grab from it is the Name, State and Total Connections number. I accomplished this via "grep -e Name -e "Total Connections" -e Type data

However, that will display it as such:

Name: vcstest004-asdfghjkl-2-direct Index: 2
Type: Local State: Suspended
Total Connections: 0 Max Connections: 65534
Name: vcstest004-asdfghjkl21-2-https Index: 3
Type: Local State: Suspended
Total Connections: 0 Max Connections: 65534

What I really WANT this to look like, is something like this:

Name: vcstest004-asdfghjkl-2-direct State: Suspended Total Connections: 0
Name: vcstest004-asdfghjkl21-2-direct State: Suspended Total Connections: 0

I am not sure how to parse through the data to yield the desired result.

Now, to FURTHER complicate things, I want to take a SECOND set of data and add it into the mix, correlating the information.


The second data set will look something like this (again, with hundreds, if not thousands of entries, and some of the names have been changes/ips, etc..)

content vcstest013-asdf-3-www
add service vcstest019-szvmHELLO-2-www
vip address
protocol tcp
port 80

content vcstest013-asdf1-4-www
add service vcstest019-szaloha225-3-www
vip address
protocol tcp
port 80

content vcstest013-asdf2-5-www
add service vcstest019-szwowser225-4-www
vip address
protocol tcp
port 80

content vcstest019-agh-3-https
add service vcstest019-szjeemonie225-2-https
vip address
port 443
protocol tcp

The "service" identifier, matches up with the "content" identifier right above it. SO.... At the end of the day, I would like something to provide me with the following information:

CONTENT Information
Service Information
What is the current State?
(from first file)
WHat is the number of Total Connections?
(from first file)

I hope this makes sense. I'm just not sure how to further narrow down the amount of data. I'm just grabbing the information off of the devices and dumping them to text files. I *REALLY* do appreciate anyone's help here. I have been working on this for a few hours, but am at a loss.
# 2  
Old 03-05-2009
Two questions:

1. How are the paragraphs in the first file separated? By blank lines?

2. How do the two files relate to each other: name in the first file matches content or service in the second one?
# 3  
Old 03-05-2009
1. There will be a blank carriage return inbetween the paragraphs.

2. The NAME (in the first one) matches Service (in the second)

Thanks for your quick response.
# 4  
Old 03-05-2009
Use nawk or /usr/xpg4/bin/awk on Solaris:

awk 'NR == FNR {
  /Name/ && n = $2
  /Local State/ && _[n] = $NF
  /Total Connections/ && _[n] = _[n] FS $3
/content/ { c = $2 }
/service/ && $NF in _ {
  split(_[$NF], t)
  print "CONTENT", c
  print "Service", $NF
  print "What is the current State?", t[1]
  print "What is the number of Total Connections?", t[2] RS
  }' file1 file2

# 5  
Old 03-05-2009
I will try that first thing tomorrow morning when I get back to work. Thank you so VERY!!! much for the help!! This could potentially save me at least 50 hours worth of manual work, and headache! Smilie SmilieSmilieSmilieSmilie
# 6  
Old 03-06-2009
If your awk version supports the sub() string function *and* if your record always have the same layout, here is another way of doing it:

awk  'BEGIN{RS="\n\n"; FS="\n"}{sub(/^.+State/, "State", $2); sub(/Max.+$/, "", $14); print $1,$2,$14}' file

Or, if you hate the one-liner style of coding:
awk  '
	sub(/^.+State/, "State", $2)
	sub(/Max.+$/, "", $14)
	print $1,$2,$14
}' file

Edit: By reading the OP, I just realized that I stopped at the first request. So, go for the Radoulov's snippet. I can't think of anything better.

Last edited by ripat; 03-06-2009 at 04:44 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Print/cut/grep/sed/ date yyyymmdd on the filename only.

I have this filename "RBD_EXTRACT_a3468_d20131118.tar.gz" and I would like print out the "yyyymmdd" only. I use this command below, but if different command like cut or print....etc. Thanks ls RBD_EXTRACT* | sed 's/.*\(........\).tar.gz$/\1/' > test.txt (9 Replies)
Discussion started by: dotran
9 Replies

2. Shell Programming and Scripting

Using a combination of sort/cut/grep/awk/join/paste/sed

I have a file and need to only select users that have a shell of “/bin/bash” in the line using awk or sed please help (4 Replies)
Discussion started by: boyboy1212
4 Replies

3. Shell Programming and Scripting

GREP/CUT/AWK to Arrays

hi people, I have a text file containing data, seperated by TAB. I want to process this tab'ed data as variable. how can I assign this? Ex: Code: 11aaa 12000 13aaa 14aaa 15aaa 16aaa 17aaa 21aaa 22000 23aaa 24aaa 25aaa 26aaa 27aaa 31aaa 32000 33aaa 34aaa 35aaa 36aaa 37aaa... (1 Reply)
Discussion started by: gc_sw
1 Replies

4. Shell Programming and Scripting

Advice using cut & echo combination commands

Hi, I am cutting data from a fixed length test file and then writing out a new record using the echo command, the problem I have is how to stop multiple spaces from being written to the output file as a single space. Example: cat filea | while read line do field1=`echo $line | cut -c1-2` ... (6 Replies)
Discussion started by: dc18
6 Replies

5. Shell Programming and Scripting

Using grep and cut within awk

Hi My input file looks like as follows: say a.txt "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss cccccc","P06 09/10","dddddd" "aaaa cc","224 AW","ss... (5 Replies)
Discussion started by: bittoo
5 Replies

6. UNIX for Dummies Questions & Answers

Awk/sed solution for grep,cut

Hi, From the file "example" with lines like below, I need the int value associated with ENG , i.e, 123 SUB: ENG123, GROUP 1 SUB: HIS124, GROUP 1 .. .. Normally , i do grep ENG example | cut -d ' ' -f 2 | cut -c 4-6 Is it possible to do it in simpler way using awk/sed ? ... (5 Replies)
Discussion started by: priyam
5 Replies

7. Shell Programming and Scripting

cut in sed/awk

Hi Can i have an example where i should be able to cut columns (like for eg cut -c 1-3) in sed or awk. Regards Dhana (12 Replies)
Discussion started by: dhanamurthy
12 Replies

8. Shell Programming and Scripting

cut sed grep or other?

Hi Is there a way to cut the last two characters off a word or number given that this word or number can be of varying length? I have tried something like TEST=`echo $OLD | cut -c 1-5` where $OLD is a variable containing a number like 1234567 which gives a result of 12345. This is fine... (4 Replies)
Discussion started by: rleebife
4 Replies

9. Shell Programming and Scripting

sed, grep, cut or combine?

I am a beginner at shell scripting, actually i am working on my first script right now. Anyway i have searched the world how to grep two letters from each word (it will always just be two words). For example: Example Blablabla I want my script to cut out Ex (from the first word) and Bl... (4 Replies)
Discussion started by: maskot
4 Replies
Login or Register to Ask a Question

Featured Tech Videos