Mad-D
Mad-D

Reputation: 4669

String handling and looping using awk and sed

I am trying to AWK a file to parse two column values as a pair and then use them in a loop to check the status of the particular application with respect to the server.

Syntax of the file:

CELL **NAME_OF_CELL** MC **SERVERNAME.COM**/PORT_NUMBER 

FILE:

#Cells
cell    app_dynamics_21  mc      dynamics21.xxxx.com/5021
cell    windows_app mc      windows_app.app.com/5041

I am interested in name_of_cell and servername.com, so my awk looks like

sed '/^\s*$/d' $FILE |grep -v -e"#" -e"server" -e"gw_ps" |  awk '{print $2"   "$4}' | grep -i -e"windows" -e"app" -e"smartphone"  > $DIRECTORY/CLEAN_FILE

CLEAN_FILE looks as mentioned below,

server    unixhost2.test.com:3115
app_dynamics_21      dynamics21.xxxx.com/5021
windows_app     windows_app.app.com/5041

As per my sed i shouldn't be seeing server in my clean_file, next i would like to read each line and hold NAME_OF_CELL in one variable and SERVERNAME.COM in another to verify status of the application and server.

Need help with SED and AWK to extract these NAME_OF_CELL and SERVERNAME.COM from the file.

Upvotes: 1

Views: 73

Answers (1)

glenn jackman
glenn jackman

Reputation: 246867

awk can do what sed and grep can do, so to put it all into one script:

awk -v IGNORECASE=1 '
    /#/ || /server/ || /gw_ps/ {next}
    /windows/ || /app/ || /smartphone/ {print $2, $4}
' "$FILE" > clean_file

But you probably want /^[[:blank:]]*#/ to remove comments, not blindly remove any line with a hash anywhere.

Upvotes: 1

Related Questions