awk or sed or grep filter a line and/or between strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or sed or grep filter a line and/or between strings
# 1  
Old 10-24-2017
awk or sed or grep filter a line and/or between strings

Hi, I have multiple files on a directory with the following content:
Code:
blahblah
blahblah
hostname server1
blahblah
blahblah
---BEGIN---
aaa
bbb
ccc
ddd
---END---
blahblah
blahblah
blahblah

I would like to filter all the files with awk or sed or something else so I can get below output
Code:
hostname server1
---BEGIN---
aaa
bbb
ccc
ddd
---END---

I can do awk '/---BEGIN---/,/---END---/' * but I don't get the hostname server1 line.
I have hundreds of file so the 'hostname server1' would at least give me some idea from which file is the output coming from.
Ideally I would get
Code:
hostname server1
---BEGIN---
...
---END---
hostname server2
---BEGIN---
...
---END---
...

Any suggestion on how can I achieve this?

Last edited by Scott; 10-24-2017 at 01:02 AM.. Reason: Replaced QUOTE tags with CODE tags
# 2  
Old 10-24-2017
One way:
Code:
awk '/---BEGIN---/,/---END---/ {
  if ( !F[FILENAME]++ ) print H " " FILENAME
  print $0
}' H=${HOSTNAME:-$(hostname)} *

This User Gave Thanks to Scott For This Post:
# 3  
Old 10-24-2017
I received a PM that my post is not clear, I'm sorry about that so I would like to update my post as below.

I mentioned that I have hundreds files, probably let's just start with one file.

I have a file, filename is config.txt and below is the content
Quote:
blahblah
blahblah
hostname server1
blahblah
blahblah
---BEGIN---
aaa
bbb
ccc
ddd
---END---
blahblah
blahblah
blahblah
I would like to filter using awk or sed and get something like below
Quote:
hostname server1
---BEGIN---
aaa
bbb
ccc
ddd
---END---
# 4  
Old 10-24-2017
Your post seemed perfectly clear to me, even thought I misread it! It seems you want just the hostname, not the filename.

Code:
awk '/---BEGIN---/,/---END---/ {
  if ( !F[H]++ ) print "hostname " H
  print $0
}' H=${HOSTNAME:-$(hostname)} file*

If you're pulling these files from multiple hosts, you might want to add the hostname to the filename or in the file itself.
This User Gave Thanks to Scott For This Post:
# 5  
Old 10-24-2017
Quote:
Originally Posted by Scott
Your post seemed perfectly clear to me, even thought I misread it! It seems you want just the hostname, not the filename.

Code:
awk '/---BEGIN---/,/---END---/ {
  if ( !F[H]++ ) print "hostname " H
  print $0
}' H=${HOSTNAME:-$(hostname)} file*

If you're pulling these files from multiple hosts, you might want to add the hostname to the filename or in the file itself.
Yep, the 'hostname' has same value as the 'filename' in my case so that does not really matter I think.

I'm still learning Linux ans I was expecting a one liner command. Apparently I need to learn more about script/bash scripting based on your reply.
I'll do more reading and practicing on awk and bash script.
Thanks Scott!
# 6  
Old 10-24-2017
If I understand your post correctly, you might want to try:
Code:
awk '/^hostname /;/---BEGIN---/,/---END---/' file*

These 2 Users Gave Thanks to Don Cragun For This Post:
# 7  
Old 10-24-2017
Quote:
Originally Posted by Don Cragun
If I understand your post correctly, you might want to try:
Code:
awk '/^hostname /;/---BEGIN---/,/---END---/' file*

That works perfectly! Thanks Don Cragun Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed or awk grep, that will only get the line with more characters.

Is there a command for sed and awk that will only sort the line with more characters? #cat file 123 12345 12 asdgjljhhho bac ss Output: asdgjljhhho #cat file2 11.2 12345.00 21.222 12345678.10 (2 Replies)
Discussion started by: invinzin21
2 Replies

2. Shell Programming and Scripting

Complex Filter using grep, awk or sed

Hi, I'm not very familiar witrh sed or awk and hope the somebody can help me to solve my problem. I need to filter a text report using grep, sed or awk. I would like to cut out text lines with the pattern INFO and if exists the following lines of the pattern DETAILS. I need te keep the lines with... (4 Replies)
Discussion started by: Frankg
4 Replies

3. Shell Programming and Scripting

Use of awk or sed to filter out the ouput

Hi, i am trying to get the system model with the help of awk : $ prtconf | awk '/^System Model/' System Model: IBM,8408-E8D but i want just the below outout that is command should chk for pattern <IBM,> and remove it from the final output : System Model:8408-E8D Can... (2 Replies)
Discussion started by: omkar.jadhav
2 Replies

4. Shell Programming and Scripting

Use less pipe for grep or awk sed to print the line not include xx yy zz

cat file |grep -v "xx" | grep -v "yy" |grep -v "zz" (3 Replies)
Discussion started by: yanglei_fage
3 Replies

5. Shell Programming and Scripting

Grep 2 same strings in a same line??

I have this code TrackingId:1362412470675;MSISDN:; INFO - number of clietns:3:Received response is: EMSResponse , protocolVersion=5, purchaseOptions=null, serviceData=ServiceData , screenData=CanvasData ]], title=null, titleResource=MessageResource], screenType=null]], serviceId=idBamboo,... (7 Replies)
Discussion started by: nikhil jain
7 Replies

6. Shell Programming and Scripting

Print lines between two strings multiple occurencies (with sed, awk, or grep)

Hello, I can extract lines in a file, between two strings but only one time. If there are multiple occurencies, my command show only one block. Example, monfichier.txt contains : debut_sect texte L1 texte L2 texte L3 texte L4 fin_sect donnees inutiles 1 donnees inutiles 2 ... (8 Replies)
Discussion started by: theclem35
8 Replies

7. Shell Programming and Scripting

Problems with Sed/awk/grep and line endings

Hello I have created the following script, which is designed to manipulate a text document: #!/bin/sh # Get 3 lines, (last of which is "Quantity"); adjust order; put all three on one line with tabs. FILENAME=~/Desktop/email.txt LIST=$(grep -B2 "Quantity" ${FILENAME} |awk 'BEGIN { FS = "\n"; RS... (6 Replies)
Discussion started by: benwiggy
6 Replies

8. Shell Programming and Scripting

Need to build a grep/sed/awk filter

Hi I need to to direct only the path and the name of the trace file to a new file. How do I use grep/awk/sed filter? eg. ABC.root>cat alert_omc_dg.log | grep trc ORA-00060: Deadlock detected. More info in file /u01/oradata/omc/udump/omc_dg_ora_3555.trc. ORA-00060: Deadlock detected. More... (8 Replies)
Discussion started by: geetap
8 Replies

9. UNIX for Advanced & Expert Users

bash/grep/awk/sed: How to extract every appearance of text between two specific strings

I have a text wich looks like this: clid=2 cid=6 client_database_id=35 client_nickname=Peter client_type=0|clid=3 cid=22 client_database_id=57 client_nickname=Paul client_type=0|clid=5 cid=22 client_database_id=7 client_nickname=Mary client_type=0|clid=6 cid=22 client_database_id=6... (3 Replies)
Discussion started by: Pioneer1976
3 Replies

10. Shell Programming and Scripting

Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line

Hi, I need help with using an awk or sed filter on the below line ALTER TABLE "ACCOUNT" ADD CONSTRAINT "ACCOUNT_PK" PRIMARY KEY ("ACCT_ID") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 FREELISTS 1 FREELIST GROUPS 1) TABLESPACE "WMC_DATA" LOGGING ENABLE Look for... (2 Replies)
Discussion started by: rajan_san
2 Replies
Login or Register to Ask a Question