If with grep in awk

Hi,

I have i/p file below

xxx.com
server version
dfsdfdfsd
asdafsf
---end---
diskspace
zdcdfzxcx
ZXccxvc

yyy.com
server version
w4ewr0as0dias90
324rwefdsok0
---end---
diskspace
.......


ddd.com
server version

ttt.com
diskspace

have four set of data, the first line is server name .com highlighted in blue.

if the server name found then it has to check server version (highlighted in red) and disk space (highlighted in purple) if both version and disk space present redirect to o/p1.txt file

if only disk space present redirect to another file o/p2.txt

if only server version redirect to other file o/p3.txt

below is my code just sample

awk ' 
a='server varsion' & b='disksapce'
{if (/.com/== 0) print $a $b > o/p.txt
}'  inputfile.txt

so if both disksapce andserver version present all the content (junk under server version "dfsdfdfsd asdafsf") should move to o/p file. shall i use

getline;print

to pint next line.

so please help me whether my code will work

There are some syntax errors.
You cannot use the / character as part of filename - it means a directory.

a='server varsion' && b='disksapce'
awk -v a="$a" -v b="$b" '  /.com$/ {next}  {print a, b >"output.txt"} '  inputfile.txt 

I've corrected syntax. The logic makes no sense to me. Please give expected output samples.

I have a file like below (five set of data)

Input file.txt

aaaa
server-version
dsfsrjjgorgk
sfoweiurtit
disk space
ldkosdife
dsfjioeufs

bbbb
server-version
fker9i5toek
dsfmeu90e
disk space
fsgter6y
sfe5t5gf

cccc
server-version
sdt4wtrei
sfd5tet

dddd
disk space
ssretr
dferte56y

eeee
server-version
.............

my requirement is need to pass all server name one by one (aaaa,bbbb,cccc,ddddd......)

once we pass server name need to check both server-version and disk space are available if yes then redirect to o/p file like below

output1.txt


aaaa
server-version
dsfsrjjgorgk
sfoweiurtit
disk space
ldkosdife
dsfjioeufs

bbbb
server-version
fker9i5toek
dsfmeu90e
disk space
fsgter6y
sfe5t5gf

If only server-version is available rediect another file

output2.txt

cccc
server-version
sdt4wtrei
sfd5tet

eeee
server-version
.............

If only disk space is available redirect to third o/p file.

I am not sure how we can pass server name one by one (aaaa,bbbb,cccc..........) since server names are different.

so once I can pass it I will assign server-version and disk space in variable.

Store all hostnames in a file ("servers" for example)...

cat servers
aaaa
cccc
dddd

Run the awk script on "InputFile.txt" to get the desired output separated by criteria into different files...

awk 'BEGIN {while (getline l < "servers" > 0) s[l]; RS=""; FS="\n"}
{
    if ($1 in s) {
        if ($0 ~ "server-version" && $0 ~ "disk space")        print $0 > "output1.txt"
        else if ($0 !~ "server-version" && $0 ~ "disk space")  print $0 > "output2.txt"
        else if ($0 ~ "server-version" && $0 !~ "disk space")  print $0 > "output3.txt"
    }
}' InputFile.txt
1 Like

but is there any way without placing all server name in a separate file to pass value

Looks like you have a blank line between each record, so awk can split records on a blank line:

awk '/server-version/ && /disk *space/ {print $0 "\n"}' RS= inputfile.txt

Also note that I put " *" in between disk and space, as I'm unsure if you datafile has a space there or not.

Thanks ShamRock

My doubt is if I pass only server name in output file 0

BEGIN {while (getline l < "servers name input file" > 0)

Its has only server (aaaa,bbbb,cccc...) then I can compare server name with server-version

aaaa="server-version

i.e,  if ($0 ~ "server-version" && $0 ~ "disk space")        print $0 > "output1.txt"

It has been a long time since I seen a thread that confused me as much as this thread.

Sometimes you have a list of servers; sometimes you have a file that contains a list of servers; sometimes the servers can just to be extracted from the data as the contents of the 1st line in an empty line separated group of lines.

Sometimes you are looking for lines that have server names like aaaa, bbbb, cccc, etc.; other times you have server names like aaaa.com, bbbb.com, cccc.com, etc. And, now you are looking for server name "server-version"???

Sometimes you're looking for lines that contain "server version"; other times you're looking for lines that contain "server-version".

Sometimes you're looking for lines that contain "diskspace"; other times you're looking for lines that contain "disk space".

Every time someone gives you some code to try, you say it doesn't work and show us new sample data that contains different strings to match.

Sometimes you want data in a file that contains complete groups in three files; now you just want server names in file 0!

Please take a LARGE step back, look at the data you want to process, show us a representative sample of the data you want to process *using CODE tags), show us all of the corresponding output files that should be produced from that sample input (all using CODE tags), and provide a simple English description of the logic that determines how the input file is supposed to be transformed into the various output files.